From bd613719f73c13d8850e9d4da91f9cf7cf2ff99a Mon Sep 17 00:00:00 2001 From: Yehyoung Kang Date: Sat, 12 Jul 2025 01:44:44 +0900 Subject: [PATCH 1/6] feat(writer): Implement TomlInteger --- crates/toml_writer/src/integer.rs | 194 ++++++++++++++++++++++++++++ crates/toml_writer/src/lib.rs | 3 + crates/toml_writer/tests/integer.rs | 92 +++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 crates/toml_writer/src/integer.rs create mode 100644 crates/toml_writer/tests/integer.rs diff --git a/crates/toml_writer/src/integer.rs b/crates/toml_writer/src/integer.rs new file mode 100644 index 000000000..e71571a5b --- /dev/null +++ b/crates/toml_writer/src/integer.rs @@ -0,0 +1,194 @@ +use core::fmt::{self, Display}; + +/// N must be an integer type. +#[derive(Copy, Clone, Debug)] +pub struct TomlInteger { + value: N, + radix: Radix, +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +#[derive(Copy, Clone, Debug)] +pub struct TomlIntegerFormat { + radix: Radix, + _separators: (), // Placeholder for future use +} + +impl TomlIntegerFormat { + pub fn new() -> Self { + Self { + radix: Radix::Decimal, + _separators: (), + } + } + + pub fn as_decimal(self) -> Self { + Self { + radix: Radix::Decimal, + ..self + } + } + + pub fn as_hex_upper(self) -> Self { + Self { + radix: Radix::Hexadecimal { + case: HexCase::Upper, + }, + ..self + } + } + + pub fn as_hex_lower(self) -> Self { + Self { + radix: Radix::Hexadecimal { + case: HexCase::Lower, + }, + ..self + } + } + + pub fn as_octal(self) -> Self { + Self { + radix: Radix::Octal, + ..self + } + } + + pub fn as_binary(self) -> Self { + Self { + radix: Radix::Binary, + ..self + } + } + + /// Returns `None` if the value is negative and the radix is not decimal. + pub fn format>(self, value: N) -> Option> + where + TomlInteger: crate::WriteTomlValue, + { + match self.radix { + Radix::Decimal => (), + Radix::Hexadecimal { .. } | Radix::Octal | Radix::Binary => { + if value < 0 { + return None; + } + } + } + + Some(TomlInteger { + value, + radix: self.radix, + }) + } +} + +impl Default for TomlIntegerFormat { + fn default() -> Self { + Self::new() + } +} + +#[derive(Copy, Clone, Debug)] +enum Radix { + Decimal, + Hexadecimal { case: HexCase }, + Octal, + Binary, +} + +#[derive(Copy, Clone, Debug)] +enum HexCase { + Upper, + Lower, +} + +fn write_toml_value< + N: Display + fmt::UpperHex + fmt::LowerHex + fmt::Octal + fmt::Binary, + W: crate::TomlWrite + ?Sized, +>( + value: N, + radix: &Radix, + writer: &mut W, +) -> fmt::Result { + match radix { + Radix::Decimal => write!(writer, "{value}")?, + Radix::Hexadecimal { case } => match case { + HexCase::Upper => write!(writer, "0x{value:X}")?, + HexCase::Lower => write!(writer, "0x{value:x}")?, + }, + Radix::Octal => write!(writer, "0o{value:o}")?, + Radix::Binary => write!(writer, "0b{value:b}")?, + } + Ok(()) +} diff --git a/crates/toml_writer/src/lib.rs b/crates/toml_writer/src/lib.rs index 7772e2e3d..60c32b549 100644 --- a/crates/toml_writer/src/lib.rs +++ b/crates/toml_writer/src/lib.rs @@ -59,11 +59,14 @@ #[cfg(feature = "alloc")] extern crate alloc; +mod integer; mod key; mod string; mod value; mod write; +pub use integer::TomlInteger; +pub use integer::TomlIntegerFormat; #[cfg(feature = "alloc")] pub use key::ToTomlKey; pub use key::WriteTomlKey; diff --git a/crates/toml_writer/tests/integer.rs b/crates/toml_writer/tests/integer.rs new file mode 100644 index 000000000..a437df8a9 --- /dev/null +++ b/crates/toml_writer/tests/integer.rs @@ -0,0 +1,92 @@ +use toml_writer::ToTomlValue; +use toml_writer::TomlIntegerFormat; + +#[test] +fn positive() { + assert_eq!( + TomlIntegerFormat::new() + .format(42) + .map(|i| i.to_toml_value()), + Some(String::from("42")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_decimal() + .format(42) + .map(|i| i.to_toml_value()), + Some(String::from("42")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_hex_upper() + .format(42) + .map(|i| i.to_toml_value()), + Some(String::from("0x2A")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_hex_lower() + .format(42) + .map(|i| i.to_toml_value()), + Some(String::from("0x2a")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_octal() + .format(42) + .map(|i| i.to_toml_value()), + Some(String::from("0o52")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_binary() + .format(42) + .map(|i| i.to_toml_value()), + Some(String::from("0b101010")) + ); +} + +#[test] +fn negative() { + assert_eq!( + TomlIntegerFormat::new() + .format(-42) + .map(|i| i.to_toml_value()), + Some(String::from("-42")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_decimal() + .format(-42) + .map(|i| i.to_toml_value()), + Some(String::from("-42")) + ); + assert_eq!( + TomlIntegerFormat::new() + .as_hex_upper() + .format(-42) + .map(|i| i.to_toml_value()), + None + ); + assert_eq!( + TomlIntegerFormat::new() + .as_hex_lower() + .format(-42) + .map(|i| i.to_toml_value()), + None + ); + assert_eq!( + TomlIntegerFormat::new() + .as_octal() + .format(-42) + .map(|i| i.to_toml_value()), + None + ); + assert_eq!( + TomlIntegerFormat::new() + .as_binary() + .format(-42) + .map(|i| i.to_toml_value()), + None + ); +} From d63f397e60fa8078b3afb5808acf85436bf7ed44 Mon Sep 17 00:00:00 2001 From: Yehyoung Kang Date: Sat, 12 Jul 2025 01:59:09 +0900 Subject: [PATCH 2/6] refactor(writer): Move TomlIntegerFormat to top --- crates/toml_writer/src/integer.rs | 158 +++++++++++++++--------------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/crates/toml_writer/src/integer.rs b/crates/toml_writer/src/integer.rs index e71571a5b..2fa8eb2ac 100644 --- a/crates/toml_writer/src/integer.rs +++ b/crates/toml_writer/src/integer.rs @@ -1,84 +1,5 @@ use core::fmt::{self, Display}; -/// N must be an integer type. -#[derive(Copy, Clone, Debug)] -pub struct TomlInteger { - value: N, - radix: Radix, -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - -impl crate::WriteTomlValue for TomlInteger { - fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) - } -} - #[derive(Copy, Clone, Debug)] pub struct TomlIntegerFormat { radix: Radix, @@ -159,6 +80,85 @@ impl Default for TomlIntegerFormat { } } +/// N must be an integer type. +#[derive(Copy, Clone, Debug)] +pub struct TomlInteger { + value: N, + radix: Radix, +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + +impl crate::WriteTomlValue for TomlInteger { + fn write_toml_value(&self, writer: &mut W) -> fmt::Result { + write_toml_value(self.value, &self.radix, writer) + } +} + #[derive(Copy, Clone, Debug)] enum Radix { Decimal, From 67e957cf4200f5970dfdb76535c14a0f611f72ca Mon Sep 17 00:00:00 2001 From: Yehyoung Kang Date: Sat, 12 Jul 2025 02:09:34 +0900 Subject: [PATCH 3/6] refactor(writer): Apply review suggestions - Remove unused _separators field in TomlIntegerFormat - Change as_decimal() et al to accept mut self - TomlInteger contains TomlIntegerFormat --- crates/toml_writer/src/integer.rs | 82 +++++++++++++------------------ 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/crates/toml_writer/src/integer.rs b/crates/toml_writer/src/integer.rs index 2fa8eb2ac..2eb1bad3d 100644 --- a/crates/toml_writer/src/integer.rs +++ b/crates/toml_writer/src/integer.rs @@ -3,54 +3,42 @@ use core::fmt::{self, Display}; #[derive(Copy, Clone, Debug)] pub struct TomlIntegerFormat { radix: Radix, - _separators: (), // Placeholder for future use } impl TomlIntegerFormat { pub fn new() -> Self { Self { radix: Radix::Decimal, - _separators: (), } } - pub fn as_decimal(self) -> Self { - Self { - radix: Radix::Decimal, - ..self - } + pub fn as_decimal(mut self) -> Self { + self.radix = Radix::Decimal; + self } - pub fn as_hex_upper(self) -> Self { - Self { - radix: Radix::Hexadecimal { - case: HexCase::Upper, - }, - ..self - } + pub fn as_hex_upper(mut self) -> Self { + self.radix = Radix::Hexadecimal { + case: HexCase::Upper, + }; + self } - pub fn as_hex_lower(self) -> Self { - Self { - radix: Radix::Hexadecimal { - case: HexCase::Lower, - }, - ..self - } + pub fn as_hex_lower(mut self) -> Self { + self.radix = Radix::Hexadecimal { + case: HexCase::Lower, + }; + self } - pub fn as_octal(self) -> Self { - Self { - radix: Radix::Octal, - ..self - } + pub fn as_octal(mut self) -> Self { + self.radix = Radix::Octal; + self } - pub fn as_binary(self) -> Self { - Self { - radix: Radix::Binary, - ..self - } + pub fn as_binary(mut self) -> Self { + self.radix = Radix::Binary; + self } /// Returns `None` if the value is negative and the radix is not decimal. @@ -69,7 +57,7 @@ impl TomlIntegerFormat { Some(TomlInteger { value, - radix: self.radix, + format: self, }) } } @@ -84,78 +72,78 @@ impl Default for TomlIntegerFormat { #[derive(Copy, Clone, Debug)] pub struct TomlInteger { value: N, - radix: Radix, + format: TomlIntegerFormat, } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } impl crate::WriteTomlValue for TomlInteger { fn write_toml_value(&self, writer: &mut W) -> fmt::Result { - write_toml_value(self.value, &self.radix, writer) + write_toml_value(self.value, &self.format, writer) } } @@ -178,10 +166,10 @@ fn write_toml_value< W: crate::TomlWrite + ?Sized, >( value: N, - radix: &Radix, + format: &TomlIntegerFormat, writer: &mut W, ) -> fmt::Result { - match radix { + match format.radix { Radix::Decimal => write!(writer, "{value}")?, Radix::Hexadecimal { case } => match case { HexCase::Upper => write!(writer, "0x{value:X}")?, From 1ed9630747fd537141d6c9fce36eaa680b3f7730 Mon Sep 17 00:00:00 2001 From: Yehyoung Kang Date: Sat, 12 Jul 2025 02:23:15 +0900 Subject: [PATCH 4/6] test(writer): Use snapbox --- crates/toml_writer/tests/integer.rs | 149 ++++++++++++++-------------- 1 file changed, 77 insertions(+), 72 deletions(-) diff --git a/crates/toml_writer/tests/integer.rs b/crates/toml_writer/tests/integer.rs index a437df8a9..eda935243 100644 --- a/crates/toml_writer/tests/integer.rs +++ b/crates/toml_writer/tests/integer.rs @@ -1,92 +1,97 @@ +use snapbox::prelude::*; +use snapbox::str; + use toml_writer::ToTomlValue; +use toml_writer::TomlInteger; use toml_writer::TomlIntegerFormat; +use toml_writer::WriteTomlValue; -#[test] -fn positive() { - assert_eq!( - TomlIntegerFormat::new() - .format(42) - .map(|i| i.to_toml_value()), - Some(String::from("42")) - ); - assert_eq!( - TomlIntegerFormat::new() +#[track_caller] +fn t>(value: N, expected: impl IntoData) +where + TomlInteger: WriteTomlValue, +{ + let results = IntegerResults { + value, + decimal: TomlIntegerFormat::new() .as_decimal() - .format(42) + .format(value) .map(|i| i.to_toml_value()), - Some(String::from("42")) - ); - assert_eq!( - TomlIntegerFormat::new() + hex_upper: TomlIntegerFormat::new() .as_hex_upper() - .format(42) + .format(value) .map(|i| i.to_toml_value()), - Some(String::from("0x2A")) - ); - assert_eq!( - TomlIntegerFormat::new() + hex_lower: TomlIntegerFormat::new() .as_hex_lower() - .format(42) + .format(value) .map(|i| i.to_toml_value()), - Some(String::from("0x2a")) - ); - assert_eq!( - TomlIntegerFormat::new() + octal: TomlIntegerFormat::new() .as_octal() - .format(42) + .format(value) .map(|i| i.to_toml_value()), - Some(String::from("0o52")) - ); - assert_eq!( - TomlIntegerFormat::new() + binary: TomlIntegerFormat::new() .as_binary() - .format(42) + .format(value) .map(|i| i.to_toml_value()), - Some(String::from("0b101010")) + }; + snapbox::assert_data_eq!(results.to_debug(), expected.raw()); +} + +#[derive(Debug)] +#[allow(dead_code)] +struct IntegerResults { + value: N, + decimal: Option, + hex_upper: Option, + hex_lower: Option, + octal: Option, + binary: Option, +} + +#[test] +fn positive() { + t( + 42, + str![[r#" +IntegerResults { + value: 42, + decimal: Some( + "42", + ), + hex_upper: Some( + "0x2A", + ), + hex_lower: Some( + "0x2a", + ), + octal: Some( + "0o52", + ), + binary: Some( + "0b101010", + ), +} + +"#]], ); } #[test] fn negative() { - assert_eq!( - TomlIntegerFormat::new() - .format(-42) - .map(|i| i.to_toml_value()), - Some(String::from("-42")) - ); - assert_eq!( - TomlIntegerFormat::new() - .as_decimal() - .format(-42) - .map(|i| i.to_toml_value()), - Some(String::from("-42")) - ); - assert_eq!( - TomlIntegerFormat::new() - .as_hex_upper() - .format(-42) - .map(|i| i.to_toml_value()), - None - ); - assert_eq!( - TomlIntegerFormat::new() - .as_hex_lower() - .format(-42) - .map(|i| i.to_toml_value()), - None - ); - assert_eq!( - TomlIntegerFormat::new() - .as_octal() - .format(-42) - .map(|i| i.to_toml_value()), - None - ); - assert_eq!( - TomlIntegerFormat::new() - .as_binary() - .format(-42) - .map(|i| i.to_toml_value()), - None + t( + -42, + str![[r#" +IntegerResults { + value: -42, + decimal: Some( + "-42", + ), + hex_upper: None, + hex_lower: None, + octal: None, + binary: None, +} + +"#]], ); } From 41b82c62fb55719b3ee71cf745623e8880aa6864 Mon Sep 17 00:00:00 2001 From: Yehyoung Kang Date: Sat, 12 Jul 2025 02:31:15 +0900 Subject: [PATCH 5/6] docs(writer): Improve docstrings --- crates/toml_writer/src/integer.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/toml_writer/src/integer.rs b/crates/toml_writer/src/integer.rs index 2eb1bad3d..57bd239ae 100644 --- a/crates/toml_writer/src/integer.rs +++ b/crates/toml_writer/src/integer.rs @@ -1,22 +1,26 @@ use core::fmt::{self, Display}; +/// Describes how a TOML integer should be formatted. #[derive(Copy, Clone, Debug)] pub struct TomlIntegerFormat { radix: Radix, } impl TomlIntegerFormat { + /// Creates a new integer format (decimal). pub fn new() -> Self { Self { radix: Radix::Decimal, } } + /// Sets the format to decimal. pub fn as_decimal(mut self) -> Self { self.radix = Radix::Decimal; self } + /// Sets the format to hexadecimal with all characters in uppercase. pub fn as_hex_upper(mut self) -> Self { self.radix = Radix::Hexadecimal { case: HexCase::Upper, @@ -24,6 +28,7 @@ impl TomlIntegerFormat { self } + /// Sets the format to hexadecimal with all characters in lowercase. pub fn as_hex_lower(mut self) -> Self { self.radix = Radix::Hexadecimal { case: HexCase::Lower, @@ -31,17 +36,22 @@ impl TomlIntegerFormat { self } + /// Sets the format to octal. pub fn as_octal(mut self) -> Self { self.radix = Radix::Octal; self } + /// Sets the format to binary. pub fn as_binary(mut self) -> Self { self.radix = Radix::Binary; self } - /// Returns `None` if the value is negative and the radix is not decimal. + /// 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). pub fn format>(self, value: N) -> Option> where TomlInteger: crate::WriteTomlValue, @@ -68,7 +78,9 @@ impl Default for TomlIntegerFormat { } } -/// N must be an integer type. +/// Helper struct for formatting TOML integers. +/// +/// This may be constructed by calling [`TomlIntegerFormat::format()`]. #[derive(Copy, Clone, Debug)] pub struct TomlInteger { value: N, From 1e39e67eacb5a3edc56e5831e3a16aa1865d4e79 Mon Sep 17 00:00:00 2001 From: Yehyoung Kang Date: Sat, 12 Jul 2025 03:00:21 +0900 Subject: [PATCH 6/6] test(writer): Integer tests require alloc --- crates/toml_writer/tests/integer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/toml_writer/tests/integer.rs b/crates/toml_writer/tests/integer.rs index eda935243..713c5d467 100644 --- a/crates/toml_writer/tests/integer.rs +++ b/crates/toml_writer/tests/integer.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "alloc")] + use snapbox::prelude::*; use snapbox::str;