Added defaults to FieldType.

This commit is contained in:
Jeff Baskin 2025-08-17 08:56:34 -04:00
parent 17feadd651
commit 051fd9ebf6

View File

@ -959,6 +959,15 @@ enum FieldType {
Uuid, Uuid,
} }
impl FieldType {
fn get_default(&self) -> Field {
match self {
FieldType::StaticString => "".into(),
FieldType::Uuid => Uuid::new_v4().into(),
}
}
}
impl From<&Field> for FieldType { impl From<&Field> for FieldType {
fn from(value: &Field) -> Self { fn from(value: &Field) -> Self {
match value { match value {
@ -968,6 +977,37 @@ impl From<&Field> for FieldType {
} }
} }
#[cfg(test)]
mod fieldtypes {
use super::*;
#[test]
fn can_get_defaults_for_uuid() {
let ftype = FieldType::Uuid;
let mut ids: Vec<Uuid> = Vec::new();
for _ in 0..5 {
let result = ftype.get_default();
match result {
Field::Uuid(data) => {
assert!(!ids.contains(&data), "found duplicate id {:?} in {:?}", data, ids);
ids.push(data.clone());
},
_ => unreachable!("got {:?}: should have been uuid", result),
}
}
}
#[test]
fn can_get_defaults_for_static_string() {
let ftype = FieldType::StaticString;
let result = ftype.get_default();
match result {
Field::StaticString(data) => assert_eq!(data, ""),
_ => unreachable!("got {:?}: should have been static string", result),
}
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
enum Field { enum Field {
StaticString(String), StaticString(String),