use crate::error::GivError;
use crate::uuid::format::UuidFormat;
use crate::uuid::output::UuidOutput;
use crate::uuid::version::UuidVersion;
use uuid::Uuid;
pub fn generate_uuid(
version: Option<UuidVersion>,
format: Option<UuidFormat>,
uppercase: bool,
) -> Result<UuidOutput, GivError> {
let version = version.unwrap_or_else(UuidVersion::default);
let uuid = match version {
UuidVersion::V4 => Uuid::new_v4(),
UuidVersion::V7 => Uuid::now_v7(),
};
let format = format.unwrap_or_else(UuidFormat::default);
let formatted = format_uuid(&uuid, format, uppercase);
Ok(UuidOutput {
uuid: formatted,
version: version.as_str().to_string(),
format: format.as_str().to_string(),
uppercase,
})
}
fn format_uuid(uuid: &Uuid, format: UuidFormat, uppercase: bool) -> String {
let base = match format {
UuidFormat::Standard => uuid.hyphenated().to_string(),
UuidFormat::Simple => uuid.simple().to_string(),
UuidFormat::Braced => uuid.braced().to_string(),
UuidFormat::URN => uuid.urn().to_string(),
UuidFormat::Hex => format!("0x{}", uuid.simple()),
};
if uppercase { base.to_uppercase() } else { base }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_uuid_default() {
let output = generate_uuid(None, None, false).unwrap();
assert_eq!(output.uuid.len(), 36);
assert_eq!(output.uuid.chars().nth(8).unwrap(), '-');
assert_eq!(output.uuid.chars().nth(13).unwrap(), '-');
assert_eq!(output.uuid.chars().nth(18).unwrap(), '-');
assert_eq!(output.uuid.chars().nth(23).unwrap(), '-');
assert_eq!(output.version, "v7");
assert_eq!(output.format, "standard");
assert!(!output.uppercase);
}
#[test]
fn test_generate_uuid_v4() {
let output = generate_uuid(Some(UuidVersion::V4), None, false).unwrap();
assert_eq!(output.version, "v4");
assert_eq!(output.uuid.len(), 36);
}
#[test]
fn test_uuid_uniqueness() {
let uuid1 = generate_uuid(None, None, false).unwrap();
let uuid2 = generate_uuid(None, None, false).unwrap();
assert_ne!(uuid1.uuid, uuid2.uuid);
}
#[test]
fn test_uuid_simple_format() {
let output = generate_uuid(None, Some(UuidFormat::Simple), false).unwrap();
assert_eq!(output.uuid.len(), 32);
assert!(!output.uuid.contains('-'));
assert_eq!(output.format, "simple");
}
#[test]
fn test_uuid_braced_format() {
let output = generate_uuid(None, Some(UuidFormat::Braced), false).unwrap();
assert_eq!(output.uuid.len(), 38); assert!(output.uuid.starts_with('{'));
assert!(output.uuid.ends_with('}'));
assert_eq!(output.format, "braced");
}
#[test]
fn test_uuid_urn_format() {
let output = generate_uuid(None, Some(UuidFormat::URN), false).unwrap();
assert!(output.uuid.starts_with("urn:uuid:"));
assert_eq!(output.format, "urn");
}
#[test]
fn test_uuid_hex_format() {
let output = generate_uuid(None, Some(UuidFormat::Hex), false).unwrap();
assert!(output.uuid.starts_with("0x"));
assert_eq!(output.uuid.len(), 34); assert_eq!(output.format, "hex");
}
#[test]
fn test_uuid_uppercase() {
let output = generate_uuid(None, None, true).unwrap();
assert!(output.uppercase);
assert!(output.uuid.chars().any(|c| c.is_ascii_uppercase()));
}
#[test]
fn test_uuid_format_parseable() {
let output = generate_uuid(None, Some(UuidFormat::Standard), false).unwrap();
let parsed = Uuid::parse_str(&output.uuid);
assert!(parsed.is_ok());
}
#[test]
fn test_uuid_combined_options() {
let output = generate_uuid(Some(UuidVersion::V4), Some(UuidFormat::Simple), true).unwrap();
assert_eq!(output.version, "v4");
assert_eq!(output.format, "simple");
assert!(output.uppercase);
assert_eq!(output.uuid.len(), 32);
assert!(!output.uuid.contains('-'));
}
}