/// Describes how a TOML integer should be formatted. /// /// # Example /// /// ```rust /// # #[cfg(feature = "alloc")] { /// # use toml_writer::ToTomlValue as _; /// let format = toml_writer::TomlIntegerFormat::new().as_hex_lower(); /// let number = 10; /// let number = format.format(number).unwrap_or(toml_writer::TomlInteger::new(number)); /// let number = number.to_toml_value(); /// assert_eq!(number, "0xa"); /// # } /// ``` #[derive(Copy, Clone, Debug)] pubstruct TomlIntegerFormat {
radix: Radix,
}
impl TomlIntegerFormat { /// Creates a new integer format (decimal). pubfn new() -> Self { Self {
radix: Radix::Decimal,
}
}
/// Sets the format to decimal. pubfn as_decimal(mutself) -> Self { self.radix = Radix::Decimal; self
}
/// Sets the format to hexadecimal with all characters in uppercase. pubfn as_hex_upper(mutself) -> Self { self.radix = Radix::Hexadecimal {
case: HexCase::Upper,
}; self
}
/// Sets the format to hexadecimal with all characters in lowercase. pubfn as_hex_lower(mutself) -> Self { self.radix = Radix::Hexadecimal {
case: HexCase::Lower,
}; self
}
/// Sets the format to octal. pubfn as_octal(mutself) -> Self { self.radix = Radix::Octal; self
}
/// Sets the format to binary. pubfn as_binary(mutself) -> Self { self.radix = Radix::Binary; self
}
/// Formats `value` as a TOML integer. /// /// Returns `None` if the value cannot be formatted /// (e.g. value is negative and the radix is not decimal). pubfn format<N: PartialOrd<i32>>(self, value: N) -> Option<TomlInteger<N>> where
TomlInteger<N>: crate::WriteTomlValue,
{ matchself.radix {
Radix::Decimal => (),
Radix::Hexadecimal { .. } | Radix::Octal | Radix::Binary => { if value < 0 { return None;
}
}
}
/// Helper struct for formatting TOML integers. /// /// This may be constructed by calling [`TomlIntegerFormat::format()`]. #[derive(Copy, Clone, Debug)] pubstruct TomlInteger<N> {
value: N,
format: TomlIntegerFormat,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.