fuel_streams_macros/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![doc = include_str!("../README.md")]

pub mod subject {
    pub use std::fmt::Debug;

    use downcast_rs::{impl_downcast, Downcast};
    pub use indexmap::IndexMap;
    use serde::{Deserialize, Serialize};
    pub use serde_json;
    pub use subject_derive::*;

    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
    pub struct FieldSchema {
        #[serde(rename = "type")]
        pub type_name: String,
        pub description: Option<String>,
    }

    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
    pub struct Schema {
        pub id: String,
        pub entity: String,
        pub subject: String,
        pub format: String,
        #[serde(rename = "wildcard")]
        pub query_all: String,
        pub fields: IndexMap<String, FieldSchema>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub variants: Option<IndexMap<String, Schema>>,
    }
    impl Schema {
        pub fn to_json(&self) -> String {
            serde_json::to_string(self).unwrap()
        }
        pub fn set_variant(
            &mut self,
            name: String,
            variant: Schema,
        ) -> &mut Self {
            if self.variants.is_none() {
                self.variants = Some(IndexMap::new());
            }
            self.variants.as_mut().unwrap().insert(name, variant);
            self
        }
    }

    #[derive(thiserror::Error, Debug, PartialEq, Eq)]
    pub enum SubjectError {
        #[error("Invalid JSON conversion: {0}")]
        InvalidJsonConversion(String),
        #[error("Expected JSON object")]
        ExpectedJsonObject,
    }

    pub trait IntoSubject: Debug + Downcast + Send + Sync + 'static {
        fn id(&self) -> &'static str;
        fn parse(&self) -> String;
        fn query_all(&self) -> &'static str;
        fn to_sql_where(&self) -> Option<String>;
        fn to_sql_select(&self) -> Option<String>;
        fn schema(&self) -> Schema;
    }
    impl_downcast!(IntoSubject);

    pub trait FromJsonString:
        serde::Serialize
        + serde::de::DeserializeOwned
        + Clone
        + Sized
        + Debug
        + Send
        + Sync
        + 'static
    {
        fn from_json(json: &str) -> Result<Self, SubjectError>;
        fn to_json(&self) -> String;
    }

    pub trait SubjectBuildable: Debug {
        fn new() -> Self;
    }

    pub fn parse_param<V: ToString>(param: &Option<V>) -> String {
        match param {
            Some(val) => val.to_string(),
            None => "*".to_string(),
        }
    }
}