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

Code before:
        std::usize::MAX
    } else {
        config.truncation_length as usize
    };

    let repo = get_hg_repo(context).ok()?;

    let branch_name = &repo.bookmark.unwrap_or(repo.branch);
    let truncated_graphemes = get_graphemes(branch_name, len);
    let truncation_symbol = get_graphemes(config.truncation_symbol, 1);
    // The truncation symbol should only be added if we truncated
    let truncated_and_symbol = if len < graphemes_len(branch_name) {
        truncated_graphemes + truncation_symbol.as_str()
    } else {
        truncated_graphemes
    };
    let topic_graphemes = if let Some(topic) = &repo.topic {
        let truncated_topic_graphemes = get_graphemes(topic, len);
        if len < graphemes_len(topic) {
            truncated_topic_graphemes + truncation_symbol.as_str()
        } else {
            truncated_topic_graphemes
        }
    } else {
        String::from("")
    };

    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 {
                "branch" => Some(Ok(truncated_and_symbol.as_str())),
                "topic" => Some(Ok(topic_graphemes.as_str())),
                _ => None,
            })
            .parse(None, Some(context))
    });

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

Review comment (suggestion):
Since you are changing truncation quite a bit, please also try moving it to the `utils::truncate` functions, like the meson module.

Code after:
        String::from("")
    };

    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 {
                "branch" => Some(Ok(branch_graphemes.as_str())),
                "topic" => Some(Ok(topic_graphemes.as_str())),
                _ => None,
            })
            .parse(None, Some(context))
    });

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

    Some(module)
}

fn get_hg_repo_root<'a>(ctx: &'a Context) -> Result<&'a Path, Error> {
    let dir = ctx.current_dir.as_path();
    let dev_id = dir.device_id();
    for root_dir in dir.ancestors() {
        if dev_id != root_dir.device_id() {
            break;
        }
        if root_dir.join(".hg").is_dir() {
            return Ok(root_dir);
        }
    }
    Err(Error::new(ErrorKind::Other, "No .hg found!"))
}

fn get_hg_branch_name(hg_root: &Path) -> Result<String, Error> {
    match read_file(hg_root.join(".hg").join("branch")) {
        Ok(b) => Ok(b.trim().to_string()),
        Err(e) => Err(e),
    }