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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#[cfg(feature = "use_csv_document")]
pub mod csv;
pub mod json;
pub mod jsonl;
pub mod text;
#[cfg(feature = "use_toml_document")]
pub mod toml;
#[cfg(feature = "use_xml_document")]
pub mod xml;
pub mod yaml;

#[cfg(feature = "use_csv_document")]
use self::csv::Csv;
use self::json::Json;
use self::jsonl::Jsonl;
use self::text::Text;
#[cfg(feature = "use_toml_document")]
use self::toml::Toml;
#[cfg(feature = "use_xml_document")]
use self::xml::Xml;
use self::yaml::Yaml;
use crate::connector::Connector;
use crate::Dataset;
use serde::{Deserialize, Serialize};
use std::io;
use super::Metadata;
use async_trait::async_trait;
use serde_json::Value;

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[serde(tag = "type")]
pub enum DocumentType {
    #[cfg(feature = "use_csv_document")]
    #[serde(rename = "csv")]
    Csv(Csv),
    #[serde(rename = "json")]
    Json(Json),
    #[serde(rename = "jsonl")]
    Jsonl(Jsonl),
    #[cfg(feature = "use_xml_document")]
    #[serde(rename = "xml")]
    Xml(Xml),
    #[serde(rename = "yaml")]
    #[serde(alias = "yml")]
    Yaml(Yaml),
    #[cfg(feature = "use_toml_document")]
    #[serde(rename = "toml")]
    Toml(Toml),
    #[serde(rename = "text")]
    #[serde(alias = "txt")]
    Text(Text),
}

impl Default for DocumentType {
    fn default() -> Self {
        DocumentType::Json(Json::default())
    }
}

impl DocumentType {
    pub fn document_inner(self) -> Box<dyn Document> {
        match self {
            #[cfg(feature = "use_csv_document")]
            DocumentType::Csv(document) => Box::new(document),
            DocumentType::Json(document) => Box::new(document),
            DocumentType::Jsonl(document) => Box::new(document),
            #[cfg(feature = "use_xml_document")]
            DocumentType::Xml(document) => Box::new(document),
            DocumentType::Yaml(document) => Box::new(document),
            #[cfg(feature = "use_toml_document")]
            DocumentType::Toml(document) => Box::new(document),
            DocumentType::Text(document) => Box::new(document),
        }
    }
    pub fn document(&self) -> &dyn Document {
        match self {
            #[cfg(feature = "use_csv_document")]
            DocumentType::Csv(document) => document,
            DocumentType::Json(document) => document,
            DocumentType::Jsonl(document) => document,
            #[cfg(feature = "use_xml_document")]
            DocumentType::Xml(document) => document,
            DocumentType::Yaml(document) => document,
            #[cfg(feature = "use_toml_document")]
            DocumentType::Toml(document) => document,
            DocumentType::Text(document) => document,
        }
    }
    pub fn document_mut(&mut self) -> &mut dyn Document {
        match self {
            #[cfg(feature = "use_csv_document")]
            DocumentType::Csv(document) => document,
            DocumentType::Json(document) => document,
            DocumentType::Jsonl(document) => document,
            #[cfg(feature = "use_xml_document")]
            DocumentType::Xml(document) => document,
            DocumentType::Yaml(document) => document,
            #[cfg(feature = "use_toml_document")]
            DocumentType::Toml(document) => document,
            DocumentType::Text(document) => document,
        }
    }
}

/// Every document_builder that implement this trait can get/write json_value through a connector.
#[async_trait]
pub trait Document: Send + Sync + DocumentClone + std::fmt::Debug {
    /// Apply some actions and read the data though the Connector.
    async fn read_data(&self, reader: &mut Box<dyn Connector>) -> io::Result<Dataset>;
    /// Format the data result into the document format, apply some action and write into the connector.
    async fn write_data(
        &self,
        writer: &mut dyn Connector,
        value: Value,
    ) -> io::Result<()>;
    /// Apply actions to close the document.
    async fn close(&self, _writer: &mut dyn Connector) -> io::Result<()> {
        Ok(())
    }
    fn metadata(&self) -> Metadata {
        Metadata::default()
    }
    /// Check if the str in argument has an empty data
    fn has_data(&self, str: &str) -> bool {
        !matches!(str, "")
    }
    fn entry_point_path_start(&self) -> String {
        "".to_string()
    }
    fn entry_point_path_end(&self) -> String {
        "".to_string()
    }
}

pub trait DocumentClone {
    fn clone_box(&self) -> Box<dyn Document>;
}

impl<T> DocumentClone for T
where
    T: 'static + Document + Clone,
{
    fn clone_box(&self) -> Box<dyn Document> {
        Box::new(self.clone())
    }
}

impl Clone for Box<dyn Document> {
    fn clone(&self) -> Box<dyn Document> {
        self.clone_box()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[cfg(feature = "use_csv_document")]
    #[test]
    fn it_should_deserialize_in_csv_type() {
        let config = r#"{"type":"csv"}"#;
        let document_builder_expected = DocumentType::Csv(Csv::default());
        let document_builder_result: DocumentType =
            serde_json::from_str(config).expect("Can't deserialize the config");
        assert_eq!(document_builder_expected, document_builder_result);
    }
    #[test]
    fn it_should_deserialize_in_json_type() {
        let config = r#"{"type":"json"}"#;
        let document_builder_expected = DocumentType::Json(Json::default());
        let document_builder_result: DocumentType =
            serde_json::from_str(config).expect("Can't deserialize the config");
        assert_eq!(document_builder_expected, document_builder_result);
    }
    #[test]
    fn it_should_deserialize_in_jsonl_type() {
        let config = r#"{"type":"jsonl"}"#;
        let document_builder_expected = DocumentType::Jsonl(Jsonl::default());
        let document_builder_result: DocumentType =
            serde_json::from_str(config).expect("Can't deserialize the config");
        assert_eq!(document_builder_expected, document_builder_result);
    }
    #[test]
    fn it_should_deserialize_in_yaml_type() {
        let config = r#"{"type":"yaml"}"#;
        let document_builder_expected = DocumentType::Yaml(Yaml::default());
        let document_builder_result: DocumentType =
            serde_json::from_str(config).expect("Can't deserialize the config");
        assert_eq!(document_builder_expected, document_builder_result);
    }
    #[cfg(feature = "use_xml_document")]
    #[test]
    fn it_should_deserialize_in_xml_type() {
        let config = r#"{"type":"xml"}"#;
        let document_builder_expected = DocumentType::Xml(Xml::default());
        let document_builder_result: DocumentType =
            serde_json::from_str(config).expect("Can't deserialize the config");
        assert_eq!(document_builder_expected, document_builder_result);
    }
    #[cfg(feature = "use_toml_document")]
    #[test]
    fn it_should_deserialize_in_toml_type() {
        let config = r#"{"type":"toml"}"#;
        let document_builder_expected = DocumentType::Toml(Toml::default());
        let document_builder_result: DocumentType =
            serde_json::from_str(config).expect("Can't deserialize the config");
        assert_eq!(document_builder_expected, document_builder_result);
    }
    #[test]
    #[should_panic(expected = "missing field `type`")]
    fn it_should_not_deserialize_without_type() {
        let config = r#"{}"#;
        let _document_builder_result: DocumentType = serde_json::from_str(config).unwrap();
    }
}