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

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

    Some(module)
}

fn get_daml_sdk_version(context: &Context) -> Option<String> {
    context
        .get_env(DAML_SDK_VERSION_ENV)
        .or_else(|| read_sdk_version_from_daml_yaml(context))
}

fn read_sdk_version_from_daml_yaml(context: &Context) -> Option<String> {
    let file_contents = context.read_file_from_pwd(DAML_YAML)?;
    let daml_yaml = yaml_rust::YamlLoader::load_from_str(&file_contents).ok()?;
    let sdk_version = daml_yaml.first()?[DAML_SDK_VERSION].as_str()?;
    Some(sdk_version.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::ModuleRenderer;
    use ansi_term::Color;
    use std::fs::File;
    use std::io::{Result, Write};

    #[test]
    fn folder_without_daml_yaml() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let actual = ModuleRenderer::new("daml").path(dir.path()).collect();
        let expected = None;
        assert_eq!(expected, actual);
        dir.close()
    }

    #[test]
    fn folder_with_daml_yaml() -> Result<()> {
        let dir = tempfile::tempdir()?;
        File::create(dir.path().join(DAML_YAML))?.write_all(b"sdk-version: 2.2.0\n")?;
        let actual = ModuleRenderer::new("daml").path(dir.path()).collect();
        let expected = Some(format!("via {}", Color::Cyan.bold().paint("Λ v2.2.0 ")));
        assert_eq!(expected, actual);
        dir.close()
    }


Review comment (suggestion):
It looks like it's safe to do with this type, but please use `get` as `Index` may panic. Perhaps also add a test covering missing `DAML_SDK_VERSION`.

Code after:
                _ => None,
            })
            .parse(None, Some(context))
    });

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

    Some(module)
}

fn get_daml_sdk_version(context: &Context) -> Option<String> {
    context
        .get_env(DAML_SDK_VERSION_ENV)
        .or_else(|| read_sdk_version_from_daml_yaml(context))
}

fn read_sdk_version_from_daml_yaml(context: &Context) -> Option<String> {
    let file_contents = context.read_file_from_pwd(DAML_YAML)?;
    let daml_yaml = yaml_rust::YamlLoader::load_from_str(&file_contents).ok()?;
    let sdk_version = daml_yaml.first()?[DAML_SDK_VERSION].as_str()?;
    Some(sdk_version.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::ModuleRenderer;
    use ansi_term::Color;
    use std::fs::File;
    use std::io::{Result, Write};

    #[test]
    fn folder_without_daml_yaml() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let actual = ModuleRenderer::new("daml").path(dir.path()).collect();
        let expected = None;
        assert_eq!(expected, actual);
        dir.close()
    }

    #[test]
    fn folder_with_daml_yaml() -> Result<()> {
        let dir = tempfile::tempdir()?;
        File::create(dir.path().join(DAML_YAML))?.write_all(b"sdk-version: 2.2.0\n")?;
        let actual = ModuleRenderer::new("daml").path(dir.path()).collect();