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

Code before:
/// There should be no actual packets send over the wire.
///
/// Will display the ip if all of the following criteria are met:
///     - `localip.disabled` is false
///     - `localip.ssh_only` is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("localip");
    let config: LocalipConfig = LocalipConfig::try_load(module.config);

    // As we default to disabled=true, we have to check here after loading our config module,
    // before it was only checking against whatever is in the config starship.toml
    if config.disabled {
        return None;
    };

    let ssh_connection = context.get_env("SSH_CONNECTION");
    if config.ssh_only && ssh_connection.is_none() {
        return None;
    }

    let localip = match get_local_ipv4() {
        Ok(ip) => ip,
        Err(e) => {
            // ErrorKind::NetworkUnreachable is unstable
            if e.raw_os_error() == Some(101) {
                "NetworkUnreachable".to_string()
            } else {
                log::warn!("unable to determine local ipv4 address: {e}");
                return None;
            }
        }
    };

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "localipv4" => Some(Ok(&localip)),
                _ => None,
            })
            .parse(None, Some(context))
    });

    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `localip`:\n{}", error);
            return None;

Review comment (suggestion):
If this is Linux-specific, we should probably gate it behind `cfg!(target_os = "linux")`.

Code after:
/// There should be no actual packets send over the wire.
///
/// Will display the ip if all of the following criteria are met:
///     - `localip.disabled` is false
///     - `localip.ssh_only` is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("localip");
    let config: LocalipConfig = LocalipConfig::try_load(module.config);

    // As we default to disabled=true, we have to check here after loading our config module,
    // before it was only checking against whatever is in the config starship.toml
    if config.disabled {
        return None;
    };

    let ssh_connection = context.get_env("SSH_CONNECTION");
    if config.ssh_only && ssh_connection.is_none() {
        return None;
    }

    let localip = match get_local_ipv4() {
        Ok(ip) => ip,
        Err(e) => {
            // ErrorKind::NetworkUnreachable is unstable
            if cfg!(target_os = "linux") && e.raw_os_error() == Some(101) {
                "NetworkUnreachable".to_string()
            } else {
                log::warn!("unable to determine local ipv4 address: {e}");
                return None;
            }
        }
    };

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "localipv4" => Some(Ok(&localip)),
                _ => None,
            })
            .parse(None, Some(context))
    });

    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `localip`:\n{}", error);
            return None;