|
1 | 1 | use itertools::{Itertools, PeekingNext};
|
2 | 2 | use malachite_bigint::{BigInt, Sign};
|
| 3 | +use num_traits::FromPrimitive; |
3 | 4 | use num_traits::{cast::ToPrimitive, Signed};
|
4 | 5 | use rustpython_literal::float;
|
5 | 6 | use rustpython_literal::format::Case;
|
@@ -419,15 +420,25 @@ impl FormatSpec {
|
419 | 420 |
|
420 | 421 | pub fn format_bool(&self, input: bool) -> Result<String, FormatSpecError> {
|
421 | 422 | 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 | + } |
424 | 436 | None => {
|
425 | 437 | let first_letter = (input.to_string().as_bytes()[0] as char).to_uppercase();
|
426 | 438 | Ok(first_letter.collect::<String>() + &input.to_string()[1..])
|
427 | 439 | }
|
428 | 440 | _ => Err(FormatSpecError::InvalidFormatSpecifier),
|
429 |
| - }; |
430 |
| - result |
| 441 | + } |
431 | 442 | }
|
432 | 443 |
|
433 | 444 | pub fn format_float(&self, num: f64) -> Result<String, FormatSpecError> {
|
@@ -1030,6 +1041,42 @@ mod tests {
|
1030 | 1041 | assert_eq!(FormatSpec::parse("<>-#23,.11b"), expected);
|
1031 | 1042 | }
|
1032 | 1043 |
|
| 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 | + |
1033 | 1080 | #[test]
|
1034 | 1081 | fn test_format_int() {
|
1035 | 1082 | assert_eq!(
|
|
0 commit comments