Adapted field type to use into()

This commit is contained in:
Jeff Baskin 2022-07-24 22:36:08 -04:00
parent 108da0267b
commit 4a810a575c
2 changed files with 40 additions and 26 deletions

View File

@ -1,16 +1,9 @@
use std::fmt;
#[derive(Clone, PartialEq)]
pub enum FieldType {
StaticString(StaticString),
}
impl FieldType {
pub fn new(ftype: &str) -> FieldType {
StaticString::new()
}
}
impl fmt::Display for FieldType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@ -19,16 +12,22 @@ impl fmt::Display for FieldType {
}
}
#[derive(Clone, PartialEq)]
impl From<StaticString> for FieldType {
fn from(data: StaticString) -> Self {
FieldType::StaticString(data)
}
}
pub struct StaticString {
data: String,
}
impl StaticString {
pub fn new() -> FieldType {
FieldType::StaticString(Self {
data: "".to_string(),
})
pub fn new<S>(name: S) -> Self
where
S: Into<String>,
{
Self { data: name.into() }
}
}
@ -38,27 +37,38 @@ impl fmt::Display for StaticString {
}
}
#[cfg(test)]
mod fieldtypes {
use super::*;
#[test]
fn create_fieldtype() {
let ftype = FieldType::new("StaticString");
assert!(ftype.to_string() == "", "Should return an empty string.");
}
}
#[cfg(test)]
mod staticstrings {
use super::*;
#[test]
fn create_static_string() {
let field = StaticString::new();
let data = "some data";
let field = StaticString::new(data);
assert!(
field.to_string() == "",
"New should return an empty string."
field.to_string() == data,
"\n\nGot: {}\nWant: {}",
field.to_string(),
data
);
let ftype: FieldType = field.into();
assert!(
ftype.to_string() == data,
"\n\nGot: {}\nWant: {}",
ftype.to_string(),
data
);
}
#[test]
fn accepts_string() {
let data = "actual string";
let field = StaticString::new(data.to_string());
assert!(
field.to_string() == data,
"\n\nGot: {}\nWant: {}",
field.to_string(),
data
);
}
}

View File

@ -164,6 +164,7 @@ impl Record {
}
}
/*
async fn update_field(&self, name: String, data: FieldType) {
let mut map = self.data.write().await;
map.insert(name, data);
@ -176,6 +177,7 @@ impl Record {
None => None,
}
}
*/
}
#[cfg(test)]
@ -235,6 +237,7 @@ mod tables {
mod records {
use super::*;
/*
#[async_std::test]
async fn update_fields() {
let rec = Record::new();
@ -258,6 +261,7 @@ mod records {
let output = rec.get_field(name).await;
assert!(output == None, "Should return an option.");
}
*/
}
/*