pub fn wrap_xuid(player_id: &str) -> String {
if is_wrapped(player_id) {
player_id.to_string()
} else {
format!("xuid({player_id})")
}
}
pub fn unwrap_xuid(player_id: &str) -> String {
match wrapped_digits(player_id) {
Some(digits) => digits.to_string(),
None => player_id.to_string(),
}
}
fn is_wrapped(player_id: &str) -> bool {
wrapped_digits(player_id).is_some()
}
fn wrapped_digits(player_id: &str) -> Option<&str> {
let open = player_id.find('(')?;
if !player_id.ends_with(')') {
return None;
}
let prefix = &player_id[..open];
if prefix.is_empty()
|| !prefix
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_')
{
return None;
}
let digits = &player_id[open + 1..player_id.len() - 1];
if !digits.is_empty() && digits.chars().all(|c| c.is_ascii_digit()) {
Some(digits)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wraps_raw_xuid() {
assert_eq!(wrap_xuid("2535405290989773"), "xuid(2535405290989773)");
}
#[test]
fn wrap_is_idempotent() {
assert_eq!(wrap_xuid("xuid(123)"), "xuid(123)");
}
#[test]
fn unwraps_wrapped_xuid() {
assert_eq!(unwrap_xuid("xuid(123)"), "123");
}
#[test]
fn unwrap_passes_through_gamertag() {
assert_eq!(unwrap_xuid("Some Gamertag"), "Some Gamertag");
}
#[test]
fn unwrap_passes_through_raw_xuid() {
assert_eq!(unwrap_xuid("123"), "123");
}
#[test]
fn wrap_passes_through_gamertag_with_parens() {
assert_eq!(wrap_xuid("gt(abc)"), "xuid(gt(abc))");
}
}