2024-03-20 05:42:35 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error("invalid type value")]
|
|
|
|
pub struct TypeValueError;
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "orm")]
|
2024-03-20 05:42:35 +01:00
|
|
|
impl From<TypeValueError> for sea_orm::sea_query::ValueTypeErr {
|
|
|
|
fn from(_: TypeValueError) -> Self {
|
|
|
|
sea_orm::sea_query::ValueTypeErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "orm")]
|
2024-03-20 05:42:35 +01:00
|
|
|
impl From<TypeValueError> for sea_orm::TryGetError {
|
|
|
|
fn from(_: TypeValueError) -> Self {
|
|
|
|
sea_orm::TryGetError::Null("value is not a valid type".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! strenum {
|
|
|
|
( $(pub enum $enum_name:ident { $($flat:ident),* ; $($deep:ident($inner:ident)),* };)+ ) => {
|
|
|
|
$(
|
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
|
|
|
|
pub enum $enum_name {
|
|
|
|
$($flat,)*
|
|
|
|
$($deep($inner),)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for $enum_name {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
$(Self::$flat => stringify!($flat),)*
|
|
|
|
$(Self::$deep(x) => x.as_ref(),)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for $enum_name {
|
2024-04-06 16:56:13 +02:00
|
|
|
type Error = $crate::macros::TypeValueError;
|
2024-03-20 05:42:35 +01:00
|
|
|
|
|
|
|
fn try_from(value:&str) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
|
|
|
$(stringify!($flat) => Ok(Self::$flat),)*
|
|
|
|
_ => {
|
|
|
|
$(
|
|
|
|
if let Ok(x) = $inner::try_from(value) {
|
|
|
|
return Ok(Self::$deep(x));
|
|
|
|
}
|
|
|
|
)*
|
2024-04-06 16:56:13 +02:00
|
|
|
Err($crate::macros::TypeValueError)
|
2024-03-20 05:42:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "orm")]
|
2024-03-20 05:42:35 +01:00
|
|
|
impl From<$enum_name> for sea_orm::Value {
|
|
|
|
fn from(value: $enum_name) -> sea_orm::Value {
|
|
|
|
sea_orm::Value::String(Some(Box::new(value.as_ref().to_string())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "orm")]
|
2024-03-20 05:42:35 +01:00
|
|
|
impl sea_orm::sea_query::ValueType for $enum_name {
|
|
|
|
fn try_from(v: sea_orm::Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
|
|
|
|
match v {
|
|
|
|
sea_orm::Value::String(Some(x)) =>
|
|
|
|
Ok(<Self as TryFrom<&str>>::try_from(x.as_str())?),
|
|
|
|
_ => Err(sea_orm::sea_query::ValueTypeErr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_name() -> String {
|
|
|
|
stringify!($enum_name).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn array_type() -> sea_orm::sea_query::ArrayType {
|
|
|
|
sea_orm::sea_query::ArrayType::String
|
|
|
|
}
|
|
|
|
|
|
|
|
fn column_type() -> sea_orm::sea_query::ColumnType {
|
|
|
|
sea_orm::sea_query::ColumnType::String(Some(24))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "orm")]
|
2024-03-20 05:42:35 +01:00
|
|
|
impl sea_orm::TryGetable for $enum_name {
|
|
|
|
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::prelude::QueryResult, index: I) -> Result<Self, sea_orm::TryGetError> {
|
|
|
|
let x : String = res.try_get_by(index)?;
|
|
|
|
Ok(Self::try_from(x.as_str())?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:34:15 +02:00
|
|
|
pub(crate) use strenum;
|
|
|
|
|
2024-03-20 05:42:35 +01:00
|
|
|
macro_rules! getter {
|
|
|
|
($name:ident -> type $t:ty) => {
|
|
|
|
fn $name(&self) -> Option<$t> {
|
|
|
|
self.get("type")?.as_str()?.try_into().ok()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-21 02:32:35 +01:00
|
|
|
($name:ident -> bool) => {
|
|
|
|
fn $name(&self) -> Option<bool> {
|
|
|
|
self.get(stringify!($name))?.as_bool()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 05:42:35 +01:00
|
|
|
($name:ident -> &str) => {
|
|
|
|
fn $name(&self) -> Option<&str> {
|
|
|
|
self.get(stringify!($name))?.as_str()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> &str) => {
|
|
|
|
fn $name(&self) -> Option<&str> {
|
|
|
|
self.get(stringify!($rename))?.as_str()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident -> f64) => {
|
|
|
|
fn $name(&self) -> Option<f64> {
|
|
|
|
self.get(stringify!($name))?.as_f64()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> f64) => {
|
|
|
|
fn $name(&self) -> Option<f64> {
|
|
|
|
self.get(stringify!($rename))?.as_f64()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 07:55:03 +01:00
|
|
|
($name:ident -> u64) => {
|
|
|
|
fn $name(&self) -> Option<u64> {
|
|
|
|
self.get(stringify!($name))?.as_u64()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> u64) => {
|
|
|
|
fn $name(&self) -> Option<u64> {
|
|
|
|
self.get(stringify!($rename))?.as_u64()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 05:42:35 +01:00
|
|
|
($name:ident -> chrono::DateTime<chrono::Utc>) => {
|
|
|
|
fn $name(&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
|
|
|
Some(
|
|
|
|
chrono::DateTime::parse_from_rfc3339(
|
|
|
|
self
|
|
|
|
.get(stringify!($name))?
|
|
|
|
.as_str()?
|
|
|
|
)
|
|
|
|
.ok()?
|
|
|
|
.with_timezone(&chrono::Utc)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> chrono::DateTime<chrono::Utc>) => {
|
|
|
|
fn $name(&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
|
|
|
Some(
|
|
|
|
chrono::DateTime::parse_from_rfc3339(
|
|
|
|
self
|
|
|
|
.get(stringify!($rename))?
|
|
|
|
.as_str()?
|
|
|
|
)
|
|
|
|
.ok()?
|
|
|
|
.with_timezone(&chrono::Utc)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident -> node $t:ty) => {
|
2024-04-06 16:56:13 +02:00
|
|
|
fn $name(&self) -> $crate::Node<$t> {
|
2024-03-20 05:42:35 +01:00
|
|
|
match self.get(stringify!($name)) {
|
2024-04-06 16:56:13 +02:00
|
|
|
Some(x) => $crate::Node::from(x.clone()),
|
|
|
|
None => $crate::Node::Empty,
|
2024-03-20 05:42:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> node $t:ty) => {
|
2024-04-06 16:56:13 +02:00
|
|
|
fn $name(&self) -> $crate::Node<$t> {
|
2024-03-20 05:42:35 +01:00
|
|
|
match self.get(stringify!($rename)) {
|
2024-04-06 16:56:13 +02:00
|
|
|
Some(x) => $crate::Node::from(x.clone()),
|
|
|
|
None => $crate::Node::Empty,
|
2024-03-20 05:42:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:34:15 +02:00
|
|
|
pub(crate) use getter;
|
|
|
|
|
2024-03-20 05:42:35 +01:00
|
|
|
macro_rules! setter {
|
2024-03-21 02:32:35 +01:00
|
|
|
($name:ident -> bool) => {
|
|
|
|
paste::item! {
|
|
|
|
fn [< set_$name >](mut self, val: Option<bool>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 02:32:35 +01:00
|
|
|
&mut self, stringify!($name), val.map(|x| serde_json::Value::Bool(x))
|
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 05:42:35 +01:00
|
|
|
($name:ident -> &str) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<&str>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($name), val.map(|x| serde_json::Value::String(x.to_string()))
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 07:55:03 +01:00
|
|
|
($name:ident::$rename:ident -> &str) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<&str>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($rename), val.map(|x| serde_json::Value::String(x.to_string()))
|
2024-03-20 07:55:03 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident -> u64) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<u64>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($name), val.map(|x| serde_json::Value::Number(serde_json::Number::from(x)))
|
2024-03-20 07:55:03 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> u64) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<u64>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($rename), val.map(|x| serde_json::Value::Number(serde_json::Number::from(x)))
|
2024-03-20 07:55:03 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 05:42:35 +01:00
|
|
|
($name:ident -> chrono::DateTime<chrono::Utc>) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<chrono::DateTime<chrono::Utc>>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($name), val.map(|x| serde_json::Value::String(x.to_rfc3339()))
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> chrono::DateTime<chrono::Utc>) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<chrono::DateTime<chrono::Utc>>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($rename), val.map(|x| serde_json::Value::String(x.to_rfc3339()))
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident -> node $t:ty ) => {
|
|
|
|
paste::item! {
|
2024-04-06 16:56:13 +02:00
|
|
|
fn [< set_$name >](mut self, val: $crate::Node<$t>) -> Self {
|
|
|
|
$crate::macros::set_maybe_node(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($name), val
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident::$rename:ident -> node $t:ty ) => {
|
|
|
|
paste::item! {
|
2024-04-06 16:56:13 +02:00
|
|
|
fn [< set_$name >](mut self, val: $crate::Node<$t>) -> Self {
|
|
|
|
$crate::macros::set_maybe_node(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, stringify!($rename), val
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident -> type $t:ty ) => {
|
|
|
|
paste::item! {
|
2024-03-21 00:04:01 +01:00
|
|
|
fn [< set_$name >](mut self, val: Option<$t>) -> Self {
|
2024-04-06 16:56:13 +02:00
|
|
|
$crate::macros::set_maybe_value(
|
2024-03-21 00:04:01 +01:00
|
|
|
&mut self, "type", val.map(|x| serde_json::Value::String(x.as_ref().to_string()))
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:34:15 +02:00
|
|
|
pub(crate) use setter;
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "unstructured")]
|
|
|
|
pub fn set_maybe_node(obj: &mut serde_json::Value, key: &str, node: crate::Node<serde_json::Value>) {
|
2024-03-20 05:42:35 +01:00
|
|
|
match node {
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Object(x) => {
|
2024-03-20 05:42:35 +01:00
|
|
|
set_maybe_value(
|
2024-04-06 16:56:13 +02:00
|
|
|
obj, key, Some(*x),
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
},
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Link(l) => {
|
2024-03-20 05:42:35 +01:00
|
|
|
set_maybe_value(
|
|
|
|
obj, key, Some(serde_json::Value::String(l.href().to_string())),
|
|
|
|
);
|
|
|
|
},
|
2024-04-23 23:26:49 +02:00
|
|
|
crate::Node::Array(arr) => {
|
2024-03-20 05:42:35 +01:00
|
|
|
set_maybe_value(
|
2024-04-23 23:26:49 +02:00
|
|
|
obj, key, Some(serde_json::Value::Array(arr.into())),
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
},
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Empty => {
|
2024-03-20 05:42:35 +01:00
|
|
|
set_maybe_value(
|
|
|
|
obj, key, None,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "unstructured")]
|
2024-03-20 05:42:35 +01:00
|
|
|
pub fn set_maybe_value(obj: &mut serde_json::Value, key: &str, value: Option<serde_json::Value>) {
|
|
|
|
if let Some(map) = obj.as_object_mut() {
|
|
|
|
match value {
|
|
|
|
Some(x) => map.insert(key.to_string(), x),
|
|
|
|
None => map.remove(key),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
tracing::error!("error setting '{key}' on json Value: not an object");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "unstructured")]
|
2024-03-20 05:42:35 +01:00
|
|
|
pub(crate) trait InsertValue {
|
2024-04-06 18:04:14 +02:00
|
|
|
fn insert_node(&mut self, k: &str, v: crate::Node<serde_json::Value>);
|
2024-03-20 05:42:35 +01:00
|
|
|
fn insert_str(&mut self, k: &str, v: Option<&str>);
|
|
|
|
fn insert_float(&mut self, k: &str, f: Option<f64>);
|
|
|
|
fn insert_timestr(&mut self, k: &str, t: Option<chrono::DateTime<chrono::Utc>>);
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:04:14 +02:00
|
|
|
#[cfg(feature = "unstructured")]
|
2024-03-20 05:42:35 +01:00
|
|
|
impl InsertValue for serde_json::Map<String, serde_json::Value> {
|
2024-04-06 18:04:14 +02:00
|
|
|
fn insert_node(&mut self, k: &str, node: crate::Node<serde_json::Value>) {
|
2024-03-20 05:42:35 +01:00
|
|
|
match node {
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Object(x) => {
|
2024-03-20 05:42:35 +01:00
|
|
|
self.insert(
|
|
|
|
k.to_string(),
|
|
|
|
*x,
|
|
|
|
);
|
|
|
|
},
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Array(ref _arr) => {
|
2024-03-20 05:42:35 +01:00
|
|
|
self.insert(
|
|
|
|
k.to_string(),
|
2024-04-06 16:56:13 +02:00
|
|
|
serde_json::Value::Array(node.into_iter().collect()),
|
2024-03-20 05:42:35 +01:00
|
|
|
);
|
|
|
|
},
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Link(l) => {
|
2024-03-20 05:42:35 +01:00
|
|
|
self.insert(
|
|
|
|
k.to_string(),
|
|
|
|
serde_json::Value::String(l.href().to_string()),
|
|
|
|
);
|
|
|
|
},
|
2024-04-06 18:04:14 +02:00
|
|
|
crate::Node::Empty => {},
|
2024-03-20 05:42:35 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_str(&mut self, k: &str, v: Option<&str>) {
|
|
|
|
if let Some(v) = v {
|
|
|
|
self.insert(
|
|
|
|
k.to_string(),
|
|
|
|
serde_json::Value::String(v.to_string()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_float(&mut self, k: &str, v: Option<f64>) {
|
|
|
|
if let Some(v) = v {
|
|
|
|
if let Some(n) = serde_json::Number::from_f64(v) {
|
|
|
|
self.insert(
|
|
|
|
k.to_string(),
|
|
|
|
serde_json::Value::Number(n),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_timestr(&mut self, k: &str, t: Option<chrono::DateTime<chrono::Utc>>) {
|
|
|
|
if let Some(published) = t {
|
|
|
|
self.insert(
|
|
|
|
k.to_string(),
|
|
|
|
serde_json::Value::String(published.to_rfc3339()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|