Skip to content

Commit

Permalink
refactor: replace RootModuleConfig with Default (starship#2458)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkna authored Mar 15, 2021
1 parent 6fd7d7b commit 052b56f
Show file tree
Hide file tree
Showing 57 changed files with 174 additions and 175 deletions.
13 changes: 6 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ use toml::Value;
/// Root config of a module.
pub trait RootModuleConfig<'a>
where
Self: ModuleConfig<'a>,
Self: ModuleConfig<'a> + Default,
{
/// Create a new root module config with default values.
fn new() -> Self;

/// Load root module config from given Value and fill unset variables with default
/// values.
fn load(config: &'a Value) -> Self {
if config.get("prompt_order").is_some() {
log::warn!("\"prompt_order\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/")
}
Self::new().load_config(config)
Self::default().load_config(config)
}

/// Helper function that will call RootModuleConfig::load(config) if config is Some,
Expand All @@ -34,11 +31,13 @@ where
if let Some(config) = config {
Self::load(config)
} else {
Self::new()
Self::default()
}
}
}

impl<'a, T: ModuleConfig<'a> + Default> RootModuleConfig<'a> for T {}

/// Parsable config.
pub trait ModuleConfig<'a>
where
Expand Down Expand Up @@ -346,7 +345,7 @@ impl StarshipConfig {
if let Some(root_config) = &self.config {
StarshipRootConfig::load(root_config)
} else {
StarshipRootConfig::new()
StarshipRootConfig::default()
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/configs/aws.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;
use starship_module_config_derive::ModuleConfig;
use std::collections::HashMap;

Expand All @@ -11,8 +11,8 @@ pub struct AwsConfig<'a> {
pub region_aliases: HashMap<String, &'a str>,
}

impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
fn new() -> Self {
impl<'a> Default for AwsConfig<'a> {
fn default() -> Self {
AwsConfig {
format: "on [$symbol($profile )(\\($region\\) )]($style)",
symbol: "☁️ ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/battery.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -14,8 +14,8 @@ pub struct BatteryConfig<'a> {
pub format: &'a str,
}

impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> {
fn new() -> Self {
impl<'a> Default for BatteryConfig<'a> {
fn default() -> Self {
BatteryConfig {
full_symbol: "",
charging_symbol: "",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/character.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -11,8 +11,8 @@ pub struct CharacterConfig<'a> {
pub disabled: bool,
}

impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> {
fn new() -> Self {
impl<'a> Default for CharacterConfig<'a> {
fn default() -> Self {
CharacterConfig {
format: "$symbol ",
success_symbol: "[❯](bold green)",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/cmake.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct CMakeConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for CMakeConfig<'a> {
fn new() -> Self {
impl<'a> Default for CMakeConfig<'a> {
fn default() -> Self {
CMakeConfig {
format: "via [$symbol($version )]($style)",
symbol: "△ ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/cmd_duration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct CmdDurationConfig<'a> {
pub min_time_to_notify: i64,
}

impl<'a> RootModuleConfig<'a> for CmdDurationConfig<'a> {
fn new() -> Self {
impl<'a> Default for CmdDurationConfig<'a> {
fn default() -> Self {
CmdDurationConfig {
min_time: 2_000,
format: "took [$duration]($style) ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/conda.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -12,8 +12,8 @@ pub struct CondaConfig<'a> {
pub disabled: bool,
}

impl<'a> RootModuleConfig<'a> for CondaConfig<'a> {
fn new() -> Self {
impl<'a> Default for CondaConfig<'a> {
fn default() -> Self {
CondaConfig {
truncation_length: 1,
format: "via [$symbol$environment]($style) ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/crystal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct CrystalConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for CrystalConfig<'a> {
fn new() -> Self {
impl<'a> Default for CrystalConfig<'a> {
fn default() -> Self {
CrystalConfig {
format: "via [$symbol($version )]($style)",
symbol: "🔮 ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/custom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig, VecOr};
use crate::config::{ModuleConfig, VecOr};

use starship_module_config_derive::ModuleConfig;

Expand Down Expand Up @@ -26,8 +26,8 @@ pub struct CustomConfig<'a> {
pub directories: Directories<'a>,
}

impl<'a> RootModuleConfig<'a> for CustomConfig<'a> {
fn new() -> Self {
impl<'a> Default for CustomConfig<'a> {
fn default() -> Self {
CustomConfig {
format: "[$symbol($output )]($style)",
symbol: "",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/dart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct DartConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for DartConfig<'a> {
fn new() -> Self {
impl<'a> Default for DartConfig<'a> {
fn default() -> Self {
DartConfig {
format: "via [$symbol($version )]($style)",
symbol: "🎯 ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/directory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;
use indexmap::IndexMap;

use starship_module_config_derive::ModuleConfig;
Expand All @@ -19,8 +19,8 @@ pub struct DirectoryConfig<'a> {
pub home_symbol: &'a str,
}

impl<'a> RootModuleConfig<'a> for DirectoryConfig<'a> {
fn new() -> Self {
impl<'a> Default for DirectoryConfig<'a> {
fn default() -> Self {
DirectoryConfig {
truncation_length: 3,
truncate_to_repo: true,
Expand Down
6 changes: 3 additions & 3 deletions src/configs/docker_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -14,8 +14,8 @@ pub struct DockerContextConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for DockerContextConfig<'a> {
fn new() -> Self {
impl<'a> Default for DockerContextConfig<'a> {
fn default() -> Self {
DockerContextConfig {
symbol: "🐳 ",
style: "blue bold",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/dotnet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -14,8 +14,8 @@ pub struct DotnetConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for DotnetConfig<'a> {
fn new() -> Self {
impl<'a> Default for DotnetConfig<'a> {
fn default() -> Self {
DotnetConfig {
format: "[$symbol($version )(🎯 $tfm )]($style)",
symbol: "•NET ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/elixir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct ElixirConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for ElixirConfig<'a> {
fn new() -> Self {
impl<'a> Default for ElixirConfig<'a> {
fn default() -> Self {
ElixirConfig {
format: "via [$symbol($version \\(OTP $otp_version\\) )]($style)",
symbol: "💧 ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/elm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct ElmConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for ElmConfig<'a> {
fn new() -> Self {
impl<'a> Default for ElmConfig<'a> {
fn default() -> Self {
ElmConfig {
format: "via [$symbol($version )]($style)",
symbol: "🌳 ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/env_var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -12,8 +12,8 @@ pub struct EnvVarConfig<'a> {
pub disabled: bool,
}

impl<'a> RootModuleConfig<'a> for EnvVarConfig<'a> {
fn new() -> Self {
impl<'a> Default for EnvVarConfig<'a> {
fn default() -> Self {
EnvVarConfig {
symbol: "",
style: "black bold dimmed",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/erlang.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct ErlangConfig<'a> {
pub detect_folders: Vec<&'a str>,
}

impl<'a> RootModuleConfig<'a> for ErlangConfig<'a> {
fn new() -> Self {
impl<'a> Default for ErlangConfig<'a> {
fn default() -> Self {
ErlangConfig {
format: "via [$symbol($version )]($style)",
symbol: " ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/gcloud.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;
use starship_module_config_derive::ModuleConfig;
use std::collections::HashMap;

Expand All @@ -11,8 +11,8 @@ pub struct GcloudConfig<'a> {
pub region_aliases: HashMap<String, &'a str>,
}

impl<'a> RootModuleConfig<'a> for GcloudConfig<'a> {
fn new() -> Self {
impl<'a> Default for GcloudConfig<'a> {
fn default() -> Self {
GcloudConfig {
format: "on [$symbol$account(\\($region\\))]($style) ",
symbol: "☁️ ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/git_branch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -14,8 +14,8 @@ pub struct GitBranchConfig<'a> {
pub disabled: bool,
}

impl<'a> RootModuleConfig<'a> for GitBranchConfig<'a> {
fn new() -> Self {
impl<'a> Default for GitBranchConfig<'a> {
fn default() -> Self {
GitBranchConfig {
format: "on [$symbol$branch]($style)(:[$remote]($style)) ",
symbol: " ",
Expand Down
6 changes: 3 additions & 3 deletions src/configs/git_commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -13,8 +13,8 @@ pub struct GitCommitConfig<'a> {
pub tag_disabled: bool,
}

impl<'a> RootModuleConfig<'a> for GitCommitConfig<'a> {
fn new() -> Self {
impl<'a> Default for GitCommitConfig<'a> {
fn default() -> Self {
GitCommitConfig {
// be consistent with git by default, which has DEFAULT_ABBREV set to 7
commit_hash_length: 7,
Expand Down
6 changes: 3 additions & 3 deletions src/configs/git_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use crate::config::ModuleConfig;

use starship_module_config_derive::ModuleConfig;

Expand All @@ -16,8 +16,8 @@ pub struct GitStateConfig<'a> {
pub disabled: bool,
}

impl<'a> RootModuleConfig<'a> for GitStateConfig<'a> {
fn new() -> Self {
impl<'a> Default for GitStateConfig<'a> {
fn default() -> Self {
GitStateConfig {
rebase: "REBASING",
merge: "MERGING",
Expand Down
Loading

0 comments on commit 052b56f

Please sign in to comment.