Skip to content

Commit

Permalink
use alternate flag to swap between simple and hyphenated
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 1, 2021
1 parent 60b5317 commit 5edf9ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
18 changes: 12 additions & 6 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@ impl fmt::Display for Variant {

impl fmt::LowerHex for Uuid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(&self.to_hyphenated_ref(), f)
if f.alternate() {
fmt::LowerHex::fmt(&self.to_simple_ref(), f)
} else {
fmt::LowerHex::fmt(&self.to_hyphenated_ref(), f)
}
}
}

impl fmt::UpperHex for Uuid {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::UpperHex::fmt(&self.to_hyphenated_ref(), f)
if f.alternate() {
fmt::UpperHex::fmt(&self.to_simple_ref(), f)
} else {
fmt::UpperHex::fmt(&self.to_hyphenated_ref(), f)
}
}
}

Expand Down Expand Up @@ -974,15 +982,13 @@ macro_rules! impl_fmt_traits {

impl<$($a),*> fmt::LowerHex for $T<$($a),*> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: Self doesn't work https://github.com/rust-lang/rust/issues/52808
f.write_str(self.encode_lower(&mut [0; $T::LENGTH]))
f.write_str(self.encode_lower(&mut [0; Self::LENGTH]))
}
}

impl<$($a),*> fmt::UpperHex for $T<$($a),*> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: Self doesn't work https://github.com/rust-lang/rust/issues/52808
f.write_str(self.encode_upper(&mut [0; $T::LENGTH]))
f.write_str(self.encode_upper(&mut [0; Self::LENGTH]))
}
}

Expand Down
33 changes: 19 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,19 +818,6 @@ mod tests {
|| c == '-');
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_uuid_upperhex() {
use crate::std::fmt::Write;

let mut buffer = String::new();
let uuid = new();

check!(buffer, "{:X}", uuid, 36, |c| c.is_uppercase()
|| c.is_digit(10)
|| c == '-');
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_nil() {
Expand Down Expand Up @@ -936,25 +923,43 @@ mod tests {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert!(buf.len() == $len);
assert_eq!($len, buf.len());
assert!($buf.chars().all($cond), "{}", $buf);
};
}

check!(buf, "{:x}", u, 36, |c| c.is_lowercase()
|| c.is_digit(10)
|| c == '-');
check!(buf, "{:X}", u, 36, |c| c.is_uppercase()
|| c.is_digit(10)
|| c == '-');
check!(buf, "{:#x}", u, 32, |c| c.is_lowercase()
|| c.is_digit(10));
check!(buf, "{:#X}", u, 32, |c| c.is_uppercase()
|| c.is_digit(10));

check!(buf, "{:X}", u.to_hyphenated(), 36, |c| c.is_uppercase()
|| c.is_digit(10)
|| c == '-');
check!(buf, "{:X}", u.to_simple(), 32, |c| c.is_uppercase()
|| c.is_digit(10));
check!(buf, "{:#X}", u.to_hyphenated(), 36, |c| c.is_uppercase()
|| c.is_digit(10)
|| c == '-');
check!(buf, "{:#X}", u.to_simple(), 32, |c| c.is_uppercase()
|| c.is_digit(10));

check!(buf, "{:x}", u.to_hyphenated(), 36, |c| c.is_lowercase()
|| c.is_digit(10)
|| c == '-');
check!(buf, "{:x}", u.to_simple(), 32, |c| c.is_lowercase()
|| c.is_digit(10));
check!(buf, "{:#x}", u.to_hyphenated(), 36, |c| c.is_lowercase()
|| c.is_digit(10)
|| c == '-');
check!(buf, "{:#x}", u.to_simple(), 32, |c| c.is_lowercase()
|| c.is_digit(10));
}

#[test]
Expand Down

0 comments on commit 5edf9ca

Please sign in to comment.