querio/
lib.rs

1pub use querio_derive::*;
2pub use strung::prelude::*;
3pub use intuple::*;
4
5/* ---------------------------------- Input --------------------------------- */
6/// Marks a struct for its fields to be used as query variable inputs.
7pub trait QuerioInput {
8    fn querio_input(&self) -> String;
9}
10#[derive(Intuple)]
11pub struct QuerioInputUnit;
12impl QuerioInput for QuerioInputUnit {
13    fn querio_input(&self) -> String {"".to_string()}
14}
15
16/* --------------------------------- Output --------------------------------- */
17/// Marks a struct as query output.
18pub trait QuerioOutput {
19    const QUERIO_OUTPUT: &'static str;
20}
21
22pub struct QuerioOutputUnit;
23impl QuerioOutput for QuerioOutputUnit {
24    const QUERIO_OUTPUT: &'static str = "";
25}
26
27/* -------------------------------- Variables ------------------------------- */
28#[derive(Strung,Intuple)]
29pub struct QuerioVariableUnit;
30pub type QuerioSectionsUnit = StrungUnit;
31
32
33/* ---------------------------------- Query --------------------------------- */
34/// Marks a struct as query
35pub trait Querio {
36
37    /* ---------------------------------- Input --------------------------------- */
38    #[cfg(feature = "native_input")]
39    type QuerioInputA: QuerioInput + IntupleStruct;
40    #[cfg(feature = "native_input")]
41    type QuerioInputB: QuerioInput + IntupleStruct;
42
43    /* -------------------------------- Variable -------------------------------- */
44    /// Specify varaibles using a struct with the [Strung]-Trait
45    #[cfg(feature = "variables")]
46    type QuerioVariable: Strung + IntupleStruct;
47
48    /* --------------------------------- Output --------------------------------- */    
49    #[cfg(feature = "native_output")]
50    type QuerioOutput: QuerioOutput;
51
52    /* ---------------------------------- Query --------------------------------- */
53    const QUERY: &'static str;
54
55    /* ------------------------------ Merging Query ----------------------------- */
56    /// Merges the query string and the in- and output together to the final query and returns it.
57    /// NOTE: Needed 3 inputs for a project I originally made this crate for, might change to 2 at some point
58    fn querio(
59        #[cfg(feature = "native_input")]
60        native_a:&Self::QuerioInputA,
61        #[cfg(feature = "native_input")]
62        native_b:&Self::QuerioInputB, 
63        #[cfg(feature = "variables")]
64        hard:&Self::QuerioVariable
65    ) -> String {
66        let mut query = Self::QUERY.to_string();
67        #[cfg(feature = "native_output")]
68        {query = query.replace("<Output>",Self::QuerioOutput::QUERIO_OUTPUT);}
69        #[cfg(feature = "variables")]
70        {query = hard.strung_hashtag(&query);}
71        #[cfg(feature = "native_input")]
72        {query = query.replace("<Input>",&(native_a.querio_input()+&native_b.querio_input()));}
73        query
74    }
75    /// Shorter form of querio, using tuple as params
76    fn qrio(
77        #[cfg(feature = "native_input")]
78        native_a:<<Self as Querio>::QuerioInputA as IntupleStruct>::Intuple, 
79        #[cfg(feature = "native_input")]
80        native_b:<<Self as Querio>::QuerioInputB as IntupleStruct>::Intuple, 
81        #[cfg(feature = "variables")]
82        hard:<<Self as Querio>::QuerioVariable as IntupleStruct>::Intuple, 
83    ) -> String { Self::querio(
84        #[cfg(feature = "native_input")]
85        &Self::QuerioInputA::from_tuple(native_a),
86        #[cfg(feature = "native_input")]
87        &Self::QuerioInputB::from_tuple(native_b),
88        #[cfg(feature = "variables")]
89        &Self::QuerioVariable::from_tuple(hard)
90    )}
91
92}