Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FallAngel1337 committed May 18, 2022
1 parent 287d351 commit 89ad41d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
29 changes: 14 additions & 15 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub enum OutputFormat {
Pretty,
}

pub struct Services(Vec<Service>);

impl Output {
fn new<P: AsRef<Path>>(service_name: String, output_format: OutputFormat, dest: Option<&P>) -> Self {
Expand All @@ -54,6 +53,8 @@ impl Output {
}
}

pub type Services = Vec<Service>;

impl Service {
fn generate_url(&self) -> String {
let mut url = self.url.clone();
Expand Down Expand Up @@ -138,23 +139,21 @@ impl Service {
}
}

impl Services {
pub fn new<P: AsRef<Path>>(p: P) -> Result<Self, io::Error> {
let parsed: toml::Value = toml::from_str(&fs::read_to_string(p)?).unwrap();
let mut services = vec![];

for (_, values) in parsed.as_table().unwrap() {
let service: Service = toml::from_str(&toml::to_string(values).unwrap()).unwrap();
services.push(service);
}
pub fn parse<P: AsRef<Path>>(p: &P) -> Result<Services, io::Error> {
let parsed: toml::Value = toml::from_str(&fs::read_to_string(p)?).unwrap();
let mut services = vec![];

Ok(Self(services))
for (_, values) in parsed.as_table().unwrap() {
let service: Service = toml::from_str(&toml::to_string(values).unwrap()).unwrap();
services.push(service);
}

pub fn statistics(self, format: OutputFormat, dest: Option<PathBuf>) {
for service in self.0.into_iter() {
println!("{}", service.execute(format, dest.as_ref()));
}
Ok(services)
}

pub fn run(services: Services, format: OutputFormat, dest: Option<PathBuf>) {
for service in services {
println!("{}", service.execute(format, dest.as_ref()));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl FromStr for Argument {
}

fn main() {
let default_config_file = Some("config.toml".to_string());
let default_config_file = "config.toml".to_string();

let mut args = std::env::args().skip(1);
let mut opts: HashMap<Argument, Option<String>> = HashMap::new();
Expand All @@ -52,14 +52,14 @@ fn main() {
}

let config_file = opts.get(&Argument::Config)
.unwrap_or(&default_config_file).as_ref();
.map_or(default_config_file, |config| config.as_ref().unwrap().to_string());

let services = api::Services::new(config_file.unwrap()).unwrap();

let output_format = opts.get(&Argument::Format)
.map_or(api::OutputFormat::Pretty, |fmt| api::OutputFormat::from_str(fmt.as_ref().unwrap()).unwrap());

let dest = opts.get(&Argument::Dest).map(|path| path.as_ref().unwrap()).map(PathBuf::from);
.map_or(api::OutputFormat::Pretty, |fmt| api::OutputFormat::from_str(fmt.as_ref().unwrap()).unwrap());

services.statistics(output_format, dest);
let dest = opts.get(&Argument::Dest).map(|path| path.as_ref().unwrap()).map(PathBuf::from);

let services = api::parse(&config_file).unwrap();
api::run(services, output_format, dest)
}

0 comments on commit 89ad41d

Please sign in to comment.