rust-lang/rust · #166259

fmt: Improve `Debug` for `FormattingOptions`

tgross35 · merged Nov 22, 20262 files · 142 + / 1 −
library/core/src/fmt/mod.rs68 + / 1 −
@@ -284,7 +284,7 @@ pub enum DebugAsHex { /// /// `FormattingOptions` is a [`Formatter`] without an attached [`Write`] trait. /// It is mainly used to construct `Formatter` instances.-#[derive(Copy, Clone, Debug, PartialEq, Eq)]+#[derive(Copy, Clone, PartialEq, Eq)] #[unstable(feature = "formatting_options", issue = "118117")] pub struct FormattingOptions {     /// Flags, with the following bit fields:@@ -535,6 +535,49 @@ impl FormattingOptions {     pub const fn create_formatter<'a>(self, write: &'a mut (dyn Write + 'a)) -> Formatter<'a> {         Formatter { options: self, buf: write }     }++    /// Recreate a format specifier like `-<04x` matching these options. Public only for testing.+    #[doc(hidden)]+    #[unstable(feature = "fmt_internals", issue = "none")]+    pub fn write_fmt_specifier(&self, f: &mut Formatter<'_>, str_escape: bool) -> Result {+        let fill = self.get_fill();+        if fill != ' ' {+            if str_escape {+                write!(f, "{}", fill.escape_debug())?;+            } else {+                write!(f, "{fill}")?;+            }+        }+        match self.get_align() {+            Some(Alignment::Left) => write!(f, "<")?,+            Some(Alignment::Right) => write!(f, ">")?,+            Some(Alignment::Center) => write!(f, "^")?,+            None => (),+        }+        match self.get_sign() {+            Some(Sign::Plus) => write!(f, "+")?,+            Some(Sign::Minus) => write!(f, "-")?,+            None => (),+        }+        if self.get_alternate() {+            write!(f, "#")?;+        }+        if self.get_sign_aware_zero_pad() {+            write!(f, "0")?;+        }+        if let Some(width) = self.get_width() {+            write!(f, "{width}")?;+        }+        if let Some(precision) = self.get_precision() {+            write!(f, ".{precision}")?;+        }+        match self.get_debug_as_hex() {+            Some(DebugAsHex::Lower) => write!(f, "x")?,+            Some(DebugAsHex::Upper) => write!(f, "X")?,+            None => (),+        }+        Ok(())+    } }  #[unstable(feature = "formatting_options", issue = "118117")]@@ -546,6 +589,30 @@ impl Default for FormattingOptions {     } } +#[unstable(feature = "formatting_options", issue = "118117")]+impl Debug for FormattingOptions {+    fn fmt(&self, f: &mut Formatter<'_>) -> Result {+        // `field_with` for optional values so we don't propagate `#` and turn a simple+        // `Some(1)` into three lines.+        f.debug_struct("FormattingOptions")+            .field_with("align", |f| write!(f, "{:?}", self.get_align()))+            .field("alternate", &self.get_alternate())+            .field_with("debug_as_hex", |f| write!(f, "{:?}", self.get_debug_as_hex()))+            .field("fill", &self.get_fill())+            .field_with("precision", |f| write!(f, "{:?}", self.get_precision()))+            .field_with("sign", |f| write!(f, "{:?}", self.get_sign()))+            .field("sign_aware_zero_pad", &self.get_sign_aware_zero_pad())+            .field_with("width", |f| write!(f, "{:?}", self.get_width()))+            .field_with("specifier", |f| {+                f.write_char('"')?;+                self.write_fmt_specifier(f, true)?;+                f.write_char('"')?;+                Ok(())+            })+            .finish()+    }+}+ /// Configuration for formatting. /// /// A `Formatter` represents various options related to formatting. Users do not
library/coretests/tests/fmt/mod.rs74 + / 0 −
@@ -2,6 +2,9 @@ mod builders; mod float; mod num; +use core::fmt;+use core::fmt::Write;+ #[test] fn test_lifetime() {     // Trigger all different forms of expansion,@@ -199,3 +202,74 @@ fn formatting_options_flags() {         }     } }++#[test]+fn test_options_as_specifier() {+    struct F;+    impl fmt::Display for F {+        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+            f.options().write_fmt_specifier(f, false)+        }+    }+    impl fmt::Debug for F {+        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+            fmt::Display::fmt(self, f)+        }+    }++    // LowerExp (arbitrary choice) prints in string format+    impl fmt::LowerExp for F {+        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+            f.write_char('"')?;+            f.options().write_fmt_specifier(f, true)?;+            f.write_char('"')?;+            Ok(())+        }+    }++    let f = F;++    // Default+    assert_eq!(format!("{f}"), "");++    // Width+    assert_eq!(format!("{f:1}"), "1");++    // Fill and align+    assert_eq!(format!("{f:<}"), "<");+    assert_eq!(format!("{f:^}"), "^");+    assert_eq!(format!("{f:>}"), ">");+    assert_eq!(format!("{f:<5}"), "<5");+    assert_eq!(format!("{f:^5}"), "^5");+    assert_eq!(format!("{f:>5}"), ">5");+    assert_eq!(format!("{f:x>5}"), "x>5");++    // Sign and alternates+    assert_eq!(format!("{f:+}"), "+");+    assert_eq!(format!("{f:-}"), "-");+    assert_eq!(format!("{f:#}"), "#");+    assert_eq!(format!("{f:0}"), "0");++    // Precision+    assert_eq!(format!("{f:.1}"), ".1");++    // Hex within other format types+    assert_eq!(format!("{f:x?}"), "x");+    assert_eq!(format!("{f:X?}"), "X");++    // Hodgepodge+    assert_eq!(format!("{f:#018}"), "#018");+    assert_eq!(format!("{f:0123}"), "0123");+    assert_eq!(format!("{f:-<5}"), "-<5");+    assert_eq!(format!("{f:+<05.2}"), "+<05.2");+    assert_eq!(format!("{f:x<+#065535.123X?}"), "x<+#065535.123X");++    // String quoting is done by the lowerexp impl in our tests+    assert_eq!(format!("{f:\"<5e}"), r#""\"<5""#);+    assert_eq!(format!("{f:'<5e}"), r#""\'<5""#);+    assert_eq!(format!("{f:\\<5e}"), r#""\\<5""#);+    // Same thing, unescaped+    assert_eq!(format!("{f:\"<5}"), r#""<5"#);+    assert_eq!(format!("{f:'<5}"), r#"'<5"#);+    assert_eq!(format!("{f:\\<5}"), r#"\<5"#);+}