Skip to content

Commit

Permalink
Fix a bunch of clippy lint errors
Browse files Browse the repository at this point in the history
This adds some extra docs, fixing some unused parameters, as well
as adding some ignores for some lints.
  • Loading branch information
mlieberman85 committed Mar 2, 2024
1 parent b4d1a6c commit 0f02af4
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 18 deletions.
15 changes: 14 additions & 1 deletion skootrs-bin/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use skootrs_statestore::SurrealProjectStateStore;
/// The project can be created for either Go or Maven ecosystems right now.
/// The project is created in Github, cloned down, and then initialized along with any other security supporting
/// tasks.
///
/// # Errors
///
/// Returns an error if the user is not authenticated with Github, or if the project can't be created
/// for any other reason.
pub async fn create() -> std::result::Result<(), SkootError> {
let name = Text::new("The name of the repository").prompt()?;
let description = Text::new("The description of the repository").prompt()?;
Expand Down Expand Up @@ -119,6 +124,11 @@ pub async fn create() -> std::result::Result<(), SkootError> {
///
/// This function prompts the user to select a project and then a facet of that project to fetch from the state store.
/// It then prints out the content of the facet.
///
/// # Errors
///
/// Returns an error if the state store is not able to be accessed or if the selected project or facet
/// is not found.
pub async fn get_facet() -> std::result::Result<(), SkootError> {
let projects = get_all().await?;
let repo_to_project: HashMap<String, &InitializedProject> = projects
Expand Down Expand Up @@ -174,6 +184,9 @@ pub async fn get_facet() -> std::result::Result<(), SkootError> {
/// Returns `Ok(())` if the able to print out a dump of the statestore.
///
/// This function prints out the content of the state store in a pretty printed JSON format.
/// # Errors
///
/// Returns an error if the state store is not able to be accessed.
pub async fn dump() -> std::result::Result<(), SkootError> {
let projects = get_all().await?;
println!("{}", serde_json::to_string_pretty(&projects)?);
Expand Down Expand Up @@ -207,7 +220,7 @@ fn get_facet_content(
}
InitializedFacet::APIBundle(f) => {
// TODO: This can make it unclear which API was used
let content = f.apis.iter().map(|a| format!("{:?}", a)).collect::<Vec<_>>();
let content = f.apis.iter().map(|a| format!("{a:?}")).collect::<Vec<_>>();
Ok(content.join("\n"))
},
}
Expand Down
2 changes: 1 addition & 1 deletion skootrs-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn init_tracing() {
.with(JsonStorageLayer)
.with(formatting_layer);
tracing::subscriber::set_global_default(subscriber)
.expect("Failed to install `tracing` subscriber.")
.expect("Failed to install `tracing` subscriber.");
}

#[tokio::main]
Expand Down
6 changes: 6 additions & 0 deletions skootrs-lib/src/service/ecosystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use skootrs_model::skootrs::{
/// The `EcosystemService` trait provides an interface for initializing and managing a project's ecosystem.
/// An ecosystem is the language or packaging ecosystem that a project is built in, such as Maven or Go.
pub trait EcosystemService {
/// Initializes a project's ecosystem. This involves setting up the project's package or build system.
/// For example `go mod init` for Go.
///
/// # Errors
///
/// Returns an error if the ecosystem can't be initialized.
fn initialize(
&self,
params: EcosystemParams,
Expand Down
44 changes: 37 additions & 7 deletions skootrs-lib/src/service/facet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ pub trait RootFacetService {
/// (DEPRECATED) The `SourceFileFacetService` trait provides an interface for initializing and managing a project's source
/// file facets. This includes things like initializing and managing READMEs, licenses, and security policy
/// files.
///
pub trait SourceFileFacetService {
/// Initializes a source file facet.
///
/// # Errors
///
/// Returns an error if the source file facet can't be initialized.
fn initialize(&self, params: SourceFileFacetParams) -> Result<SourceFileFacet, SkootError>;
}

Expand All @@ -73,13 +79,23 @@ pub trait SourceFileFacetService {
/// This replaces the `SourceFileFacetService` trait since it's more generic and can handle more than just
/// single files.
pub trait SourceBundleFacetService {
/// Initializes a source bundle facet.
///
/// # Errors
///
/// Returns an error if the source bundle facet can't be initialized.
fn initialize(
&self,
params: SourceBundleFacetParams,
) -> Result<SourceBundleFacet, SkootError>;
}

impl SourceBundleFacetService for LocalFacetService {
/// Initializes a source bundle facet.
///
/// # Errors
///
/// Returns an error if the source bundle facet can't be initialized.
fn initialize(
&self,
params: SourceBundleFacetParams,
Expand All @@ -91,7 +107,6 @@ impl SourceBundleFacetService for LocalFacetService {
InitializedEcosystem::Go(_) => GoGithubSourceBundleContentHandler {},
InitializedEcosystem::Maven(_) => todo!(),
};
let _api_bundle_handler = GithubAPIBundleHandler {};

let source_bundle_content = match params.facet_type {
SupportedFacetType::Readme
Expand Down Expand Up @@ -238,8 +253,8 @@ impl APIBundleHandler for GithubAPIBundleHandler {
) -> Result<APIBundleFacet, SkootError> {
let InitializedRepo::Github(repo) = &params.common.repo;
match params.facet_type {
SupportedFacetType::BranchProtection => self.generate_branch_protection(params, repo).await,
SupportedFacetType::VulnerabilityReporting => self.generate_vulnerability_reporting(params, repo).await,
SupportedFacetType::BranchProtection => self.generate_branch_protection(repo).await,
SupportedFacetType::VulnerabilityReporting => self.generate_vulnerability_reporting(repo).await,
_ => todo!("Not implemented yet"),
}
}
Expand All @@ -248,7 +263,6 @@ impl APIBundleHandler for GithubAPIBundleHandler {
impl GithubAPIBundleHandler {
async fn generate_branch_protection(
&self,
_params: &APIBundleFacetParams,
repo: &InitializedGithubRepo,
) -> Result<APIBundleFacet, SkootError> {
let enforce_branch_protection_endpoint = format!(
Expand Down Expand Up @@ -287,7 +301,6 @@ impl GithubAPIBundleHandler {

async fn generate_vulnerability_reporting(
&self,
_params: &APIBundleFacetParams,
repo: &InitializedGithubRepo,
) -> Result<APIBundleFacet, SkootError> {
let vulnerability_reporting_endpoint = format!(
Expand Down Expand Up @@ -606,6 +619,7 @@ impl GoGithubSourceBundleContentHandler {
module_name: String,
}

#[allow(clippy::match_wildcard_for_single_variants)]
let module = match &params.common.ecosystem {
InitializedEcosystem::Go(go) => go.module(),
_ => unreachable!("Ecosystem should be Go"),
Expand Down Expand Up @@ -719,11 +733,17 @@ impl GoGithubSourceBundleContentHandler {
pub struct FacetSetParamsGenerator {}

impl FacetSetParamsGenerator {
/// Generates the default set of facet params for a project.
/// This includes things like generating default source bundle and API bundle facet params.
///
/// # Errors
///
/// Returns an error if any of the facet set params can't be generated.
pub fn generate_default(
&self,
common_params: &CommonFacetParams,
) -> Result<FacetSetParams, Box<dyn Error + Send + Sync>> {
let source_bundle_params = self.generate_default_source_bundle(common_params)?;
let source_bundle_params = self.generate_default_source_bundle_facet_params(common_params)?;
let api_bundle_params = self.generate_default_api_bundle(common_params)?;
let total_params = FacetSetParams {
facets_params: [
Expand All @@ -736,6 +756,11 @@ impl FacetSetParamsGenerator {
Ok(total_params)
}

/// Generates the default set of API bundle facet params for a project.
///
/// # Errors
///
/// Returns an error if the default set of API bundle facets can't be generated.
pub fn generate_default_api_bundle(
&self,
common_params: &CommonFacetParams,
Expand All @@ -760,7 +785,12 @@ impl FacetSetParamsGenerator {
}

// TODO: Come up with a better solution than hard coding the default facets
pub fn generate_default_source_bundle(
/// Generates the default set of source bundle facet params for a project.
///
/// # Errors
///
/// Returns an error if the default set of source bundle facets can't be generated.
pub fn generate_default_source_bundle_facet_params(
&self,
common_params: &CommonFacetParams,
) -> Result<FacetSetParams, SkootError> {
Expand Down
9 changes: 8 additions & 1 deletion skootrs-lib/src/service/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::module_name_repetitions)]

use std::error::Error;

use crate::service::facet::{FacetSetParamsGenerator, RootFacetService};
Expand All @@ -29,6 +31,11 @@ use tracing::debug;

/// The `ProjectService` trait provides an interface for initializing and managing a Skootrs project.
pub trait ProjectService {
/// Initializes a Skootrs project.
///h
/// # Errors
///
/// Returns an error if the project can't be initialized for any reason.
fn initialize(
&self,
params: ProjectParams,
Expand Down Expand Up @@ -80,7 +87,7 @@ where
};
//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(&common_params)?;
facet_set_params_generator.generate_default_source_bundle_facet_params(&common_params)?;
let api_facet_set_params =
facet_set_params_generator.generate_default_api_bundle(&common_params)?;
let initialized_source_facets = self
Expand Down
12 changes: 12 additions & 0 deletions skootrs-lib/src/service/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ use skootrs_model::{skootrs::{GithubRepoParams, GithubUser, InitializedGithubRep
/// The `RepoService` trait provides an interface for initializing and managing a project's source code
/// repository. This repo is usually something like Github or Gitlab.
pub trait RepoService {
/// Initializes a project's source code repository. This is usually a remote repo hosted on a service
/// like Github or Gitlab.
///
/// # Errors
///
/// Returns an error if the source code repository can't be initialized.
fn initialize(&self, params: RepoParams) -> impl std::future::Future<Output = Result<InitializedRepo, SkootError>> + Send;

/// Clones a project's source code repository to the local machine.
///
/// # Errors
///
/// Returns an error if the source code repository can't be cloned to the local machine.
fn clone_local(&self, initialized_repo: InitializedRepo, path: String) -> Result<InitializedSource, SkootError>;
}

Expand Down
24 changes: 24 additions & 0 deletions skootrs-lib/src/service/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,47 @@ use super::repo::{LocalRepoService, RepoService};
/// This code is usually something a local git repo. The service differs from the repo service
/// in that it's focused on the files and not the repo itself.
pub trait SourceService {
/// Initializes a source code directory for a project. This usually involves cloning a repo from
/// a repo service.
///
/// # Errors
///
/// Returns an error if the source code directory can't be initialized.
fn initialize(
&self,
params: SourceParams,
initialized_repo: InitializedRepo,
) -> Result<InitializedSource, SkootError>;

/// Commits changes to the repo and pushed them to the remote.
///
/// # Errors
///
/// Returns an error if the changes can't be committed and pushed to the remote.
fn commit_and_push_changes(
&self,
source: InitializedSource,
message: String,
) -> Result<(), SkootError>;

/// Writes a file to the source code directory.
///
/// # Errors
///
/// Returns an error if the file can't be written to the source code directory.
fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(
&self,
source: InitializedSource,
path: P,
name: String,
contents: C,
) -> Result<(), SkootError>;

/// Reads a file from the source code directory.
///
/// # Errors
///
/// Returns an error if the file can't be read from the source code directory.
fn read_file<P: AsRef<Path>>(
&self,
source: &InitializedSource,
Expand Down
1 change: 1 addition & 0 deletions skootrs-model/src/cd_events/repo_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![allow(clippy::clone_on_copy)]
#![allow(clippy::return_self_not_must_use)]
#![allow(clippy::default_trait_access)]
#![allow(clippy::to_string_trait_impl)]

use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
Expand Down
2 changes: 2 additions & 0 deletions skootrs-model/src/skootrs/facet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
//! This includes things like README, SECURITY.md, as well as API calls
//! like enabling branch protection on GitHub.
#![allow(clippy::module_name_repetitions)]

use std::fmt;

use serde::{Serialize, Deserialize};
Expand Down
4 changes: 2 additions & 2 deletions skootrs-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! This is the crate where the REST API for Skootrs is defined. The REST API should have feature parity with the CLI.
//! The REST API uses Actix and also utilizes utoipa for OpenAPI documentation and functionality.
//! This is the crate where the REST API for `Skootrs` is defined. The REST API should have feature parity with the CLI.
//! The REST API uses `Actix` and also utilizes utoipa for `OpenAPI` documentation and functionality.
pub mod server;
28 changes: 22 additions & 6 deletions skootrs-statestore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! This is the crate where the statestore where the management of Skootrs project state is defined.
//! The statestore currently supports an in memory SurrealDB instance that writes to a file.
//! This is the crate where the statestore where the management of `Skootrs` project state is defined.
//! The statestore currently supports an in memory `SurrealDB` instance that writes to a file.
use surrealdb::{engine::local::{Db, RocksDb}, Surreal};

use skootrs_model::skootrs::{InitializedProject, SkootError};

/// The SurrealDB state store for Skootrs projects.
/// The `SurrealDB` state store for Skootrs projects.
#[derive(Debug)]
pub struct SurrealProjectStateStore {
pub db: Surreal<Db>
}

/// The functionality for the SurrealDB state store for Skootrs projects.
/// The functionality for the `SurrealDB` state store for Skootrs projects.
impl SurrealProjectStateStore {
/// Create a new SurrealDB state store for Skootrs projects if `state.db` does not exist, otherwise open it.
/// Create a new `SurrealDB` state store for Skootrs projects if `state.db` does not exist, otherwise open it.
///
/// # Errors
///
/// Returns an error if the state store can't be created or opened.
pub async fn new() -> Result<Self, SkootError> {
let db = Surreal::new::<RocksDb>("state.db").await?;
db.use_ns("kusaridev").use_db("skootrs").await?;
Expand All @@ -37,7 +41,11 @@ impl SurrealProjectStateStore {
})
}

/// Store a new project in the state store.
/// Store a new project in the state store.
///
/// # Errors
///
/// Returns an error if the project can't be stored in the state store.
pub async fn create(&self, project: InitializedProject) -> Result<Option<InitializedProject>, SkootError> {
let created = self.db
.create(("project", project.repo.full_url()))
Expand All @@ -47,6 +55,10 @@ impl SurrealProjectStateStore {
}

/// Fetch a project from the state store.
///
/// # Errors
///
/// Returns an error if the project can't be fetched from the state store.
pub async fn select(&self, repo_url: String) -> Result<Option<InitializedProject>, SkootError> {
let record = self.db
.select(("project", repo_url))
Expand All @@ -55,6 +67,10 @@ impl SurrealProjectStateStore {
}

/// Fetch all projects from the state store.
///
/// # Errors
///
/// Returns an error if all the projects can't be dumped from the state store.
pub async fn select_all(&self) -> Result<Vec<InitializedProject>, SkootError> {
let records = self.db
.select("project")
Expand Down

0 comments on commit 0f02af4

Please sign in to comment.