diff --git a/src/message.rs b/src/message.rs index 6d337fb..476e3c2 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,3 +1,4 @@ +use chrono::prelude::*; use std::{ collections::{HashMap, HashSet}, sync::{ @@ -950,6 +951,7 @@ impl CreateDoc { #[derive(Clone, Debug, PartialEq)] enum FieldType { + DateTime, None, StaticString, Uuid, @@ -958,6 +960,7 @@ enum FieldType { impl FieldType { fn get_default(&self) -> Field { match self { + FieldType::DateTime => Utc::now().into(), FieldType::None => Field::None, FieldType::StaticString => "".into(), FieldType::Uuid => Uuid::new_v4().into(), @@ -968,6 +971,7 @@ impl FieldType { impl From<&Field> for FieldType { fn from(value: &Field) -> Self { match value { + Field::DateTime(_) => Self::DateTime, Field::None => Self::None, Field::StaticString(_) => Self::StaticString, Field::Uuid(_) => Self::Uuid, @@ -1013,6 +1017,7 @@ mod fieldtypes { #[derive(Clone, Debug, Eq, Hash, PartialEq)] enum Field { + DateTime(DateTime), None, StaticString(String), Uuid(Uuid), @@ -1024,6 +1029,12 @@ impl Field { } } +impl From> for Field { + fn from(value: DateTime) -> Self { + Self::DateTime(value) + } +} + impl From for Field { fn from(value: String) -> Self { Self::StaticString(value) @@ -1086,9 +1097,7 @@ mod fields { struct FieldSetting { fieldtype: FieldType, use_default: bool, - use_unique: bool, default_value: Field, - unique: HashSet, } impl FieldSetting { @@ -1096,9 +1105,7 @@ impl FieldSetting { Self { fieldtype: ftype, use_default: false, - use_unique: false, default_value: Field::None, - unique: HashSet::new(), } } @@ -1117,23 +1124,6 @@ impl FieldSetting { Ok(()) } - fn set_unique(&mut self) -> Result<(), MTTError> { - self.use_unique = true; - Ok(()) - } - - fn use_unique_value(&mut self, field: Field) { - if self.use_unique { - self.unique.insert(field); - } - } - - fn remove_unique_value(&mut self, field: &Field) { - if self.use_unique { - self.unique.remove(field); - } - } - fn validate(&self, value: Option) -> Result { match value { Some(data) => { @@ -1144,11 +1134,6 @@ impl FieldSetting { vft, )); } - if self.use_unique { - if self.unique.get(&data).is_some() { - return Err(MTTError::FieldDuplicate("".to_string(), data.clone())); - } - } Ok(data.clone()) } None => { @@ -1227,24 +1212,6 @@ mod fieldsettings { Err(err) => unreachable!("got {:?}: should have gotten a value", err), } } - - #[test] - fn validates_for_unique_value() { - let mut fset = FieldSetting::new(FieldType::Uuid); - let field: Field = Uuid::new_v4().into(); - fset.set_unique(); - fset.use_unique_value(field.clone()); - match fset.validate(Some(field.clone())) { - Ok(data) => unreachable!("got {:?}: should have been error", data), - Err(err) => match err { - MTTError::FieldDuplicate(key, result) => { - assert_eq!(key, ""); - assert_eq!(result, field); - } - _ => unreachable!("got {:?}: should be a duplicate field", err), - }, - } - } } #[derive(Clone, Debug)] @@ -1542,13 +1509,13 @@ mod operands { } #[derive(Clone, Debug)] -struct Specifier { +struct Operation { field_name: String, operation: Operand, value: Field, } -impl Specifier { +impl Operation { fn new(name: String, op: Operand, value: F) -> Self where F: Into, @@ -1571,7 +1538,7 @@ impl Specifier { #[derive(Clone, Debug)] struct Query { - specifiers: Vec, + specifiers: Vec, } impl Query { @@ -1585,11 +1552,11 @@ impl Query { where F: Into, { - let spec = Specifier::new(name, op, value); + let spec = Operation::new(name, op, value); self.specifiers.push(spec); } - fn iter(&self) -> impl Iterator { + fn iter(&self) -> impl Iterator { self.specifiers.iter() } } @@ -1803,7 +1770,7 @@ impl Index { Ok(()) } - fn get(&self, spec: &Specifier) -> Vec { + fn get(&self, spec: &Operation) -> Vec { let mut output = Vec::new(); for (field, oids) in self.data.iter() { if spec.validate(field) { @@ -1915,7 +1882,7 @@ mod indexes { index.add(fields[i].clone(), oids[i].clone()); } for i in 0..count { - let spec = Specifier::new("stuff".to_string(), Operand::Equal, fields[i].clone()); + let spec = Operation::new("stuff".to_string(), Operand::Equal, fields[i].clone()); let result = index.get(&spec); assert_eq!(result.len(), 1); assert_eq!(result[0], oids[i]); @@ -1931,7 +1898,7 @@ mod indexes { for i in 0..count { index.add(fields[0].clone(), oids[i].clone()); } - let spec = Specifier::new("unimportant".to_string(), Operand::Equal, fields[0].clone()); + let spec = Operation::new("unimportant".to_string(), Operand::Equal, fields[0].clone()); let result = index.get(&spec); assert_eq!(result.len(), 3); for oid in oids { @@ -1950,7 +1917,7 @@ mod indexes { index.add(fields[0].clone(), oids[i].clone()); } index.remove(&fields[0], &oids[pos]); - let spec = Specifier::new("x".to_string(), Operand::Equal, fields[0].clone()); + let spec = Operation::new("x".to_string(), Operand::Equal, fields[0].clone()); let result = index.get(&spec); assert!(!result.contains(&oids[pos]), "should have removed oid"); }