1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use super::*;

pub(super) fn write(
    mut buf: String,
    node: Node,
    fmts: &[Fmt],
    col: usize,
    seq: SeqIter,
    field_name: Option<&str>,
) -> String {
    let fmt = fmts.get(node.index()).unwrap();

    let id = node.kserd().id();

    let prefix = |mut buf| {
        maybe_write_nonprim_ident(&mut buf, fmt.id, id);
        buf.push('[');
        buf
    };
    let suffix = |mut buf: String| {
        buf.push(']');
        buf
    };

    match fmt.line {
        Repr::Inline => {
            delim_writer(buf, prefix, suffix, |mut buf| {
                let rm_trailing = seq.len() > 0;

                for n in seq {
                    buf = write_node(buf, n, fmts, col);
                    buf.push_str(FIELDS_SEPARATOR);
                }
                if rm_trailing {
                    FIELDS_SEPARATOR.chars().for_each(|_| {
                        buf.pop();
                    }); // remove trailing separator
                }
                buf
            })
        }
        Repr::Concise => delim_writer(buf, prefix, suffix, |mut buf| {
            buf.push('\n');

            let c = col + INDENT;

            for n in seq {
                write_indent(&mut buf, c);
                buf = write_node(buf, n, fmts, c);
                buf.push('\n');
            }

            write_indent(&mut buf, col);

            buf
        }),
        Repr::Verbose => {
            let field_name = field_name.expect("Verbose Seqs require a field name");

            for n in seq {
                // write field name
                write_indent(&mut buf, col);
                buf.push_str("[[");
                buf.push_str(field_name);
                buf.push_str("]]\n");

                // write value
                match fmts.get(n.index()).unwrap().line {
                    Repr::Verbose => {
                        buf = write_node(buf, n, fmts, col + INDENT);
                    }
                    _ => {
                        write_indent(&mut buf, col);
                        buf = write_node(buf, n, fmts, col);
                        buf.push('\n');
                    }
                }
                buf.push('\n');
            }

            buf
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn seq_fmting() {
        let kserd = Kserd::with_id(
            "hello",
            Value::Seq(vec![
                Kserd::new_num(100),
                Kserd::new_num(-101),
                Kserd::new_num(3.14),
            ]),
        )
        .unwrap();

        let mut config = FormattingConfig {
            id_on_primitives: true,
            id_on_seqs: true,
            width_limit: None,
            ..Default::default()
        };

        let s = kserd.as_str_with_config(config);
        println!("{}", s);
        assert_eq!(&s, "hello [<i32> 100, <i32> -101, <f64> 3.14]");

        config.width_limit = Some(20);

        let s = kserd.as_str_with_config(config);
        println!("{}", s);
        assert_eq!(
            &s,
            "hello [
    <i32> 100
    <i32> -101
    <f64> 3.14
]"
        );

        config.width_limit = Some(0); // if seq is root, can only be concise

        let s = kserd.as_str_with_config(config);
        println!("{}", s);
        assert_eq!(
            &s,
            "hello [
    <i32> 100
    <i32> -101
    <f64> 3.14
]"
        );
    }

    #[test]
    fn empty_seq() {
        let kserd = Kserd::with_id("something", Value::Seq(vec![])).unwrap();

        let mut fmtr = Formatter::new(&kserd);

        // with id
        fmtr.id(0, true).unwrap();

        fmtr.concise(0).unwrap(); // concise
        assert_eq!(&fmtr.write_string(String::new()), "something [\n]");

        fmtr.inline(0).unwrap(); // inline
        assert_eq!(&fmtr.write_string(String::new()), "something []");

        // no id
        fmtr.id(0, false).unwrap();

        fmtr.concise(0).unwrap(); // concise
        assert_eq!(&fmtr.write_string(String::new()), "[\n]");

        fmtr.inline(0).unwrap(); // inline
        assert_eq!(&fmtr.write_string(String::new()), "[]");
    }

}