Switched add database from option to result.

This commit is contained in:
Jeff Baskin 2023-01-27 12:44:26 -05:00
parent 766d2e753a
commit 3ca71733c2

View File

@ -13,12 +13,14 @@ impl Databases {
}
}
fn add_database(&mut self, name: &str, id: &str) -> Option<String> {
if name.len() == 0 || self.db_map.contains_key(name) {
None
fn add_database(&mut self, name: &str, id: &str) -> Result<String, DBError> {
if name.len() == 0 {
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 {
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 get_id {
output
.add_database(str::from_utf8(&name).unwrap(), str::from_utf8(&id).unwrap());
match output
.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();
id.clear();
}
@ -90,10 +100,8 @@ mod functions {
let name = "fred";
let id = "123456";
let mut dbs = Databases::new();
let output = dbs.add_database(name, id);
assert_eq!(output, Some(id.to_string()));
let output = dbs.get_database(name);
assert_eq!(output, Some(id.to_string()));
let output = dbs.add_database(name, id).unwrap();
assert_eq!(output, id.to_string());
}
#[test]
@ -101,8 +109,10 @@ mod functions {
let name = "";
let id = "123456";
let mut dbs = Databases::new();
let output = dbs.add_database(name, id);
assert_eq!(output, None);
match dbs.add_database(name, id) {
Ok(_) => assert!(false, "There should have been an error"),
Err(err) => assert_eq!(err.to_string(), "database names cannot be empty"),
}
}
#[test]
@ -110,9 +120,11 @@ mod functions {
let name = "barney";
let id = "abcde";
let mut dbs = Databases::new();
dbs.add_database(name, id);
let output = dbs.add_database(name, "09876");
assert_eq!(output, None);
dbs.add_database(name, id).unwrap();
match dbs.add_database(name, "09876") {
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);
assert_eq!(output, Some(id.to_string()));
}
@ -127,11 +139,11 @@ mod functions {
#[test]
fn list_databases() {
let mut dbs = Databases::new();
dbs.add_database("zebra", "a");
dbs.add_database("alpha", "a");
dbs.add_database("charlie", "a");
dbs.add_database("wilma", "a");
dbs.add_database("frank", "a");
dbs.add_database("zebra", "a").unwrap();
dbs.add_database("alpha", "a").unwrap();
dbs.add_database("charlie", "a").unwrap();
dbs.add_database("wilma", "a").unwrap();
dbs.add_database("frank", "a").unwrap();
let expected = ["alpha", "charlie", "frank", "wilma", "zebra"];
let output = dbs.show();
assert_eq!(output, expected);
@ -141,6 +153,7 @@ mod functions {
#[cfg(test)]
mod filedata {
use super::*;
use std::error::Error;
#[test]
fn to_bytes_new() {
@ -155,7 +168,7 @@ mod filedata {
let mut dbs = Databases::new();
let name = "something";
let id = "id";
dbs.add_database(name, id);
dbs.add_database(name, id).unwrap();
let mut expected: Vec<u8> = Vec::new();
expected.append(&mut name.as_bytes().to_vec());
expected.push(0);
@ -168,9 +181,9 @@ mod filedata {
#[test]
fn from_bytes() {
let mut dbs = Databases::new();
dbs.add_database("one", "1");
dbs.add_database("two", "2");
dbs.add_database("three", "3");
dbs.add_database("one", "1").unwrap();
dbs.add_database("two", "2").unwrap();
dbs.add_database("three", "3").unwrap();
let data = dbs.to_bytes();
let mut feed = data.iter();
let output = Databases::from_bytes(&mut feed).unwrap();
@ -198,4 +211,28 @@ mod filedata {
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")
}
}
}
}