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
use std::io::Error;

use serde::de::DeserializeOwned;

pub enum InputSourceType {
    File(String),
    String(String),
    Json(serde_json::Value),
    JsonString(String),
}

pub trait InputSource {
    fn get_data(&self) -> Result<Vec<u8>, Error>;
}

impl InputSource for InputSourceType {
    fn get_data(&self) -> Result<Vec<u8>, Error> {
        match self {
            InputSourceType::File(path) => std::fs::read(path),
            InputSourceType::String(str) => Ok(str.as_bytes().to_vec()),
            InputSourceType::Json(json_value) => Ok(serde_json::to_vec(json_value)?),
            InputSourceType::JsonString(json_str) => Ok(json_str.as_bytes().to_vec()),
        }
    }
}

pub trait InputReader<T: DeserializeOwned> {
    type Properties;

    fn read(source: &dyn InputSource, properties: Self::Properties) -> Result<T, Error>;
}