1pub mod config;
4pub mod element;
5mod token;
6pub mod v1;
7
8use std::fmt::Write;
9
10pub use config::Config;
11pub use token::*;
12use wdl_ast::Element;
13use wdl_ast::Node as AstNode;
14
15use crate::element::FormatElement;
16
17#[cfg(windows)]
19pub const NEWLINE: &str = "\r\n";
20#[cfg(not(windows))]
22pub const NEWLINE: &str = "\n";
23pub const SPACE: &str = " ";
25pub const TAB: &str = "\t";
27
28pub trait Writable {
30 fn write(&self, stream: &mut TokenStream<PreToken>);
32}
33
34impl Writable for &FormatElement {
35 fn write(&self, stream: &mut TokenStream<PreToken>) {
36 match self.element() {
37 Element::Node(node) => match node {
38 AstNode::AccessExpr(_) => v1::expr::format_access_expr(self, stream),
39 AstNode::AdditionExpr(_) => v1::expr::format_addition_expr(self, stream),
40 AstNode::ArrayType(_) => v1::decl::format_array_type(self, stream),
41 AstNode::Ast(_) => v1::format_ast(self, stream),
42 AstNode::BoundDecl(_) => v1::decl::format_bound_decl(self, stream),
43 AstNode::CallAfter(_) => v1::workflow::call::format_call_after(self, stream),
44 AstNode::CallAlias(_) => v1::workflow::call::format_call_alias(self, stream),
45 AstNode::CallExpr(_) => v1::expr::format_call_expr(self, stream),
46 AstNode::CallInputItem(_) => {
47 v1::workflow::call::format_call_input_item(self, stream)
48 }
49 AstNode::CallStatement(_) => {
50 v1::workflow::call::format_call_statement(self, stream)
51 }
52 AstNode::CallTarget(_) => v1::workflow::call::format_call_target(self, stream),
53 AstNode::CommandSection(_) => v1::task::format_command_section(self, stream),
54 AstNode::ConditionalStatement(_) => {
55 v1::workflow::format_conditional_statement(self, stream)
56 }
57 AstNode::ConditionalStatementClause(_) => {
58 v1::workflow::format_conditional_statement_clause(self, stream)
59 }
60 AstNode::DefaultOption(_) => v1::expr::format_default_option(self, stream),
61 AstNode::DivisionExpr(_) => v1::expr::format_division_expr(self, stream),
62 AstNode::EqualityExpr(_) => v1::expr::format_equality_expr(self, stream),
63 AstNode::EnumDefinition(_) => v1::r#enum::format_enum_definition(self, stream),
64 AstNode::EnumTypeParameter(_) => {
65 v1::r#enum::format_enum_type_parameter(self, stream)
66 }
67 AstNode::EnumVariant(_) => v1::r#enum::format_enum_variant(self, stream),
68 AstNode::ExponentiationExpr(_) => {
69 v1::expr::format_exponentiation_expr(self, stream)
70 }
71 AstNode::GreaterEqualExpr(_) => v1::expr::format_greater_equal_expr(self, stream),
72 AstNode::GreaterExpr(_) => v1::expr::format_greater_expr(self, stream),
73 AstNode::IfExpr(_) => v1::expr::format_if_expr(self, stream),
74 AstNode::ImportAlias(_) => v1::import::format_import_alias(self, stream),
75 AstNode::ImportStatement(_) => v1::import::format_import_statement(self, stream),
76 AstNode::IndexExpr(_) => v1::expr::format_index_expr(self, stream),
77 AstNode::InequalityExpr(_) => v1::expr::format_inequality_expr(self, stream),
78 AstNode::InputSection(_) => v1::format_input_section(self, stream),
79 AstNode::LessEqualExpr(_) => v1::expr::format_less_equal_expr(self, stream),
80 AstNode::LessExpr(_) => v1::expr::format_less_expr(self, stream),
81 AstNode::LiteralArray(_) => v1::expr::format_literal_array(self, stream),
82 AstNode::LiteralBoolean(_) => v1::expr::format_literal_boolean(self, stream),
83 AstNode::LiteralFloat(_) => v1::expr::format_literal_float(self, stream),
84 AstNode::LiteralHints(_) => v1::format_literal_hints(self, stream),
85 AstNode::LiteralHintsItem(_) => v1::format_literal_hints_item(self, stream),
86 AstNode::LiteralInput(_) => v1::format_literal_input(self, stream),
87 AstNode::LiteralInputItem(_) => v1::format_literal_input_item(self, stream),
88 AstNode::LiteralInteger(_) => v1::expr::format_literal_integer(self, stream),
89 AstNode::LiteralMap(_) => v1::expr::format_literal_map(self, stream),
90 AstNode::LiteralMapItem(_) => v1::expr::format_literal_map_item(self, stream),
91 AstNode::LiteralNone(_) => v1::expr::format_literal_none(self, stream),
92 AstNode::LiteralNull(_) => v1::meta::format_literal_null(self, stream),
93 AstNode::LiteralObject(_) => v1::expr::format_literal_object(self, stream),
94 AstNode::LiteralObjectItem(_) => v1::expr::format_literal_object_item(self, stream),
95 AstNode::LiteralOutput(_) => v1::format_literal_output(self, stream),
96 AstNode::LiteralOutputItem(_) => v1::format_literal_output_item(self, stream),
97 AstNode::LiteralPair(_) => v1::expr::format_literal_pair(self, stream),
98 AstNode::LiteralString(_) => v1::expr::format_literal_string(self, stream),
99 AstNode::LiteralStruct(_) => v1::r#struct::format_literal_struct(self, stream),
100 AstNode::LiteralStructItem(_) => {
101 v1::r#struct::format_literal_struct_item(self, stream)
102 }
103 AstNode::LogicalAndExpr(_) => v1::expr::format_logical_and_expr(self, stream),
104 AstNode::LogicalNotExpr(_) => v1::expr::format_logical_not_expr(self, stream),
105 AstNode::LogicalOrExpr(_) => v1::expr::format_logical_or_expr(self, stream),
106 AstNode::MapType(_) => v1::decl::format_map_type(self, stream),
107 AstNode::MetadataArray(_) => v1::meta::format_metadata_array(self, stream),
108 AstNode::MetadataObject(_) => v1::meta::format_metadata_object(self, stream),
109 AstNode::MetadataObjectItem(_) => {
110 v1::meta::format_metadata_object_item(self, stream)
111 }
112 AstNode::MetadataSection(_) => v1::meta::format_metadata_section(self, stream),
113 AstNode::ModuloExpr(_) => v1::expr::format_modulo_expr(self, stream),
114 AstNode::MultiplicationExpr(_) => {
115 v1::expr::format_multiplication_expr(self, stream)
116 }
117 AstNode::NameRefExpr(_) => v1::expr::format_name_ref_expr(self, stream),
118 AstNode::NegationExpr(_) => v1::expr::format_negation_expr(self, stream),
119 AstNode::OutputSection(_) => v1::format_output_section(self, stream),
120 AstNode::PairType(_) => v1::decl::format_pair_type(self, stream),
121 AstNode::ObjectType(_) => v1::decl::format_object_type(self, stream),
122 AstNode::ParameterMetadataSection(_) => {
123 v1::meta::format_parameter_metadata_section(self, stream)
124 }
125 AstNode::ParenthesizedExpr(_) => v1::expr::format_parenthesized_expr(self, stream),
126 AstNode::Placeholder(_) => v1::expr::format_placeholder(self, stream),
127 AstNode::PrimitiveType(_) => v1::decl::format_primitive_type(self, stream),
128 AstNode::RequirementsItem(_) => v1::task::format_requirements_item(self, stream),
129 AstNode::RequirementsSection(_) => {
130 v1::task::format_requirements_section(self, stream)
131 }
132 AstNode::RuntimeItem(_) => v1::task::format_runtime_item(self, stream),
133 AstNode::RuntimeSection(_) => v1::task::format_runtime_section(self, stream),
134 AstNode::ScatterStatement(_) => {
135 v1::workflow::format_scatter_statement(self, stream)
136 }
137 AstNode::SepOption(_) => v1::expr::format_sep_option(self, stream),
138 AstNode::StructDefinition(_) => {
139 v1::r#struct::format_struct_definition(self, stream)
140 }
141 AstNode::SubtractionExpr(_) => v1::expr::format_subtraction_expr(self, stream),
142 AstNode::TaskDefinition(_) => v1::task::format_task_definition(self, stream),
143 AstNode::TaskHintsItem(_) => v1::task::format_task_hints_item(self, stream),
144 AstNode::TaskHintsSection(_) => v1::task::format_task_hints_section(self, stream),
145 AstNode::TrueFalseOption(_) => v1::expr::format_true_false_option(self, stream),
146 AstNode::TypeRef(_) => v1::decl::format_type_ref(self, stream),
147 AstNode::UnboundDecl(_) => v1::decl::format_unbound_decl(self, stream),
148 AstNode::VersionStatement(_) => v1::format_version_statement(self, stream),
149 AstNode::WorkflowDefinition(_) => {
150 v1::workflow::format_workflow_definition(self, stream)
151 }
152 AstNode::WorkflowHintsArray(_) => {
153 v1::workflow::format_workflow_hints_array(self, stream)
154 }
155 AstNode::WorkflowHintsItem(_) => {
156 v1::workflow::format_workflow_hints_item(self, stream)
157 }
158 AstNode::WorkflowHintsObject(_) => {
159 v1::workflow::format_workflow_hints_object(self, stream)
160 }
161 AstNode::WorkflowHintsObjectItem(_) => {
162 v1::workflow::format_workflow_hints_object_item(self, stream)
163 }
164 AstNode::WorkflowHintsSection(_) => {
165 v1::workflow::format_workflow_hints_section(self, stream)
166 }
167 },
168 Element::Token(token) => {
169 stream.push_ast_token(token);
170 }
171 }
172 }
173}
174
175#[derive(Debug, Default)]
177pub struct Formatter {
178 config: Config,
180}
181
182impl Formatter {
183 pub fn new(config: Config) -> Self {
185 Self { config }
186 }
187
188 pub fn config(&self) -> &Config {
190 &self.config
191 }
192
193 pub fn format<W: Writable>(&self, element: W) -> std::result::Result<String, std::fmt::Error> {
195 let mut result = String::new();
196
197 for token in self.to_stream(element) {
198 write!(result, "{token}", token = token.display(self.config()))?;
199 }
200
201 Ok(result)
202 }
203
204 fn to_stream<W: Writable>(&self, element: W) -> TokenStream<PostToken> {
206 let mut stream = TokenStream::default();
207 element.write(&mut stream);
208
209 let mut postprocessor = Postprocessor::default();
210 postprocessor.run(stream, self.config())
211 }
212}
213
214#[cfg(test)]
215mod tests {
216 use wdl_ast::Document;
217 use wdl_ast::Node;
218
219 use crate::Formatter;
220 use crate::element::node::AstNodeFormatExt as _;
221
222 #[test]
223 fn smoke() {
224 let (document, diagnostics) = Document::parse(
225 "## WDL
226version 1.2 # This is a comment attached to the version.
227
228# This is a comment attached to the task keyword.
229task foo # This is an inline comment on the task ident.
230{
231
232} # This is an inline comment on the task close brace.
233
234# This is a comment attached to the workflow keyword.
235workflow bar # This is an inline comment on the workflow ident.
236{
237 # This is attached to the call keyword.
238 call foo {}
239} # This is an inline comment on the workflow close brace.",
240 );
241
242 assert!(diagnostics.is_empty());
243 let document = Node::Ast(document.ast().into_v1().unwrap()).into_format_element();
244 let formatter = Formatter::default();
245 let result = formatter.format(&document);
246 match result {
247 Ok(s) => {
248 print!("{s}");
249 }
250 Err(err) => {
251 panic!("failed to format document: {err}");
252 }
253 }
254 }
255}