Repository: starship/starship
File: src/print.rs (Rust)

Code before:

    prompt_order
}

/// Load the correct formatter for the context (ie left prompt or right prompt)
/// and the list of all modules used in a format string
fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>, BTreeSet<String>) {
    let config = &context.root_config;
    let lformatter = StringFormatter::new(&config.format);
    let rformatter = StringFormatter::new(&config.right_format);
    let cformatter = StringFormatter::new(&config.continuation_prompt);
    let (pformatter, profile_name) = match &context.target {
        Target::Profile(name) => (
            match config.profiles.get(name) {
                Some(format) => StringFormatter::new(format),
                _ => Err(StringFormatterError::Custom("Invalid Profile".to_string())),
            },
            Some(name),
        ),
        _ => (StringFormatter::new(""), None),
    };
    if lformatter.is_err() {
        log::error!("Error parsing `format`")
    }
    if rformatter.is_err() {
        log::error!("Error parsing `right_format`")
    }
    if cformatter.is_err() {
        log::error!("Error parsing `continuation_prompt`")
    }
    if pformatter.is_err() {
        if let Some(profile) = profile_name {
            log::error!("Error parsing profile `{}`", profile)
        }
    }
    match (lformatter, rformatter, cformatter, pformatter) {
        (Ok(lf), Ok(rf), Ok(cf), Ok(pf)) => {
            let mut modules: BTreeSet<String> = BTreeSet::new();
            if context.target != Target::Continuation {
                modules.extend(lf.get_variables());
                modules.extend(rf.get_variables());
            }
            match context.target {
                Target::Main => (lf, modules),
                Target::Right => (rf, modules),
                Target::Continuation => (cf, modules),
                Target::Profile(_) => (pf, modules),
            }
        }
        _ => (StringFormatter::raw(">"), BTreeSet::new()),
    }

Review comment (refactor):
If this isn't too difficult to refactor, I would prefer If only the required targets get initialized like in the previous version of your code.

Code after:
        Target::Right => (
            StringFormatter::new(&config.right_format),
            "right_format".to_string(),
        ),
        Target::Continuation => (
            StringFormatter::new(&config.continuation_prompt),
            "continuation_prompt".to_string(),
        ),
        Target::Profile(name) => (
            match config.profiles.get(name) {
                Some(format) => StringFormatter::new(format),
                _ => Err(StringFormatterError::Custom("Invalid Profile".to_string())),
            },
            format!("profile: {}", &name),
        ),
    };

    let rformatter = StringFormatter::new(&config.right_format);

    if formatter.is_err() {
        log::error!("Error parsing `{}`", config_param);
    }
    if rformatter.is_err() {
        log::error!("Error parsing `right_format`")
    }

    match (formatter, rformatter) {
        (Ok(lf), Ok(rf)) => {
            let mut modules: BTreeSet<String> = BTreeSet::new();
            if context.target != Target::Continuation {
                modules.extend(lf.get_variables());
                modules.extend(rf.get_variables());
            }
            (lf, modules)
        }
        _ => (StringFormatter::raw(">"), BTreeSet::new()),
    }
}

#[cfg(feature = "config-schema")]
pub fn print_schema() {
    let schema = schemars::schema_for!(crate::configs::FullConfig);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

#[derive(Clone, Debug)]
pub struct Preset(pub &'static str);

impl ValueEnum for Preset {
    fn value_variants<'a>() -> &'a [Self] {
        shadow::get_preset_list()