Repository: nushell/nushell
File: crates/nu-engine/src/env.rs (Rust)

Code before:
        Err(err)
    } else {
        Ok(())
    }
}

/// Translate one environment variable from Value to String
///
/// Returns Ok(None) if the env var is not
pub fn env_to_string(
    env_name: &str,
    value: &Value,
    engine_state: &EngineState,
    stack: &Stack,
) -> Result<String, ShellError> {
    match get_converted_value(engine_state, stack, env_name, value, "to_string") {
        Ok(v) => Ok(v.coerce_into_string()?),
        Err(ConversionError::ShellError(e)) => Err(e),
        Err(ConversionError::CellPathError) => match value.coerce_string() {
            Ok(s) => Ok(s),
            Err(_) => {
                if env_name.to_lowercase() == "path" {
                    // Try to convert PATH/Path list to a string
                    match value {
                        Value::List { vals, .. } => {
                            let paths = vals
                                .iter()
                                .map(|s| {
                                    let path = Value::coerce_str(s);
                                    let expanded_path =
                                        nu_path::expand_tilde(path.clone()?.as_ref());
                                    Ok::<String, ShellError>(
                                        expanded_path.to_string_lossy().into_owned(),
                                    )
                                })
                                .collect::<Result<Vec<_>, _>>()?;

                            match std::env::join_paths(
                                paths.iter().map(AsRef::<std::ffi::OsStr>::as_ref),
                            ) {
                                Ok(p) => Ok(p.to_string_lossy().to_string()),
                                Err(_) => Err(ShellError::EnvVarNotAString {
                                    envvar_name: env_name.to_string(),
                                    span: value.span(),
                                }),
                            }
                        }
                        _ => Err(ShellError::EnvVarNotAString {
                            envvar_name: env_name.to_string(),
                            span: value.span(),
                        }),

Review comment (refactor):
I'm not one to nitpick rust code really but I was playing around with simplifying this section and came up with this. You be the judge if you think it works the way it's supposed to and is easier to understand.
```rust
                            let paths: Vec<String> = vals
                                .iter()
                                .filter_map(|v| v.coerce_str().ok())
                                .map(|s| nu_path::expand_tilde(&*s).to_string_lossy().into_owned())
                                .collect();

                            std::env::join_paths(paths.iter().map(AsRef::<str>::as_ref))
                                .map(|p| p.to_string_lossy().to_string())
                                .map_err(|_| ShellError::EnvVarNotAString {
                                    envvar_name: env_name.to_string(),
                                    span: value.span(),
                                })
```

Code after:
        Err(err)
    } else {
        Ok(())
    }
}

/// Translate one environment variable from Value to String
///
/// Returns Ok(None) if the env var is not
pub fn env_to_string(
    env_name: &str,
    value: &Value,
    engine_state: &EngineState,
    stack: &Stack,
) -> Result<String, ShellError> {
    match get_converted_value(engine_state, stack, env_name, value, "to_string") {
        Ok(v) => Ok(v.coerce_into_string()?),
        Err(ConversionError::ShellError(e)) => Err(e),
        Err(ConversionError::CellPathError) => match value.coerce_string() {
            Ok(s) => Ok(s),
            Err(_) => {
                if env_name.to_lowercase() == "path" {
                    // Try to convert PATH/Path list to a string
                    match value {
                        Value::List { vals, .. } => {
                            let paths: Vec<String> = vals
                                .iter()
                                .filter_map(|v| v.coerce_str().ok())
                                .map(|s| nu_path::expand_tilde(&*s).to_string_lossy().into_owned())
                                .collect();

                            std::env::join_paths(paths.iter().map(AsRef::<str>::as_ref))
                                .map(|p| p.to_string_lossy().to_string())
                                .map_err(|_| ShellError::EnvVarNotAString {
                                    envvar_name: env_name.to_string(),
                                    span: value.span(),
                                })
                        }
                        _ => Err(ShellError::EnvVarNotAString {
                            envvar_name: env_name.to_string(),
                            span: value.span(),
                        }),
                    }
                } else {
                    Err(ShellError::EnvVarNotAString {
                        envvar_name: env_name.to_string(),
                        span: value.span(),
                    })
                }
            }
        },