f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
use super::{LabelFormat, LabelPiece, LabelPlace, WrittenLabel};
use crate::chart::Decimal;
use crate::configuration::NumberSeparators;
use crate::params::CHART_FORMAT_RED;

fn separators() -> NumberSeparators {
    NumberSeparators::new('.', ',').unwrap()
}

fn value(text: &str) -> Decimal {
    Decimal::parse(text).unwrap()
}

/// The pieces of `value` in the code `code`, each text as itself and each blank as `_` and
/// its character, joined.
fn written(code: &str, text: &str) -> String {
    let format = LabelFormat::code(code, separators()).unwrap();
    shown(&format.write(value(text)).unwrap())
}

fn shown(written: &WrittenLabel) -> String {
    written
        .pieces
        .iter()
        .map(|piece| match piece {
            LabelPiece::Text(text) => text.clone(),
            LabelPiece::Blank(ch) => format!("_{ch}"),
        })
        .collect()
}

#[test]
fn writes_the_digit_placeholders_point_groups_and_percent_signs() {
    for (code, text, label) in [
        ("0%", "0.2125", "21%"),
        ("0%", "0.125", "13%"),
        ("0.0%", "0.0125", "1.3%"),
        ("0.00%", "0.0001", "0.01%"),
        ("0", "3.5", "4"),
        ("0", "0.25", "0"),
        ("0.0", "3", "3.0"),
        ("0.00", "4", "4.00"),
        ("#,##0", "1234567", "1,234,567"),
        ("#,##0", "1234.5", "1,235"),
        ("#,##0.0", "12345", "12,345.0"),
        ("#,##0.0", "0.05", "0.1"),
        ("0.0#", "1.5", "1.5"),
        ("0.0#", "1.256", "1.26"),
        ("#.00", "0.5", ".50"),
        ("0,", "1234567", "1235"),
        ("#,##0,", "12345678", "12,346"),
        ("0.0,,", "1234567", "1.2"),
        ("#,##0.##", "1234", "1,234."),
        ("#,##0.##", "7.12345", "7.12"),
        ("000", "7", "007"),
    ] {
        assert_eq!(written(code, text), label, "{text} in {code}");
    }
}

#[test]
fn rounds_half_away_from_zero_after_fifteen_significant_digits() {
    for (code, text, label) in [
        ("0.00", "1.0049999999999999", "1.01"),
        ("0.00", "2.6749999999999998", "2.68"),
        ("0.0", "0.25", "0.3"),
        ("0.0", "-0.25", "-0.3"),
        ("0", "2.5", "3"),
        ("0.0%", "0.21249999999999999", "21.3%"),
    ] {
        assert_eq!(written(code, text), label, "{text} in {code}");
    }
}

