Repository: starship/starship
File: src/modules/gcloud.rs (Rust)

Code before:
        })
}

fn get_active_config(context: &Context, config_dir: &Path) -> Option<String> {
    context.get_env("CLOUDSDK_ACTIVE_CONFIG_NAME").or_else(|| {
        let path = config_dir.join("active_config");
        match utils::read_file(path) {
            Ok(data) => data.lines().next().map(String::from),
            Err(_) => None,
        }
    })
}

pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("gcloud");
    let config: GcloudConfig = GcloudConfig::try_load(module.config);

    if !(context.detect_env_vars(&config.detect_env_vars)) {
        return None;
    }

    let (config_name, config_path) = get_current_config(context)?;
    let gcloud_context = GcloudContext::new(&config_name, &config_path);
    let account: Lazy<Option<Account<'_>>, _> = Lazy::new(|| gcloud_context.get_account());

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_meta(|variable, _| match variable {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "account" => account
                    .map(|(account, _)| account)
                    .map(Cow::Borrowed)
                    .map(Ok),
                "domain" => account
                    .and_then(|(_, domain)| domain)
                    .map(Cow::Borrowed)
                    .map(Ok),
                "region" => gcloud_context
                    .get_region()
                    .map(|region| config.region_aliases.get(region).copied().unwrap_or(region))
                    .map(Cow::Borrowed)
                    .map(Ok),
                "project" => context
                    .get_env("CLOUDSDK_CORE_PROJECT")

Review comment (suggestion):
I think I would prefer putting this into `get_current_config`, but this is fine too.

Code after:
        })
}

fn get_active_config(context: &Context, config_dir: &Path) -> Option<String> {
    context.get_env("CLOUDSDK_ACTIVE_CONFIG_NAME").or_else(|| {
        let path = config_dir.join("active_config");
        match utils::read_file(path) {
            Ok(data) => data.lines().next().map(String::from),
            Err(_) => None,
        }
    })
}

pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("gcloud");
    let config: GcloudConfig = GcloudConfig::try_load(module.config);

    if !(context.detect_env_vars(&config.detect_env_vars)) {
        return None;
    }

    let (config_name, config_path) = get_current_config(context)?;

    if config_name == "NONE" {
        return None;
    }
    let gcloud_context = GcloudContext::new(&config_name, &config_path);
    let account: Lazy<Option<Account<'_>>, _> = Lazy::new(|| gcloud_context.get_account());

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_meta(|variable, _| match variable {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "account" => account
                    .map(|(account, _)| account)
                    .map(Cow::Borrowed)
                    .map(Ok),
                "domain" => account
                    .and_then(|(_, domain)| domain)
                    .map(Cow::Borrowed)
                    .map(Ok),
                "region" => gcloud_context
                    .get_region()
                    .map(|region| config.region_aliases.get(region).copied().unwrap_or(region))