From 107545cfe35f8fcd8255bd32aacc2f152fc1c178 Mon Sep 17 00:00:00 2001 From: Michael Lieberman Date: Sun, 3 Mar 2024 22:25:14 +0000 Subject: [PATCH] Refactor facet list This makes it easier for reuse --- skootrs-bin/src/helpers.rs | 12 +++++++----- skootrs-lib/src/service/project.rs | 22 +++++++++++++++------- skootrs-model/src/skootrs/mod.rs | 19 +++---------------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/skootrs-bin/src/helpers.rs b/skootrs-bin/src/helpers.rs index cf4f997..43a552d 100644 --- a/skootrs-bin/src/helpers.rs +++ b/skootrs-bin/src/helpers.rs @@ -184,12 +184,15 @@ impl Facet { let facet_get_params = if let Some(p) = facet_get_params { p } else { - let project = Project::get(config, project_service, None).await?; - let facet_map_keys = project.facet_key_set(); + // let project = Project::get(config, project_service, None).await?; + let project_get_params = Project::prompt_get(config).await?; + let facet_map_keys = project_service + .list_facets(project_get_params.clone()) + .await?; let fmk = Facet::prompt_get(config, facet_map_keys.into_iter().collect())?; FacetGetParams { - project_url: project.repo.full_url(), facet_map_key: fmk, + project_get_params, } }; @@ -226,8 +229,7 @@ impl Facet { Some(p) => p, None => Project::prompt_get(config).await?, }; - let project = project_service.get(project_get_params).await?; - let facet_map_keys = project.facet_key_set(); + let facet_map_keys = project_service.list_facets(project_get_params).await?; println!("{}", serde_json::to_string(&facet_map_keys)?); Ok(()) } diff --git a/skootrs-lib/src/service/project.rs b/skootrs-lib/src/service/project.rs index dffb1d3..b72cccf 100644 --- a/skootrs-lib/src/service/project.rs +++ b/skootrs-lib/src/service/project.rs @@ -59,6 +59,16 @@ pub trait ProjectService { &self, params: FacetGetParams, ) -> impl std::future::Future> + Send; + + /// Lists the facets of an initialized project. + /// + /// # Errors + /// + /// Returns an error if the list of facets can't be fetched. + fn list_facets( + &self, + params: ProjectGetParams, + ) -> impl std::future::Future, SkootError>> + Send; } /// The `LocalProjectService` struct provides an implementation of the `ProjectService` trait for initializing @@ -109,7 +119,6 @@ where repo: initialized_repo.clone(), ecosystem: initialized_ecosystem.clone(), }; - //let facet_set_params = facet_set_params_generator.generate_default(&common_params)?; let source_facet_set_params = facet_set_params_generator .generate_default_source_bundle_facet_params(&common_params)?; let api_facet_set_params = @@ -164,12 +173,7 @@ where &self, params: FacetGetParams, ) -> Result { - let initialized_project = self - .get(ProjectGetParams { - project_url: params.project_url.clone(), - }) - .await?; - //let facet = initialized_project.facets.iter().find(|f| f.facet_type() == params.facet_type); + let initialized_project = self.get(params.project_get_params.clone()).await?; let facet = initialized_project .facets .get(¶ms.facet_map_key) @@ -220,6 +224,10 @@ where InitializedFacet::SourceFile(_) => Err(SkootError::from("Facet type not supported")), } } + + async fn list_facets(&self, params: ProjectGetParams) -> Result, SkootError> { + Ok(self.get(params).await?.facets.keys().cloned().collect()) + } } #[cfg(test)] diff --git a/skootrs-model/src/skootrs/mod.rs b/skootrs-model/src/skootrs/mod.rs index 6c92544..8127306 100644 --- a/skootrs-model/src/skootrs/mod.rs +++ b/skootrs-model/src/skootrs/mod.rs @@ -15,12 +15,7 @@ pub mod facet; -use std::{ - collections::{HashMap, HashSet}, - error::Error, - fmt, - str::FromStr, -}; +use std::{collections::HashMap, error::Error, fmt, str::FromStr}; use serde::{Deserialize, Serialize}; use strum::{EnumString, VariantNames}; @@ -72,14 +67,6 @@ pub struct InitializedProject { pub facets: HashMap, } -impl InitializedProject { - /// Returns the set of keys for the facet `HashMap` - #[must_use] - pub fn facet_key_set(&self) -> HashSet { - self.facets.keys().cloned().collect() - } -} - /// A helper enum for how a facet can be pulled from a `HashMap` #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "openapi", derive(ToSchema))] @@ -190,8 +177,8 @@ pub enum ProjectOutputType { #[derive(Serialize, Deserialize, Clone, Debug)] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub struct FacetGetParams { - /// The URL of the Skootrs project to get the facet from. - pub project_url: String, + /// Parameters for first getting the project. + pub project_get_params: ProjectGetParams, /// The key of the facet to get from the project. pub facet_map_key: FacetMapKey, }