aoc_framework 0.1.0

A framework used to run Advent of Code Challenges
Documentation
use std::fs;

/// The `InputLoader` trait is used to load the input data for the challenge.
/// It is implemented for different types of input data such as files, strings and https.
pub trait InputLoader {
    /// Loads the input data and returns it as a `Vec<String>`.
    fn load(&self) -> Vec<String>;
}

/// Creates a `InputLoader` which loads the input data from a file.
/// Each line of the file will be returned as a `String` in the `Vec`.
/// This method is a convenience method for `FileLoader::new(path)`.
pub fn from_file(path: &str) -> FileLoader {
    FileLoader::new(path.to_string())
}

/// The `FileLoader` is used to load the input data from a file.
pub struct FileLoader {
    path: String
}

impl FileLoader {
    /// Creates a new `FileLoader` for the given path.
    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()
    }
}