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

Code before:
    pub fn get_config_path_os(&self) -> OsString {
        get_config_path_os(&self.env)
    }
}

impl Default for Context<'_> {
    fn default() -> Self {
        Context::new(Default::default(), Target::Main)
    }
}

fn home_dir(env: &Env) -> Option<PathBuf> {
    if cfg!(test) {
        if let Some(home) = env.get_env("HOME") {
            return Some(PathBuf::from(home));
        }
    }
    utils::home_dir()
}

fn get_config_path_os(env: &Env) -> OsString {
    if let Some(config_path) = env.get_env_os("STARSHIP_CONFIG") {
        return config_path;
    }
    home_dir(env)
        .expect("couldn't find home directory")
        .join(".config")
        .join("starship.toml")
        .into()
}

#[derive(Debug)]
pub struct DirContents {
    // HashSet of all files, no folders, relative to the base directory given at construction.
    files: HashSet<PathBuf>,
    // HashSet of all file names, e.g. the last section without any folders, as strings.
    file_names: HashSet<String>,
    // HashSet of all folders, relative to the base directory given at construction.
    folders: HashSet<PathBuf>,
    // HashSet of all extensions found, without dots, e.g. "js" instead of ".js".
    extensions: HashSet<String>,
}

impl DirContents {
    #[cfg(test)]
    fn from_path(base: &Path) -> Result<Self, std::io::Error> {
        Self::from_path_with_timeout(base, Duration::from_secs(30))
    }

    fn from_path_with_timeout(base: &Path, timeout: Duration) -> Result<Self, std::io::Error> {
        let start = Instant::now();

Review comment (suggestion):
Since, the aim is to reduce panics, perhaps also get red of this one by returning `Option<OsString>` instead.

Code after:
        if !self.try_begin_scan()?.set_files(&[file_name]).is_match() {
            log::debug!(
                "Not attempting to read {file_name} because, it was not found during scan."
            );
            return None;
        }

        read_file(self.current_dir.join(file_name)).ok()
    }

    pub fn get_config_path_os(&self) -> Option<OsString> {
        get_config_path_os(&self.env)
    }
}

impl Default for Context<'_> {
    fn default() -> Self {
        Context::new(Default::default(), Target::Main)
    }
}

fn home_dir(env: &Env) -> Option<PathBuf> {
    if cfg!(test) {
        if let Some(home) = env.get_env("HOME") {
            return Some(PathBuf::from(home));
        }
    }
    utils::home_dir()
}

fn get_config_path_os(env: &Env) -> Option<OsString> {
    if let Some(config_path) = env.get_env_os("STARSHIP_CONFIG") {
        return Some(config_path);
    }
    Some(home_dir(env)?.join(".config").join("starship.toml").into())
}

#[derive(Debug)]
pub struct DirContents {
    // HashSet of all files, no folders, relative to the base directory given at construction.
    files: HashSet<PathBuf>,
    // HashSet of all file names, e.g. the last section without any folders, as strings.
    file_names: HashSet<String>,
    // HashSet of all folders, relative to the base directory given at construction.
    folders: HashSet<PathBuf>,
    // HashSet of all extensions found, without dots, e.g. "js" instead of ".js".
    extensions: HashSet<String>,
}

impl DirContents {
    #[cfg(test)]