diff --git a/src/morethantext/fieldtype/mod.rs b/src/morethantext/fieldtype/mod.rs index 10d4367..4a6f959 100644 --- a/src/morethantext/fieldtype/mod.rs +++ b/src/morethantext/fieldtype/mod.rs @@ -1,13 +1,29 @@ -use std::fmt; - mod static_string; +use crate::morethantext::error::MTTError; use static_string::StaticString; +use std::fmt; pub enum FieldType { StaticString(StaticString), } +impl FieldType { + fn new(ftype: &str, data: &str) -> Result { + let field = match ftype { + "StaticString" => StaticString::new(data), + _ => Err(MTTError::new(format!( + "field type {} does not exist", + ftype + ))), + }; + match field { + Ok(fld) => Ok(fld.into()), + Err(e) => Err(e), + } + } +} + impl fmt::Display for FieldType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -29,7 +45,7 @@ mod converstion { #[test] fn from_static_string() { let data = "a static string"; - let field = StaticString::new(data); + let field = StaticString::new(data).unwrap(); let ftype: FieldType = field.into(); assert!( ftype.to_string() == data, @@ -38,4 +54,36 @@ mod converstion { data ); } + + #[test] + fn bad_field_type() -> Result<(), String> { + let field_type = "dragon"; + let err_msg = format!("field type {} does not exist", field_type); + match FieldType::new(field_type, "marmalade") { + Ok(_) => Err("Should have returned an error.".to_string()), + Err(err) => { + if err.to_string() == err_msg { + Ok(()) + } else { + Err(format!( + "Error message is incorrect: Got: '{}' Want: '{}'", + err.to_string(), + err_msg + )) + } + } + } + } + + #[test] + fn new_static_string() { + let data = "This is a test."; + let field = FieldType::new("StaticString", data).unwrap(); + assert!( + field.to_string() == data, + "\n\nGot: {}\nWant: {}\n\n", + field.to_string(), + data + ); + } } diff --git a/src/morethantext/fieldtype/static_string.rs b/src/morethantext/fieldtype/static_string.rs index f555990..8f054c6 100644 --- a/src/morethantext/fieldtype/static_string.rs +++ b/src/morethantext/fieldtype/static_string.rs @@ -1,3 +1,4 @@ +use crate::morethantext::error::MTTError; use std::fmt; pub struct StaticString { @@ -5,11 +6,11 @@ pub struct StaticString { } impl StaticString { - pub fn new(name: S) -> Self + pub fn new(name: S) -> Result where S: Into, { - Self { data: name.into() } + Ok(Self { data: name.into() }) } } @@ -26,7 +27,7 @@ mod creation { #[test] fn new_accepts_str() { let data = "some data"; - let field = StaticString::new(data); + let field = StaticString::new(data).unwrap(); assert!( field.to_string() == data, "\n\nGot: {}\nWant: {}", @@ -38,7 +39,7 @@ mod creation { #[test] fn new_accepts_string() { let data = "actual string"; - let field = StaticString::new(data.to_string()); + let field = StaticString::new(data.to_string()).unwrap(); assert!( field.to_string() == data, "\n\nGot: {}\nWant: {}", diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index a42e215..9012066 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -1,5 +1,5 @@ -pub mod fieldtype; pub mod error; +pub mod fieldtype; use async_std::sync::{Arc, RwLock}; use error::MTTError; @@ -50,8 +50,7 @@ impl Table { Self {} } - async fn new_column(&self, _name: &str, _type: &str) { - } + async fn new_column(&self, _name: &str, _type: &str) {} } #[cfg(test)]