wdl_format/v1/
decl.rs

1//! Formatting functions for declarations.
2
3use wdl_ast::SyntaxKind;
4
5use crate::PreToken;
6use crate::TokenStream;
7use crate::Writable as _;
8use crate::element::FormatElement;
9
10/// Formats a [`PrimitiveType`](wdl_ast::v1::PrimitiveType).
11///
12/// # Panics
13///
14/// This will panic if the element does not have the expected children.
15pub fn format_primitive_type(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
16    for child in element.children().expect("primitive type children") {
17        (&child).write(stream);
18    }
19}
20
21/// Formats an [`ArrayType`](wdl_ast::v1::ArrayType).
22///
23/// # Panics
24///
25/// This will panic if the element does not have the expected children.
26pub fn format_array_type(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
27    for child in element.children().expect("array type children") {
28        (&child).write(stream);
29    }
30}
31
32/// Formats a [`MapType`](wdl_ast::v1::MapType).
33///
34/// # Panics
35///
36/// This will panic if the element does not have the expected children.
37pub fn format_map_type(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
38    for child in element.children().expect("map type children") {
39        (&child).write(stream);
40        if child.element().kind() == SyntaxKind::Comma {
41            stream.end_word();
42        }
43    }
44}
45
46/// Formats an [`ObjectType`](wdl_ast::v1::ObjectType).
47///
48/// # Panics
49///
50/// This will panic if the element does not have the expected children.
51pub fn format_object_type(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
52    for child in element.children().expect("object type children") {
53        (&child).write(stream);
54    }
55}
56
57/// Formats a [`PairType`](wdl_ast::v1::PairType).
58///
59/// # Panics
60///
61/// This will panic if the element does not have the expected children.
62pub fn format_pair_type(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
63    for child in element.children().expect("pair type children") {
64        (&child).write(stream);
65        if child.element().kind() == SyntaxKind::Comma {
66            stream.end_word();
67        }
68    }
69}
70
71/// Formats a [`TypeRef`](wdl_ast::v1::TypeRef).
72///
73/// # Panics
74///
75/// This will panic if the element does not have the expected children.
76pub fn format_type_ref(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
77    for child in element.children().expect("type ref children") {
78        (&child).write(stream);
79    }
80}
81
82/// Formats an [`UnboundDecl`](wdl_ast::v1::UnboundDecl).
83///
84/// # Panics
85///
86/// This will panic if the element does not have the expected children.
87pub fn format_unbound_decl(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
88    for child in element.children().expect("unbound decl children") {
89        (&child).write(stream);
90        stream.end_word();
91    }
92    stream.end_line();
93}
94
95/// Formats a [`BoundDecl`](wdl_ast::v1::BoundDecl).
96///
97/// # Panics
98///
99/// This will panic if the element does not have the expected children.
100pub fn format_bound_decl(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
101    for child in element.children().expect("bound decl children") {
102        (&child).write(stream);
103        stream.end_word();
104    }
105    stream.end_line();
106}