Skip to content

Commit

Permalink
Refactor facet list
Browse files Browse the repository at this point in the history
This makes it easier for reuse
  • Loading branch information
mlieberman85 committed Mar 3, 2024
1 parent 50e81d8 commit 107545c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
12 changes: 7 additions & 5 deletions skootrs-bin/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
};

Expand Down Expand Up @@ -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(())
}
Expand Down
22 changes: 15 additions & 7 deletions skootrs-lib/src/service/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ pub trait ProjectService {
&self,
params: FacetGetParams,
) -> impl std::future::Future<Output = Result<InitializedFacet, SkootError>> + 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<Output = Result<Vec<FacetMapKey>, SkootError>> + Send;
}

/// The `LocalProjectService` struct provides an implementation of the `ProjectService` trait for initializing
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -164,12 +173,7 @@ where
&self,
params: FacetGetParams,
) -> Result<InitializedFacet, SkootError> {
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(&params.facet_map_key)
Expand Down Expand Up @@ -220,6 +224,10 @@ where
InitializedFacet::SourceFile(_) => Err(SkootError::from("Facet type not supported")),
}
}

async fn list_facets(&self, params: ProjectGetParams) -> Result<Vec<FacetMapKey>, SkootError> {
Ok(self.get(params).await?.facets.keys().cloned().collect())
}
}

#[cfg(test)]
Expand Down
19 changes: 3 additions & 16 deletions skootrs-model/src/skootrs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -72,14 +67,6 @@ pub struct InitializedProject {
pub facets: HashMap<FacetMapKey, InitializedFacet>,
}

impl InitializedProject {
/// Returns the set of keys for the facet `HashMap`
#[must_use]
pub fn facet_key_set(&self) -> HashSet<FacetMapKey> {
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))]
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 107545c

Please sign in to comment.