1mod config;
14mod credential;
15mod endpoints;
16mod helpers;
17mod kinds;
18mod live;
19mod message;
20mod model_info;
21mod parts;
22mod request;
23mod stream;
24
25use serde_json::Value;
26
27use crate::types::ValidationError;
28
29pub(crate) use helpers::{float_from_value, int_from_value};
31pub use kinds::{roundtrip, validate, KINDS};
32
33pub trait Canonical: Sized {
35 fn from_json(value: &Value) -> Result<Self, ValidationError>;
37
38 fn to_json(&self) -> Value;
40
41 fn from_json_str(text: &str) -> Result<Self, ValidationError> {
43 let value: Value = serde_json::from_str(text)
44 .map_err(|e| ValidationError::value(format!("invalid JSON: {e}")))?;
45 Self::from_json(&value)
46 }
47
48 fn to_json_string(&self) -> String {
50 self.to_json().to_string()
51 }
52}
53
54macro_rules! impl_serde_via_canonical {
56 ($($ty:ty),+ $(,)?) => {
57 $(
58 impl ::serde::Serialize for $ty {
59 fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
60 $crate::serde::Canonical::to_json(self).serialize(serializer)
61 }
62 }
63
64 impl<'de> ::serde::Deserialize<'de> for $ty {
65 fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
66 let value = ::serde_json::Value::deserialize(deserializer)?;
67 <$ty as $crate::serde::Canonical>::from_json(&value).map_err(::serde::de::Error::custom)
68 }
69 }
70 )+
71 };
72}
73
74pub(crate) use impl_serde_via_canonical;