From 96808da3bd692a060ea466bd53c2e77de4025923 Mon Sep 17 00:00:00 2001 From: Stuart Lynn Date: Thu, 16 May 2024 13:22:48 +0100 Subject: [PATCH 1/3] Adding a command to load the data spec from a file --- src/cli.rs | 53 ++++++++++++++++++++++++++++++++++++++++-------- test_recipe.json | 20 ++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 test_recipe.json diff --git a/src/cli.rs b/src/cli.rs index 82b1711..ef5d164 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,7 +8,7 @@ use popgetter::{ formatters::{CSVFormatter, GeoJSONFormatter, GeoJSONSeqFormatter, OutputFormatter, OutputGenerator}, Popgetter }; use serde::{Deserialize, Serialize}; -use std::{fs::File, str::FromStr}; +use std::{fs::{self, File}, str::FromStr}; use strum_macros::EnumString; /// Defines the output formats we are able to produce data in. @@ -51,13 +51,9 @@ pub struct DataCommand { output_file: String } -impl RunCommand for DataCommand { - async fn run(&self) -> Result<()> { - let popgetter = Popgetter::new()?; - let data_request = DataRequestSpec::from(self); - let mut results = popgetter.get_data_request(&data_request).await?; - - let formatter = match &self.output_format{ +impl From<&OutputFormat> for OutputFormatter{ + fn from(value: &OutputFormat) -> Self { + match value{ OutputFormat::GeoJSON=>{ OutputFormatter::GeoJSON(GeoJSONFormatter) }, @@ -68,10 +64,19 @@ impl RunCommand for DataCommand { OutputFormatter::GeoJSONSeq(GeoJSONSeqFormatter) }, _=>todo!("output format not implemented") - }; + } + } +} + +impl RunCommand for DataCommand { + async fn run(&self) -> Result<()> { + let popgetter = Popgetter::new()?; + let data_request = DataRequestSpec::from(self); + let mut results = popgetter.get_data_request(&data_request).await?; println!("{results:#?}"); let mut f = File::create(&self.output_file)?; + let formatter: OutputFormatter = (&self.output_format).into(); formatter.save(&mut f,&mut results)?; Ok(()) @@ -141,6 +146,33 @@ impl RunCommand for SurveysCommand { } } +/// The Recipe command loads a recipy file and generates the output data requested +#[derive(Args, Debug)] +pub struct RecipeCommand{ + #[arg(short, long)] + recipe_file: String, + + #[arg(short='f', long)] + output_format: OutputFormat, + + #[arg(short='o',long)] + output_file: String +} + +impl RunCommand for RecipeCommand{ + async fn run(&self) -> Result<()> { + let popgetter = Popgetter::new()?; + let config = fs::read_to_string(&self.recipe_file)?; + let data_request: DataRequestSpec = serde_json::from_str(&config)?; + let mut results = popgetter.get_data_request(&data_request).await?; + println!("{results}"); + let formatter: OutputFormatter = (&self.output_format).into(); + let mut f = File::create(&self.output_file)?; + formatter.save(&mut f,&mut results)?; + Ok(()) + } +} + /// The entrypoint for the CLI. #[derive(Parser, Debug)] #[command(version, about, long_about = None, name="popgetter", long_about="Popgetter is a tool to quickly get the data you need!")] @@ -162,8 +194,11 @@ pub enum Commands { Metrics(MetricsCommand), /// Surveys Surveys(SurveysCommand), + /// From recipe + Recipe(RecipeCommand) } + #[cfg(test)] mod tests { use super::*; diff --git a/test_recipe.json b/test_recipe.json new file mode 100644 index 0000000..8d585b0 --- /dev/null +++ b/test_recipe.json @@ -0,0 +1,20 @@ +{ + "region": [ + { + "BoundingBox": [ + -74.251785, + 40.647043, + -73.673286, + 40.91014 + ] + } + ], + "metrics": [ + { + "NamedMetric": "B01001_E002" + }, + { + "NamedMetric": "B01001_E001" + } + ] +} From 6bd7430ca061520c72cca615ad1587483dacd187 Mon Sep 17 00:00:00 2001 From: Sam Greenbury Date: Wed, 5 Jun 2024 15:02:07 +0100 Subject: [PATCH 2/3] Make recipe file positional argument --- src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index ef5d164..ac30c1b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -149,7 +149,7 @@ impl RunCommand for SurveysCommand { /// The Recipe command loads a recipy file and generates the output data requested #[derive(Args, Debug)] pub struct RecipeCommand{ - #[arg(short, long)] + #[arg(index=1)] recipe_file: String, #[arg(short='f', long)] From 022735dc5c4f7510a3f9763fbfd0c16d6a996861 Mon Sep 17 00:00:00 2001 From: Sam Greenbury Date: Wed, 5 Jun 2024 15:07:17 +0100 Subject: [PATCH 3/3] Add conversion from owned OutputFormat for OutputFormatter --- src/cli.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index ac30c1b..1713f92 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -68,6 +68,12 @@ impl From<&OutputFormat> for OutputFormatter{ } } +impl From for OutputFormatter{ + fn from(value: OutputFormat) -> Self { + Self::from(&value) + } +} + impl RunCommand for DataCommand { async fn run(&self) -> Result<()> { let popgetter = Popgetter::new()?;