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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use async_trait::async_trait;
use regex::Regex;
use std::collections::HashMap;
use url::Url;

pub mod detectors;
pub mod getters;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("invalid url: {0}, reason: {1}")]
    InvalidUrl(String, String),

    #[error(transparent)]
    Io(#[from] std::io::Error),

    #[error("client not set")]
    ClientNotSet,

    #[error("source file not found")]
    SourceNotFound,

    #[error("getter for {0} not found")]
    GetterNotFound(String),

    #[error("destination path already exists and is not a symlink")]
    DestinationExists,
    #[error("destination could not be created")]
    DestinationNotCreated,

    #[error(transparent)]
    UrlParseError(#[from] url::ParseError),

    #[error(transparent)]
    Unknown(#[from] Box<dyn std::error::Error>),
}

pub trait Detector {
    fn detect(&self, path: &str) -> Result<Option<String>, Error>;
}

/// Getter trait
/// Implement this trait to add a new getter
///
/// ## Extending Gette
///
/// Gette is designed to be extensible. You can add your own getters by implementing this trait.
/// the first step is to create a struct that implements this trait:
///
/// ```ignore
/// use gette::getter;
/// use async_trait::async_trait;
///
/// pub struct mygetter;
/// #[async_trait]
/// impl getter for mygetter {
///     async fn get(&self, _dest: &str, _source: &str) -> result<(), gette::error> {
///       ok(())   
///     }
/// }
///```
///
/// the next step is to add it to the builder:
///
///```rust
/// use gette::RequestBuilder;
/// # use gette::Getter;
/// # use async_trait::async_trait;
///
/// # pub struct Mygetter;
/// # #[async_trait]
/// # impl Getter for Mygetter {
/// #     async fn get(&self, _dest: &str, _source: &str) -> Result<(), gette::Error> {
/// #       Ok(())   
/// #     }
/// # }
///
/// # tokio_test::block_on(async {
/// let b = RequestBuilder::builder().src("mygetter://test.txt".to_string())
///     .dest("test2.txt".to_string())
///     .add_getter("mygetter", Box::new(Mygetter))
///     .get()
///     .await
///     .unwrap();
/// # })
///```
#[async_trait]
pub trait Getter {
    async fn get(&self, dest: &str, source: &str) -> Result<(), Error>;
    async fn set_client(&mut self) -> Result<(), Error> {
        Ok(())
    }
}

pub trait Decompressor {}

#[derive(Default, Debug)]
pub struct NoSrc;
#[derive(Default, Debug)]
pub struct Src(String);

#[derive(Default, Debug)]
pub struct NoDest;
#[derive(Default, Debug)]
pub struct Dest(String);

pub struct RequestBuilder<S, D> {
    src: S,
    dest: D,
    detectors: Vec<Box<dyn Detector>>,
    getters: HashMap<String, Box<dyn Getter + Send>>,
}

impl Default for RequestBuilder<NoSrc, NoDest> {
    fn default() -> Self {
        let mut getters: HashMap<String, Box<dyn Getter + Send>> = HashMap::new();
        getters.insert("file".to_string(), Box::new(getters::File));

        let s3 = getters::S3::default();
        getters.insert("s3".to_string(), Box::new(s3));

        Self {
            src: NoSrc,
            dest: NoDest,
            getters,
            detectors: vec![Box::new(detectors::File), Box::new(detectors::S3)],
        }
    }
}

impl RequestBuilder<NoSrc, NoDest> {
    pub fn builder() -> Self {
        Default::default()
    }
}

impl<D> RequestBuilder<NoSrc, D> {
    pub fn src(self, src: String) -> RequestBuilder<Src, D> {
        let Self {
            src: _,
            dest,
            detectors,
            getters,
        } = self;

        RequestBuilder {
            src: Src(src),
            dest,
            detectors,
            getters,
        }
    }
}

impl<S> RequestBuilder<S, NoDest> {
    pub fn dest(self, dest: String) -> RequestBuilder<S, Dest> {
        let Self {
            src,
            dest: _,
            detectors,
            getters,
        } = self;

        RequestBuilder {
            src,
            dest: Dest(dest),
            detectors,
            getters,
        }
    }
}

impl<S, D> RequestBuilder<S, D> {
    pub fn add_getter(mut self, name: &str, getter: Box<dyn Getter + Send>) -> Self {
        self.getters.insert(name.to_string(), getter);
        self
    }

    pub fn add_detector(mut self, detector: Box<dyn Detector>) -> Self {
        self.detectors.push(detector);
        self
    }
}

impl RequestBuilder<Src, Dest> {
    fn detect(&self) -> Result<String, Error> {
        let (is_force, _) = get_forced_proto(&self.src.0);

        if Url::parse(self.src.0.as_str()).is_ok() {
            return Ok(self.src.0.clone());
        }

        for d in self.detectors.iter() {
            let res = d.detect(&self.src.0)?;

            println!("res: {:?}", res);

            if res.is_none() {
                continue;
            }

            let src: &str = &res.unwrap();
            let (forced_proto, src) = get_forced_proto(src);

            if is_force.is_some() {
                return Ok(format!("{}+{}", is_force.unwrap(), src));
            } else if forced_proto.is_some() {
                return Ok(format!("{}+{}", forced_proto.unwrap(), src));
            }

            return Ok(src.to_string());
        }

        Err(Error::GetterNotFound(self.src.0.clone()))
    }

    pub async fn get(&self) -> Result<(), Error> {
        let src = self.detect()?;

        let (mut forced, src) = get_forced_proto(&src);

        let parsed_url = Url::parse(src)?;
        if forced.is_none() {
            forced = Some(parsed_url.scheme());
        }

        if let Some(getter) = self.getters.get(forced.unwrap()) {
            return getter.get(&self.dest.0, src).await;
        }

        Ok(())
    }
}

fn get_forced_proto(v: &str) -> (Option<&str>, &str) {
    if let Some(re) = Regex::new(r"^([A-Za-z0-9]+)\+(.*)$").unwrap().captures(v) {
        return (
            Some(re.get(1).unwrap().as_str()),
            re.get(2).unwrap().as_str(),
        );
    }

    (None, v)
}

#[cfg(test)]
mod tests {

    use std::{
        env,
        fs::{self, File},
        io::Write,
    };

    use super::*;

    #[tokio::test]
    async fn test_simple_detect() {
        let b = RequestBuilder::builder()
            .src("file://test.txt".to_string())
            .dest("test2.txt".to_string());
        let res = b.detect().unwrap();
        assert_eq!("file://test.txt", res);
    }

    #[tokio::test]
    async fn test_file_detect_without_proto() {
        let b = RequestBuilder::builder()
            .src("test.txt".to_string())
            .dest("test2.txt".to_string());

        let res = b.detect().unwrap();
        let p = env::current_dir().unwrap().join("test.txt");
        assert_eq!(format!("file://{}", p.to_str().unwrap()), res);
    }

    #[tokio::test]
    async fn test_get_call() {
        let source = "./test-get-call.txt";
        let dest = "./test-get-call-destination.txt";
        let mut f = File::create(source).unwrap();

        f.write_all("test".as_bytes()).unwrap();
        let builder = RequestBuilder::builder()
            .src(source.to_string())
            .dest(dest.to_string());

        println!("src: {:?}\ndest: {:?}", builder.src, builder.dest);
        builder.get().await.unwrap();
        fs::remove_file(source).unwrap();
        fs::remove_file(dest).unwrap();
    }
}