From f57460f3c1b8f5da615741ad319397c49ef20359 Mon Sep 17 00:00:00 2001 From: Sam Greenbury Date: Thu, 25 Jul 2024 18:25:11 +0100 Subject: [PATCH] Add MetricText for search params from data request spec --- src/data_request_spec.rs | 35 +++++++++++++++++++++++++---------- test_recipe.json | 23 +++++++++++++---------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/data_request_spec.rs b/src/data_request_spec.rs index c6d1885..e810351 100644 --- a/src/data_request_spec.rs +++ b/src/data_request_spec.rs @@ -2,10 +2,11 @@ // See [#67](https://github.com/Urban-Analytics-Technology-Platform/popgetter-cli/issues/67) use itertools::Itertools; +use nonempty::nonempty; use serde::{Deserialize, Serialize}; use crate::geo::BBox; -use crate::search::{GeometryLevel, MetricId, SearchParams, YearRange}; +use crate::search::{GeometryLevel, MetricId, SearchContext, SearchParams, SearchText, YearRange}; #[derive(Serialize, Deserialize, Debug, Default)] pub struct DataRequestSpec { @@ -18,12 +19,28 @@ pub struct DataRequestSpec { impl TryFrom for SearchParams { type Error = anyhow::Error; fn try_from(value: DataRequestSpec) -> Result { + // TODO: handle MetricSpec::DataProduct variant Ok(Self { - text: vec![], + // TODO: consider updating for regex field following [#66](https://github.com/Urban-Analytics-Technology-Platform/popgetter-cli/issues/66) + text: value + .metrics + .iter() + .filter_map(|metric| match metric { + MetricSpec::MetricText(text) => Some(SearchText { + text: text.clone(), + context: nonempty![ + SearchContext::HumanReadableName, + SearchContext::Hxl, + SearchContext::Description + ], + }), + _ => None, + }) + .collect_vec(), year_range: if let Some(v) = value.years { Some( v.iter() - .map(|year| format!("{year}...{year}").parse::()) + .map(|year| year.parse::()) .collect::, anyhow::Error>>()?, ) } else { @@ -32,12 +49,9 @@ impl TryFrom for SearchParams { metric_id: value .metrics .iter() - .filter_map(|metric| { - match metric { - MetricSpec::Metric(m) => Some(m.clone()), - // TODO: handle DataProduct variant - MetricSpec::DataProduct(_) => None, - } + .filter_map(|metric| match metric { + MetricSpec::MetricId(m) => Some(m.clone()), + _ => None, }) .collect_vec(), geometry_level: value @@ -96,7 +110,8 @@ impl TryFrom for SearchParams { #[derive(Serialize, Deserialize, Debug)] pub enum MetricSpec { - Metric(MetricId), + MetricId(MetricId), + MetricText(String), DataProduct(String), } diff --git a/test_recipe.json b/test_recipe.json index 2940bfc..0c3f117 100644 --- a/test_recipe.json +++ b/test_recipe.json @@ -1,23 +1,26 @@ { "region": [ { - "BoundingBox": [ - -74.251785, - 40.647043, - -73.673286, - 40.91014 - ] + "BoundingBox": [-74.251785, 40.647043, -73.673286, 40.91014] } ], "metrics": [ { - "Metric": "f29c1976" + "MetricId": "f29c1976" }, { - "Metric": "079f3ba3" + "MetricId": "079f3ba3" }, { - "Metric": "81cae95d" + "MetricId": "81cae95d" + }, + { + "MetricText": "Key: uniqueID, Value: B01001_001;" } - ] + ], + "years": ["2021"], + "geometry": { + "geometry_level": "tract", + "include_geoms": true + } }