efmt_core/items/expressions/
tuples.rs1use crate::format::{Format, Formatter};
2use crate::items::components::TupleLike;
3use crate::items::tokens::AtomToken;
4use crate::items::Expr;
5use crate::parse::Parse;
6use crate::span::Span;
7
8#[derive(Debug, Clone, Span, Parse, Format)]
10pub struct TupleExpr(TupleLike<Expr>);
11
12impl TupleExpr {
13    pub fn children(&self) -> impl Iterator<Item = &Expr> {
14        self.items().1.iter()
15    }
16
17    pub(crate) fn items(&self) -> (Option<&AtomToken>, &[Expr]) {
18        self.0.items()
19    }
20
21    pub(crate) fn try_format_app_file(&self, fmt: &mut Formatter) -> bool {
22        self.0.try_format_app_file(fmt)
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn tuple_works() {
32        let texts = [
33            "{}",
34            "{1}",
35            "{foo, bar, baz}",
36            indoc::indoc! {"
37            {1, 2, 3, 4, 5, 6,
38             7, 8, 9}"},
39            indoc::indoc! {"
40            {1,
41             2,
42             {3, 4, 5},
43             6,
44             {7, 8, 9}}"},
45            indoc::indoc! {"
46            {error,
47             {Foo, Bar,
48              Baz},
49             qux}"},
50        ];
51        for text in texts {
52            crate::assert_format!(text, Expr);
53        }
54    }
55
56    #[test]
57    fn tagged_tuple_works() {
58        let texts = [indoc::indoc! {"
59            {error, {Foo, Bar,
60                     Baz},
61                    qux}"}];
62        for text in texts {
63            crate::assert_format!(text, Expr);
64        }
65    }
66}