#[test]
fn writes_literals_blanks_sections_and_the_red_color() {
    let accounting = r"#,##0.0_);[Red]\(#,##0.0\)";
    for (code, text, label) in [
        (accounting, "1234.55", "1,234.6_)"),
        ("_(0.0_)", "1.25", "_(1.3_)"),
        (accounting, "-3.45", "(3.5)"),
        (r##""$"#,##0.00"##, "1234.567", "$1,234.57"),
        (r#"0.0" x""#, "1.25", "1.3 x"),
        ("0.0", "-1.25", "-1.3"),
        // A value below zero that rounds to zero writes no minus sign, but the text of its
        // own section.
        ("0.0", "-0.04", "0.0"),
        ("0%", "-0.004", "0%"),
        ("0.0;(0.0)", "-0.04", "(0.0)"),
        (r#""$"0.00"#, "-1.5", "-$1.50"),
        (r#"0;(0);"zero""#, "0", "zero"),
    ] {
        assert_eq!(written(code, text), label, "{text} in {code}");
    }
    let format = LabelFormat::code(accounting, separators()).unwrap();
    assert_eq!(
        format.write(value("-1")).unwrap().color,
        Some(CHART_FORMAT_RED)
    );
    assert_eq!(format.write(value("1")).unwrap().color, None);
    let comma = LabelFormat::code("#,##0.0", NumberSeparators::new(',', '.').unwrap()).unwrap();
    assert_eq!(shown(&comma.write(value("1234.56")).unwrap()), "1.234,6");
}

#[test]
fn writes_fill_currency_brackets_question_blanks_and_scientific() {
    let comma = r#"_-* #,##0.00_-;\-* #,##0.00_-;_-* "-"_-;_-@_-"#;
    let accounting = r#"_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)"#;
    for (code, text, label) in [
        (comma, "5000", "_-5,000.00_-"),
        (comma, "-1000", "-1,000.00_-"),
        (comma, "0", "_--_-"),
        (accounting, "5000", "_($5,000.00_)"),
        (accounting, "-1000", "_($(1,000.00)"),
        (accounting, "0", "_($-_ _ _)"),
        ("[$€-2]#,##0.00", "250", "€250.00"),
        ("[$$-409]#,##0;([$$-409]#,##0)", "300", "$300"),
        ("[$$-409]#,##0;([$$-409]#,##0)", "-50", "($50)"),
        ("0.0?", "3.5", "3.5_ "),
        ("0.0?", "3.25", "3.25"),
        ("0.00E+00", "0", "0.00E+00"),
        ("0.00E+00", "120000", "1.20E+05"),
        ("0.00E+00", "0.00045", "4.50E-04"),
        ("0.00E+00", "9.999", "1.00E+01"),
        ("0.00E-00", "120000", "1.20E05"),
        (r"0.0\ \ ", "3.5", "3.5  "),
    ] {
        assert_eq!(written(code, text), label, "{text} in {code}");
    }
}

#[test]
fn reads_no_code_with_a_part_that_it_does_not_write() {
    for code in [
        "",
        "##0.0E+0",
        "0.0E+",
        "# ?/?",
        "?/?",
        "m/d/yyyy",
        "@",
        "[>100]0;0",
        "[Blue]0",
        "[$-409]0",
        "0;0;0;@;0",
        "x0",
    ] {
        assert_eq!(LabelFormat::code(code, separators()), None, "{code}");
    }
}

/// Each value of `cases` in General at `place` writes its text.
fn assert_general(place: LabelPlace, cases: &[(&str, &str)]) {
    let general = LabelFormat::general(separators(), place);
    for (text, label) in cases {
        let written = general.write(value(text)).map(|written| shown(&written));
        assert_eq!(written.as_deref(), Some(*label), "{text}");
    }
}

#[test]
fn writes_general_of_a_data_label_in_at_most_eleven_characters() {
    assert_general(
        LabelPlace::Data,
        &[
            ("0.30000000000000004", "0.3"),
            ("0.33333333333333331", "0.333333333"),
            ("1234.55", "1234.55"),
            ("0.125", "0.125"),
            // The minus sign does not count.
            ("-0.33333333333333331", "-0.333333333"),
            ("-12345678901", "-12345678901"),
            ("12345678901", "12345678901"),
            ("0", "0"),
            ("0.000123456789", "0.000123457"),
            ("0.0001", "0.0001"),
            // Scientific notation where it shows more of the value's significant digits than
            // decimals do.
            ("123456789012", "1.23457E+11"),
            ("-987654321000000", "-9.87654E+14"),
            ("0.0000123456789", "1.23457E-05"),
            ("0.00001", "0.00001"),
            ("0.000000001", "0.000000001"),
            ("0.0000001234", "1.234E-07"),
            ("0.0000000000012", "1.2E-12"),
            ("999999999999.6", "1E+12"),
            ("1E+150", "1E+150"),
        ],
    );
}

#[test]
fn writes_general_of_a_value_axis_in_at_most_nine_characters() {
    assert_general(
        LabelPlace::ValueAxis,
        &[
            ("800000000", "800000000"),
            ("1000000000", "1E+09"),
            ("1600000000", "1.6E+09"),
            ("14000000000", "1.4E+10"),
            // The minus sign counts.
            ("-300000000", "-3E+08"),
            ("-90000000", "-90000000"),
            ("0.000014", "0.000014"),
            ("0.000002", "0.000002"),
            ("1234567890", "1.235E+09"),
            ("0.14285714285714285", "0.1428571"),
        ],
    );
}

#[test]
fn writes_every_digit_of_general_where_the_target_does_not_write_formats() {
    let general = LabelFormat::all_digits();
    let written = general.write(value("0.30000000000000004")).unwrap();
    assert_eq!(shown(&written), "0.30000000000000004");
}