fuel_streams_macros/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub mod subject {
4    pub use std::fmt::Debug;
5
6    use downcast_rs::{impl_downcast, Downcast};
7    pub use indexmap::IndexMap;
8    use serde::{Deserialize, Serialize};
9    pub use serde_json;
10    pub use subject_derive::*;
11
12    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
13    pub struct FieldSchema {
14        #[serde(rename = "type")]
15        pub type_name: String,
16        pub description: Option<String>,
17    }
18
19    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
20    pub struct Schema {
21        pub id: String,
22        pub entity: String,
23        pub subject: String,
24        pub format: String,
25        #[serde(rename = "wildcard")]
26        pub query_all: String,
27        pub fields: IndexMap<String, FieldSchema>,
28        #[serde(skip_serializing_if = "Option::is_none")]
29        pub variants: Option<IndexMap<String, Schema>>,
30    }
31    impl Schema {
32        pub fn to_json(&self) -> String {
33            serde_json::to_string(self).unwrap()
34        }
35        pub fn set_variant(
36            &mut self,
37            name: String,
38            variant: Schema,
39        ) -> &mut Self {
40            if self.variants.is_none() {
41                self.variants = Some(IndexMap::new());
42            }
43            self.variants.as_mut().unwrap().insert(name, variant);
44            self
45        }
46    }
47
48    #[derive(thiserror::Error, Debug, PartialEq, Eq)]
49    pub enum SubjectError {
50        #[error("Invalid JSON conversion: {0}")]
51        InvalidJsonConversion(String),
52        #[error("Expected JSON object")]
53        ExpectedJsonObject,
54    }
55
56    pub trait IntoSubject: Debug + Downcast + Send + Sync + 'static {
57        fn id(&self) -> &'static str;
58        fn parse(&self) -> String;
59        fn query_all(&self) -> &'static str;
60        fn to_sql_where(&self) -> Option<String>;
61        fn to_sql_select(&self) -> Option<String>;
62        fn schema(&self) -> Schema;
63    }
64    impl_downcast!(IntoSubject);
65
66    pub trait FromJsonString:
67        serde::Serialize
68        + serde::de::DeserializeOwned
69        + Clone
70        + Sized
71        + Debug
72        + Send
73        + Sync
74        + 'static
75    {
76        fn from_json(json: &str) -> Result<Self, SubjectError>;
77        fn to_json(&self) -> String;
78    }
79
80    pub trait SubjectBuildable: Debug {
81        fn new() -> Self;
82    }
83
84    pub fn parse_param<V: ToString>(param: &Option<V>) -> String {
85        match param {
86            Some(val) => val.to_string(),
87            None => "*".to_string(),
88        }
89    }
90}