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;
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<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 {
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
);
}
}

View File

@ -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<S>(name: S) -> Self
pub fn new<S>(name: S) -> Result<Self, MTTError>
where
S: Into<String>,
{
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: {}",

View File

@ -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)]