use std::fs;
pub trait InputLoader {
fn load(&self) -> Vec<String>;
}
pub fn from_file(path: &str) -> FileLoader {
FileLoader::new(path.to_string())
}
pub struct FileLoader {
path: String
}
impl FileLoader {
pub fn new(path: String) -> Self {
FileLoader {
path
}
}
}
impl InputLoader for FileLoader {
fn load(&self) -> Vec<String> {
let file_content = fs::read_to_string(&self.path).expect("Unable to read file");
file_content.lines().map(|line| line.to_string()).collect()
}
}
pub struct StringLoader {
input: String
}
impl StringLoader {
pub fn new(input: String) -> Self {
StringLoader {
input
}
}
}
impl InputLoader for StringLoader {
fn load(&self) -> Vec<String> {
self.input.lines().map(|line| line.to_string()).collect()
}
}
pub struct HTTPLoader {
url: String
}
impl HTTPLoader {
pub fn new(url: String) -> Self {
HTTPLoader {
url
}
}
}
impl InputLoader for HTTPLoader {
fn load(&self) -> Vec<String> {
let body = reqwest::blocking::get(&self.url).unwrap().text().unwrap();
body.lines().map(|line| line.to_string()).collect()
}
}