Skip to content

Commit

Permalink
refactor: remove some lazy static constants
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent f391df6 commit dd26c84
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cocogitto::conventional::version::VersionIncrement;
use cocogitto::git::hook::HookKind;
use cocogitto::log::filter::{CommitFilter, CommitFilters};
use cocogitto::log::output::Output;
use cocogitto::{CocoGitto, HOOK_PROFILES};
use cocogitto::{CocoGitto, SETTINGS};

const APP_SETTINGS: &[AppSettings] = &[
AppSettings::SubcommandRequiredElseHelp,
Expand All @@ -27,8 +27,9 @@ const SUBCOMMAND_SETTINGS: &[AppSettings] = &[
];

fn hook_profiles() -> Vec<&'static str> {
HOOK_PROFILES
.iter()
SETTINGS
.bump_profiles
.keys()
.map(|profile| profile.as_ref())
.collect()
}
Expand Down Expand Up @@ -186,7 +187,6 @@ fn main() -> Result<()> {
unreachable!()
};

// TODO mode to cli
cocogitto.create_version(increment, pre.as_deref(), hook_profile.as_deref())?
}
Cli::Verify { message } => {
Expand Down
4 changes: 2 additions & 2 deletions src/conventional/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::conventional::commit::Commit;
use crate::{OidOf, COMMITS_METADATA};
use crate::{OidOf, SETTINGS};
use anyhow::Result;
use itertools::Itertools;
use std::fmt::Write;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Changelog {
.into_group_map();

for (commit_type, commits) in grouped {
if let Some(meta) = COMMITS_METADATA.get(&commit_type) {
if let Some(meta) = SETTINGS.commit_types().get(&commit_type) {
write!(&mut out, "\n### {}\n\n", meta.changelog_title).unwrap();
out.extend(commits);
}
Expand Down
8 changes: 4 additions & 4 deletions src/conventional/commit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::error::CocogittoError::CommitFormat;
use crate::AUTHORS;
use crate::REMOTE_URL;
use crate::SETTINGS;
use anyhow::Result;
use chrono::{NaiveDateTime, Utc};
use colored::*;
Expand Down Expand Up @@ -84,7 +83,8 @@ impl Commit {
self.author.blue()
)
} else {
let username = AUTHORS
let username = SETTINGS
.authors
.iter()
.find(|author| author.signature == self.author);
let github_author = username.map(|user| {
Expand All @@ -93,7 +93,7 @@ impl Commit {
username = &user.username
)
});
let oid = REMOTE_URL.as_ref().map(|remote_url| {
let oid = SETTINGS.github.as_ref().map(|remote_url| {
format!("[{}]({}/commit/{})", &self.oid[0..6], remote_url, &self.oid)
});
format!(
Expand Down
42 changes: 9 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use hook::Hook;
use itertools::Itertools;
use log::filter::CommitFilters;
use semver::{Prerelease, Version};
use settings::AuthorSetting;
use std::collections::HashMap;
use std::fmt::Display;
use std::fmt::Formatter;
Expand All @@ -50,42 +49,19 @@ pub type CommitsMetadata = HashMap<CommitType, CommitConfig>;
pub const CONFIG_PATH: &str = "cog.toml";

lazy_static! {
// This cannot be carried by `Cocogitto` struct since we need it to be available in `Changelog`,
// `Commit` etc. Be ensure that `CocoGitto::new` is called before using this in order to bypass
// unwrapping in case of error.
pub static ref COMMITS_METADATA: CommitsMetadata = {
pub static ref SETTINGS: Settings = {
if let Ok(repo) = Repository::open(".") {
Settings::get(&repo).unwrap_or_default().commit_types()
Settings::get(&repo).unwrap_or_default()
} else {
Settings::default().commit_types()
Settings::default()
}
};

pub static ref HOOK_PROFILES: Vec<String> = {
if let Ok(repo) = Repository::open(".") {
Settings::get(&repo).unwrap_or_default().bump_profiles
.keys()
.cloned()
.collect()
} else {
vec![]
}
};

static ref REMOTE_URL: Option<String> = {
if let Ok(repo) = Repository::open(".") {
Settings::get(&repo).unwrap_or_default().github
} else {
None
}
};

static ref AUTHORS: Vec<AuthorSetting> = {
if let Ok(repo) = Repository::open(".") {
Settings::get(&repo).unwrap_or_default().authors
} else {
vec![]
}
// This cannot be carried by `Cocogitto` struct since we need it to be available in `Changelog`,
// `Commit` etc. Be ensure that `CocoGitto::new` is called before using this in order to bypass
// unwrapping in case of error.
pub static ref COMMITS_METADATA: CommitsMetadata = {
SETTINGS.commit_types()
};
}

Expand Down Expand Up @@ -311,7 +287,7 @@ impl CocoGitto {
.iter()
.map(|commit| {
let commit_type = &commit.message.commit_type;
match &COMMITS_METADATA.get(commit_type) {
match &SETTINGS.commit_types().get(commit_type) {
Some(_) => Ok(()),
None => Err(anyhow!(CocogittoError::CommitTypeNotAllowed {
oid: commit.oid.clone(),
Expand Down
10 changes: 5 additions & 5 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ type CommitsMetadataSettings = HashMap<String, CommitConfig>;
pub(crate) type AuthorSettings = Vec<AuthorSetting>;

#[derive(Copy, Clone)]
pub(crate) enum HookType {
pub enum HookType {
PreBump,
PostBump,
}

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct Settings {
pub struct Settings {
pub changelog_path: Option<PathBuf>,
pub github: Option<String>,
#[serde(default)]
Expand Down Expand Up @@ -79,22 +79,22 @@ impl Settings {
}
}

pub(crate) fn commit_types(&self) -> CommitsMetadata {
pub fn commit_types(&self) -> CommitsMetadata {
let commit_settings = self.commit_types.clone();
let mut custom_types = HashMap::new();

commit_settings.iter().for_each(|(key, value)| {
let _ = custom_types.insert(CommitType::from(key.as_str()), value.clone());
});

let mut default_types = Settings::get_default_commit_config();
let mut default_types = Settings::default_commit_config();

default_types.extend(custom_types);

default_types
}

fn get_default_commit_config() -> CommitsMetadata {
fn default_commit_config() -> CommitsMetadata {
let mut default_types = HashMap::new();
default_types.insert(CommitType::Feature, CommitConfig::new("Features"));
default_types.insert(CommitType::BugFix, CommitConfig::new("Bug Fixes"));
Expand Down

0 comments on commit dd26c84

Please sign in to comment.