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

Code before:
use std::env;

use super::{Context, Module, ModuleConfig};

use crate::configs::kubernetes::KubernetesConfig;
use crate::formatter::StringFormatter;
use crate::utils;

#[derive(Default)]
struct KubeCtxComponents {
    user: Option<String>,
    namespace: Option<String>,
    cluster: Option<String>,
}

fn get_current_kube_context_name(document: &JsonOrYaml) -> Option<String> {
    match document {
        JsonOrYaml::Json(json) => json
            .get("current-context")
            .and_then(JsonValue::as_str)
            .filter(|s| !s.is_empty())
            .map(String::from),
        JsonOrYaml::Yaml(yaml) => yaml["current-context"]
            .as_str()
            .filter(|s| !s.is_empty())
            .map(String::from),
    }
}

fn get_kube_ctx_components(
    document: &JsonOrYaml,
    current_ctx_name: &str,
) -> Option<KubeCtxComponents> {
    match document {
        JsonOrYaml::Json(json) => json["contexts"]
            .as_array()?
            .iter()
            .find(|ctx| ctx["name"].as_str() == Some(current_ctx_name))
            .map(|ctx| KubeCtxComponents {
                user: ctx["context"]["user"]
                    .as_str()
                    .filter(|s| !s.is_empty())
                    .map(String::from),
                namespace: ctx["context"]["namespace"]
                    .as_str()
                    .filter(|s| !s.is_empty())
                    .map(String::from),
                cluster: ctx["context"]["cluster"]
                    .as_str()
                    .filter(|s| !s.is_empty())
                    .map(String::from),

Review comment (performance):
This is also a great candidate for borrowing. Something along the lines of... (just as an example)

```suggestion
fn get_kube_ctx_components<'doc>(
    document: &'doc Document,
    current_ctx_name: &str,
) -> Option<KubeCtxComponents<'doc>> {
```

with something like

```rust
#[derive(Default)]
struct KubeCtxComponents<'doc> {
    user: Option<&'doc str>,
    namespace: Option<&'doc str>,
    cluster: Option<&'doc str>,
}
```

This:

- gets rid of clones (`String::from`)
- is unlikely to improve performance in meaningful ways, while complicating (== adding more constraints to) the code... but if this PR is about performance, why not give it a shot...
- might not work (depending on how this change affects other parts of the code)

Code after:
use std::env;

use super::{Context, Module, ModuleConfig};

use crate::configs::kubernetes::KubernetesConfig;
use crate::formatter::StringFormatter;
use crate::utils;

#[derive(Default)]
struct KubeCtxComponents {
    user: Option<String>,
    namespace: Option<String>,
    cluster: Option<String>,
}

fn get_current_kube_context_name<T: DataValue>(document: &T) -> Option<&str> {
    document
        .get("current-context")
        .and_then(DataValue::as_str)
        .filter(|s| !s.is_empty())
}

fn get_kube_ctx_components<T: DataValue>(
    document: &T,
    current_ctx_name: &str,
) -> Option<KubeCtxComponents> {
    document
        .get("contexts")?
        .as_array()?
        .iter()
        .find(|ctx| ctx.get("name").and_then(DataValue::as_str) == Some(current_ctx_name))
        .map(|ctx| KubeCtxComponents {
            user: ctx
                .get("context")
                .and_then(|v| v.get("user"))
                .and_then(DataValue::as_str)
                .map(String::from),
            namespace: ctx
                .get("context")
                .and_then(|v| v.get("namespace"))
                .and_then(DataValue::as_str)
                .map(String::from),
            cluster: ctx
                .get("context")
                .and_then(|v| v.get("cluster"))
                .and_then(DataValue::as_str)
                .map(String::from),
        })
}

fn get_aliased_name<'a>(