Added new function to field types.

This commit is contained in:
Jeff Baskin 2022-08-12 18:57:57 -04:00
parent 69e311226e
commit dda166ecbe
3 changed files with 58 additions and 10 deletions

View File

@ -1,13 +1,29 @@
use std::fmt;
mod static_string; mod static_string;
use crate::morethantext::error::MTTError;
use static_string::StaticString; use static_string::StaticString;
use std::fmt;
pub enum FieldType { pub enum FieldType {
StaticString(StaticString), StaticString(StaticString),
} }
impl FieldType {
fn new(ftype: &str, data: &str) -> Result<Self, MTTError> {
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 { impl fmt::Display for FieldType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
@ -29,7 +45,7 @@ mod converstion {
#[test] #[test]
fn from_static_string() { fn from_static_string() {
let data = "a static string"; let data = "a static string";
let field = StaticString::new(data); let field = StaticString::new(data).unwrap();
let ftype: FieldType = field.into(); let ftype: FieldType = field.into();
assert!( assert!(
ftype.to_string() == data, ftype.to_string() == data,
@ -38,4 +54,36 @@ mod converstion {
data 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
);
}
} }

View File

@ -1,3 +1,4 @@
use crate::morethantext::error::MTTError;
use std::fmt; use std::fmt;
pub struct StaticString { pub struct StaticString {
@ -5,11 +6,11 @@ pub struct StaticString {
} }
impl StaticString { impl StaticString {
pub fn new<S>(name: S) -> Self pub fn new<S>(name: S) -> Result<Self, MTTError>
where where
S: Into<String>, S: Into<String>,
{ {
Self { data: name.into() } Ok(Self { data: name.into() })
} }
} }
@ -26,7 +27,7 @@ mod creation {
#[test] #[test]
fn new_accepts_str() { fn new_accepts_str() {
let data = "some data"; let data = "some data";
let field = StaticString::new(data); let field = StaticString::new(data).unwrap();
assert!( assert!(
field.to_string() == data, field.to_string() == data,
"\n\nGot: {}\nWant: {}", "\n\nGot: {}\nWant: {}",
@ -38,7 +39,7 @@ mod creation {
#[test] #[test]
fn new_accepts_string() { fn new_accepts_string() {
let data = "actual string"; let data = "actual string";
let field = StaticString::new(data.to_string()); let field = StaticString::new(data.to_string()).unwrap();
assert!( assert!(
field.to_string() == data, field.to_string() == data,
"\n\nGot: {}\nWant: {}", "\n\nGot: {}\nWant: {}",

View File

@ -1,5 +1,5 @@
pub mod fieldtype;
pub mod error; pub mod error;
pub mod fieldtype;
use async_std::sync::{Arc, RwLock}; use async_std::sync::{Arc, RwLock};
use error::MTTError; use error::MTTError;
@ -50,8 +50,7 @@ impl Table {
Self {} Self {}
} }
async fn new_column(&self, _name: &str, _type: &str) { async fn new_column(&self, _name: &str, _type: &str) {}
}
} }
#[cfg(test)] #[cfg(test)]