Skip to content

Commit edcfcb4

Browse files
authored
Fix bool format (#91)
* Fix format_bool * Add test_format_bool
1 parent 40a6032 commit edcfcb4

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

format/src/format.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use itertools::{Itertools, PeekingNext};
22
use malachite_bigint::{BigInt, Sign};
3+
use num_traits::FromPrimitive;
34
use num_traits::{cast::ToPrimitive, Signed};
45
use rustpython_literal::float;
56
use rustpython_literal::format::Case;
@@ -419,15 +420,25 @@ impl FormatSpec {
419420

420421
pub fn format_bool(&self, input: bool) -> Result<String, FormatSpecError> {
421422
let x = u8::from(input);
422-
let result: Result<String, FormatSpecError> = match &self.format_type {
423-
Some(FormatType::Decimal) => Ok(x.to_string()),
423+
match &self.format_type {
424+
Some(
425+
FormatType::Binary
426+
| FormatType::Decimal
427+
| FormatType::Octal
428+
| FormatType::Number(Case::Lower)
429+
| FormatType::Hex(_)
430+
| FormatType::GeneralFormat(_)
431+
| FormatType::Character,
432+
) => self.format_int(&BigInt::from_u8(x).unwrap()),
433+
Some(FormatType::Exponent(_) | FormatType::FixedPoint(_) | FormatType::Percentage) => {
434+
self.format_float(x as f64)
435+
}
424436
None => {
425437
let first_letter = (input.to_string().as_bytes()[0] as char).to_uppercase();
426438
Ok(first_letter.collect::<String>() + &input.to_string()[1..])
427439
}
428440
_ => Err(FormatSpecError::InvalidFormatSpecifier),
429-
};
430-
result
441+
}
431442
}
432443

433444
pub fn format_float(&self, num: f64) -> Result<String, FormatSpecError> {
@@ -1030,6 +1041,42 @@ mod tests {
10301041
assert_eq!(FormatSpec::parse("<>-#23,.11b"), expected);
10311042
}
10321043

1044+
fn format_bool(text: &str, value: bool) -> Result<String, FormatSpecError> {
1045+
FormatSpec::parse(text).and_then(|spec| spec.format_bool(value))
1046+
}
1047+
1048+
#[test]
1049+
fn test_format_bool() {
1050+
assert_eq!(format_bool("b", true), Ok("1".to_owned()));
1051+
assert_eq!(format_bool("b", false), Ok("0".to_owned()));
1052+
assert_eq!(format_bool("d", true), Ok("1".to_owned()));
1053+
assert_eq!(format_bool("d", false), Ok("0".to_owned()));
1054+
assert_eq!(format_bool("o", true), Ok("1".to_owned()));
1055+
assert_eq!(format_bool("o", false), Ok("0".to_owned()));
1056+
assert_eq!(format_bool("n", true), Ok("1".to_owned()));
1057+
assert_eq!(format_bool("n", false), Ok("0".to_owned()));
1058+
assert_eq!(format_bool("x", true), Ok("1".to_owned()));
1059+
assert_eq!(format_bool("x", false), Ok("0".to_owned()));
1060+
assert_eq!(format_bool("X", true), Ok("1".to_owned()));
1061+
assert_eq!(format_bool("X", false), Ok("0".to_owned()));
1062+
assert_eq!(format_bool("g", true), Ok("1".to_owned()));
1063+
assert_eq!(format_bool("g", false), Ok("0".to_owned()));
1064+
assert_eq!(format_bool("G", true), Ok("1".to_owned()));
1065+
assert_eq!(format_bool("G", false), Ok("0".to_owned()));
1066+
assert_eq!(format_bool("c", true), Ok("\x01".to_owned()));
1067+
assert_eq!(format_bool("c", false), Ok("\x00".to_owned()));
1068+
assert_eq!(format_bool("e", true), Ok("1.000000e+00".to_owned()));
1069+
assert_eq!(format_bool("e", false), Ok("0.000000e+00".to_owned()));
1070+
assert_eq!(format_bool("E", true), Ok("1.000000E+00".to_owned()));
1071+
assert_eq!(format_bool("E", false), Ok("0.000000E+00".to_owned()));
1072+
assert_eq!(format_bool("f", true), Ok("1.000000".to_owned()));
1073+
assert_eq!(format_bool("f", false), Ok("0.000000".to_owned()));
1074+
assert_eq!(format_bool("F", true), Ok("1.000000".to_owned()));
1075+
assert_eq!(format_bool("F", false), Ok("0.000000".to_owned()));
1076+
assert_eq!(format_bool("%", true), Ok("100.000000%".to_owned()));
1077+
assert_eq!(format_bool("%", false), Ok("0.000000%".to_owned()));
1078+
}
1079+
10331080
#[test]
10341081
fn test_format_int() {
10351082
assert_eq!(

0 commit comments

Comments
 (0)