use crate::Validator;
use crate::error::{Error, ErrorKind};
use tanzim_value::{Value, ValueType};
#[derive(Debug, Clone, Default)]
pub struct Uuid;
impl Uuid {
pub fn new() -> Self {
Self
}
}
impl Validator for Uuid {
fn validate(&self, value: &mut Value) -> Result<(), Error> {
let text = match value {
Value::String(text) => text,
other => {
return Err(Error::new(ErrorKind::Type {
expected: ValueType::String,
found: other.type_name(),
}));
}
};
match uuid::Uuid::parse_str(text) {
Ok(_) => Ok(()),
Err(_) => Err(Error::new(ErrorKind::Format { expected: "uuid" })),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_and_rejects() {
assert!(
Uuid::new()
.validate(&mut Value::String(
"67e55044-10b1-426f-9247-bb680e5fe0c8".into()
))
.is_ok()
);
assert!(
Uuid::new()
.validate(&mut Value::String("nope".into()))
.is_err()
);
}
}