Switched add database from option to result.
This commit is contained in:
parent
766d2e753a
commit
3ca71733c2
@ -13,12 +13,14 @@ impl Databases {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_database(&mut self, name: &str, id: &str) -> Option<String> {
|
fn add_database(&mut self, name: &str, id: &str) -> Result<String, DBError> {
|
||||||
if name.len() == 0 || self.db_map.contains_key(name) {
|
if name.len() == 0 {
|
||||||
None
|
return Err(DBError::new("database names cannot be empty"));
|
||||||
|
} else if self.db_map.contains_key(name) {
|
||||||
|
return Err(DBError::new("database already exists"));
|
||||||
} else {
|
} else {
|
||||||
self.db_map.insert(name.to_string(), id.to_string());
|
self.db_map.insert(name.to_string(), id.to_string());
|
||||||
Some(id.to_string())
|
Ok(id.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +65,16 @@ impl FileData<Self> for Databases {
|
|||||||
}
|
}
|
||||||
if letter == 0 {
|
if letter == 0 {
|
||||||
if get_id {
|
if get_id {
|
||||||
output
|
match output
|
||||||
.add_database(str::from_utf8(&name).unwrap(), str::from_utf8(&id).unwrap());
|
.add_database(str::from_utf8(&name).unwrap(), str::from_utf8(&id).unwrap())
|
||||||
|
{
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(err) => {
|
||||||
|
let mut error = DBError::new("file corruption");
|
||||||
|
error.add_source(err);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
name.clear();
|
name.clear();
|
||||||
id.clear();
|
id.clear();
|
||||||
}
|
}
|
||||||
@ -90,10 +100,8 @@ mod functions {
|
|||||||
let name = "fred";
|
let name = "fred";
|
||||||
let id = "123456";
|
let id = "123456";
|
||||||
let mut dbs = Databases::new();
|
let mut dbs = Databases::new();
|
||||||
let output = dbs.add_database(name, id);
|
let output = dbs.add_database(name, id).unwrap();
|
||||||
assert_eq!(output, Some(id.to_string()));
|
assert_eq!(output, id.to_string());
|
||||||
let output = dbs.get_database(name);
|
|
||||||
assert_eq!(output, Some(id.to_string()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -101,8 +109,10 @@ mod functions {
|
|||||||
let name = "";
|
let name = "";
|
||||||
let id = "123456";
|
let id = "123456";
|
||||||
let mut dbs = Databases::new();
|
let mut dbs = Databases::new();
|
||||||
let output = dbs.add_database(name, id);
|
match dbs.add_database(name, id) {
|
||||||
assert_eq!(output, None);
|
Ok(_) => assert!(false, "There should have been an error"),
|
||||||
|
Err(err) => assert_eq!(err.to_string(), "database names cannot be empty"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -110,9 +120,11 @@ mod functions {
|
|||||||
let name = "barney";
|
let name = "barney";
|
||||||
let id = "abcde";
|
let id = "abcde";
|
||||||
let mut dbs = Databases::new();
|
let mut dbs = Databases::new();
|
||||||
dbs.add_database(name, id);
|
dbs.add_database(name, id).unwrap();
|
||||||
let output = dbs.add_database(name, "09876");
|
match dbs.add_database(name, "09876") {
|
||||||
assert_eq!(output, None);
|
Ok(_) => assert!(false, "There should have been an error"),
|
||||||
|
Err(err) => assert_eq!(err.to_string(), "database already exists"),
|
||||||
|
}
|
||||||
let output = dbs.get_database(name);
|
let output = dbs.get_database(name);
|
||||||
assert_eq!(output, Some(id.to_string()));
|
assert_eq!(output, Some(id.to_string()));
|
||||||
}
|
}
|
||||||
@ -127,11 +139,11 @@ mod functions {
|
|||||||
#[test]
|
#[test]
|
||||||
fn list_databases() {
|
fn list_databases() {
|
||||||
let mut dbs = Databases::new();
|
let mut dbs = Databases::new();
|
||||||
dbs.add_database("zebra", "a");
|
dbs.add_database("zebra", "a").unwrap();
|
||||||
dbs.add_database("alpha", "a");
|
dbs.add_database("alpha", "a").unwrap();
|
||||||
dbs.add_database("charlie", "a");
|
dbs.add_database("charlie", "a").unwrap();
|
||||||
dbs.add_database("wilma", "a");
|
dbs.add_database("wilma", "a").unwrap();
|
||||||
dbs.add_database("frank", "a");
|
dbs.add_database("frank", "a").unwrap();
|
||||||
let expected = ["alpha", "charlie", "frank", "wilma", "zebra"];
|
let expected = ["alpha", "charlie", "frank", "wilma", "zebra"];
|
||||||
let output = dbs.show();
|
let output = dbs.show();
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
@ -141,6 +153,7 @@ mod functions {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod filedata {
|
mod filedata {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn to_bytes_new() {
|
fn to_bytes_new() {
|
||||||
@ -155,7 +168,7 @@ mod filedata {
|
|||||||
let mut dbs = Databases::new();
|
let mut dbs = Databases::new();
|
||||||
let name = "something";
|
let name = "something";
|
||||||
let id = "id";
|
let id = "id";
|
||||||
dbs.add_database(name, id);
|
dbs.add_database(name, id).unwrap();
|
||||||
let mut expected: Vec<u8> = Vec::new();
|
let mut expected: Vec<u8> = Vec::new();
|
||||||
expected.append(&mut name.as_bytes().to_vec());
|
expected.append(&mut name.as_bytes().to_vec());
|
||||||
expected.push(0);
|
expected.push(0);
|
||||||
@ -168,9 +181,9 @@ mod filedata {
|
|||||||
#[test]
|
#[test]
|
||||||
fn from_bytes() {
|
fn from_bytes() {
|
||||||
let mut dbs = Databases::new();
|
let mut dbs = Databases::new();
|
||||||
dbs.add_database("one", "1");
|
dbs.add_database("one", "1").unwrap();
|
||||||
dbs.add_database("two", "2");
|
dbs.add_database("two", "2").unwrap();
|
||||||
dbs.add_database("three", "3");
|
dbs.add_database("three", "3").unwrap();
|
||||||
let data = dbs.to_bytes();
|
let data = dbs.to_bytes();
|
||||||
let mut feed = data.iter();
|
let mut feed = data.iter();
|
||||||
let output = Databases::from_bytes(&mut feed).unwrap();
|
let output = Databases::from_bytes(&mut feed).unwrap();
|
||||||
@ -198,4 +211,28 @@ mod filedata {
|
|||||||
Err(err) => assert_eq!(err.to_string(), "file corruption"),
|
Err(err) => assert_eq!(err.to_string(), "file corruption"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_bytes_handles_error() {
|
||||||
|
let mut data = "duplicate".as_bytes().to_vec();
|
||||||
|
data.push(0);
|
||||||
|
data.append(&mut "first".as_bytes().to_vec());
|
||||||
|
data.push(0);
|
||||||
|
data.append(&mut "duplicate".as_bytes().to_vec());
|
||||||
|
data.push(0);
|
||||||
|
data.append(&mut "second".as_bytes().to_vec());
|
||||||
|
data.push(0);
|
||||||
|
let mut feed = data.iter();
|
||||||
|
match Databases::from_bytes(&mut feed) {
|
||||||
|
Ok(_) => assert!(false, "This should have failed."),
|
||||||
|
Err(err) => {
|
||||||
|
assert_eq!(err.to_string(), "file corruption");
|
||||||
|
assert!(
|
||||||
|
err.source().is_some(),
|
||||||
|
"Should state file corruption cause."
|
||||||
|
);
|
||||||
|
assert_eq!(err.source().unwrap().to_string(), "database already exists")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user