mod error;
mod id;
mod id_class;
mod ided;
mod identifiable;
#[cfg(feature = "sqlx")]
mod postgres;
#[cfg(feature = "serde")]
mod id_enum;
#[cfg(feature = "jsonschema")]
mod jsonschema;
#[cfg(feature = "openapi")]
mod openapi;
#[cfg(feature = "serde")]
mod serde_serialize;
#[allow(unused_imports)]
pub use {error::*, id::*, id_class::*, ided::*, identifiable::*, kind_proc::*};
#[allow(unused_imports)]
#[cfg(feature = "serde")]
pub use {crate::serde_serialize::*, id_enum::*};
#[allow(unused_imports)]
#[cfg(feature = "jsonschema")]
pub use crate::jsonschema::*;
#[allow(unused_imports)]
#[cfg(feature = "openapi")]
pub use crate::openapi::*;
#[test]
fn test_id_ided() {
#[derive(Debug, Identifiable)]
#[kind(class = "Cust")]
pub struct Customer {
pub name: String,
}
#[derive(Debug, Identifiable)]
#[kind(class = "Cont")]
pub struct Contract {
}
assert_eq!(
std::mem::size_of::<Id<Customer>>(),
std::mem::size_of::<uuid::Uuid>(),
);
let id: Id<Customer> = "Cust_371c35ec-34d9-4315-ab31-7ea8889a419a".parse().unwrap();
assert!("Cust_371c35ec-34d9-4315-ab31-7ea8889a419a"
.parse::<Id<Contract>>()
.is_err());
assert_eq!(
id,
"cust_371c35ec-34d9-4315-ab31-7ea8889a419a".parse().unwrap()
);
assert_eq!(
id,
"CUST_371C35EC-34D9-4315-AB31-7EA8889A419A".parse().unwrap()
);
let new_customer = Customer {
name: "John".to_string(),
};
let customer = Ided::new(id, new_customer);
assert_eq!(customer.name, "John");
assert_eq!(
customer.id().to_string(),
"Cust_371c35ec-34d9-4315-ab31-7ea8889a419a"
);
}
#[cfg(feature = "serde")]
#[test]
fn test_serde() {
#[derive(Debug, Identifiable, serde::Serialize, serde::Deserialize)]
#[kind(class = "Cust")]
pub struct Customer {
pub name: String,
}
let json = r#"{
"id": "Cust_371c35ec-34d9-4315-ab31-7ea8889a419a",
"name": "John"
}"#;
let customer: Ided<Customer> = serde_json::from_str(json).unwrap();
assert_eq!(customer.entity().name, "John");
assert_eq!(
customer.id().to_string(),
"Cust_371c35ec-34d9-4315-ab31-7ea8889a419a"
);
let json = r#"{
"id": "Con_371c35ec-34d9-4315-ab31-7ea8889a419a",
"name": "John"
}"#;
assert!(serde_json::from_str::<Ided<Customer>>(json).is_err());
assert_eq!(
serde_json::to_string(&customer).unwrap(),
r#"{"id":"Cust_371c35ec-34d9-4315-ab31-7ea8889a419a","name":"John"}"#,
);
}