Repository: nushell/nushell
File: crates/nu-command/src/random/uuid.rs (Rust)

Code before:
            return Err(ShellError::MissingParameter {
                param_name: "namespace".to_string(),
                span,
            });
        }
    };

    let name = match name {
        Some(n) => n,
        None => {
            return Err(ShellError::MissingParameter {
                param_name: "name".to_string(),
                span,
            });
        }
    };

    let namespace = match namespace_str.to_lowercase().as_str() {
        "dns" => Uuid::NAMESPACE_DNS,
        "url" => Uuid::NAMESPACE_URL,
        "oid" => Uuid::NAMESPACE_OID,
        "x500" => Uuid::NAMESPACE_X500,
        _ => match Uuid::parse_str(&namespace_str) {
            Ok(uuid) => uuid,
            Err(_) => {
                return Err(ShellError::GenericError {
                        error: format!("Invalid namespace: {}", namespace_str),
                        msg: "Namespace must be one of: dns, url, oid, x500, or a valid UUID string".to_string(),
                        span: Some(span),
                        help: Some("Use one of the predefined namespaces (dns, url, oid, x500) or a valid UUID string".to_string()),
                        inner: Vec::new(),
                    });
            }
        },
    };

    Ok((namespace, name))
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(SubCommand {})
    }
}


Review comment (bug):
There might be a more accurate error: `IncorrectValue` maybe?


Code after:
        1 => {
            if call
                .get_flag::<Option<String>>(engine_state, stack, "namespace")?
                .is_some()
            {
                return Err(ShellError::IncompatibleParametersSingle {
                    msg: "version 1 uuid does not take namespace as a parameter".to_string(),
                    span,
                });
            }
            if call
                .get_flag::<Option<String>>(engine_state, stack, "name")?
                .is_some()
            {
                return Err(ShellError::IncompatibleParametersSingle {
                    msg: "version 1 uuid does not take name as a parameter".to_string(),
                    span,
                });
            }
        }
        3 | 5 => {
            if call
                .get_flag::<Option<String>>(engine_state, stack, "mac")?
                .is_some()
            {
                return Err(ShellError::IncompatibleParametersSingle {
                    msg: "version 3 and 5 uuids do not take mac as a parameter".to_string(),
                    span,
                });
            }
        }
        v => {
            if v != 4 && v != 7 {
                return Err(ShellError::IncorrectValue {
                    msg: format!(
                        "Unsupported UUID version: {}. Supported versions are 1, 3, 4, 5, and 7.",
                        v
                    ),
                    val_span: span,
                    call_span: span,
                });
            }
            if call
                .get_flag::<Option<String>>(engine_state, stack, "mac")?
                .is_some()
            {
                return Err(ShellError::IncompatibleParametersSingle {
                    msg: format!("version {} uuid does not take mac as a parameter", v),
                    span,
                });
            }