Skip to content

Commit

Permalink
Add Fmt and FmtAlternative wrapper types with fmt and fmt_alt
Browse files Browse the repository at this point in the history
… helper functions (#19)
  • Loading branch information
tyranron authored Jan 9, 2025
1 parent 55e336f commit 49d91c1
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ pub enum EncodeError {
BufferTooSmall,
}

impl core::fmt::Display for DecodeError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DecodeError::ArithmeticOverflow => {
f.write_str("Decoded number cannot fit into a `u128`")
}
DecodeError::EmptyInput => f.write_str("Encoded input is an empty string"),
DecodeError::InvalidBase62Byte(ch, idx) => {
use core::fmt::Write;
use fmt::Write;
f.write_str("Encoded input contains the invalid byte b'")?;
for char_in_escape in core::ascii::escape_default(ch) {
f.write_char(char::from(char_in_escape))?;
Expand Down Expand Up @@ -92,8 +92,8 @@ impl core::error::Error for EncodeError {}
impl std::error::Error for EncodeError {}

// You'll also need to implement std::fmt::Display for EncodeError since it's required for Error
impl core::fmt::Display for EncodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EncodeError::BufferTooSmall => write!(f, "Buffer too small to encode the number"),
}
Expand All @@ -104,7 +104,6 @@ impl core::fmt::Display for EncodeError {
///
/// # Example
/// ```rust
/// # use core::fmt::Write;
/// let mut output = String::new();
/// base62::encode_fmt(1337_u32, &mut output).unwrap();
/// assert_eq!(output, "LZ");
Expand All @@ -125,12 +124,32 @@ pub fn encode_fmt<T: Into<u128>, W: fmt::Write + ?Sized>(num: T, f: &mut W) -> f
}
}

/// Wraps a number to [`Display`] it via [`encode_fmt()`].
///
/// # Example
/// ```rust
/// assert_eq!(format!("{}", base62::fmt(1337_u32)), "LZ");
/// assert_eq!(base62::fmt(1337_u32).to_string(), "LZ");
/// ```
///
/// [`Display`]: fmt::Display
pub fn fmt<T: Into<u128>>(num: T) -> impl fmt::Display {
Fmt(num.into())
}

struct Fmt(u128);

impl fmt::Display for Fmt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
encode_fmt(self.0, f)
}
}

/// Writes the base62 representation of a number using the alternative alphabet (0 to 9, then a to z, then A to Z)
/// to any fmt::Write destination.
///
/// # Example
/// ```rust
/// # use core::fmt::Write;
/// let mut output = String::new();
/// base62::encode_alternative_fmt(1337_u32, &mut output).unwrap();
/// assert_eq!(output, "lz");
Expand All @@ -154,6 +173,27 @@ pub fn encode_alternative_fmt<T: Into<u128>, W: fmt::Write + ?Sized>(
}
}

/// Wraps a number to [`Display`] it via [`encode_alternative_fmt()`].
///
/// # Example
/// ```rust
/// assert_eq!(format!("{}", base62::fmt_alt(1337_u32)), "lz");
/// assert_eq!(base62::fmt_alt(1337_u32).to_string(), "lz");
/// ```
///
/// [`Display`]: fmt::Display
pub fn fmt_alt<T: Into<u128>>(num: T) -> impl fmt::Display {
FmtAlternative(num.into())
}

struct FmtAlternative(u128);

impl fmt::Display for FmtAlternative {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
encode_alternative_fmt(self.0, f)
}
}

/// Writes the base62 representation of a number using the standard alphabet to any io::Write destination.
///
/// This function is only available when the `std` feature is enabled.
Expand Down

0 comments on commit 49d91c1

Please sign in to comment.