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::DefaultOption(_) => v1::expr::format_default_option(self, stream),
58 AstNode::DivisionExpr(_) => v1::expr::format_division_expr(self, stream),
59 AstNode::EqualityExpr(_) => v1::expr::format_equality_expr(self, stream),
60 AstNode::ExponentiationExpr(_) => {
61 v1::expr::format_exponentiation_expr(self, stream)
62 }
63 AstNode::GreaterEqualExpr(_) => v1::expr::format_greater_equal_expr(self, stream),
64 AstNode::GreaterExpr(_) => v1::expr::format_greater_expr(self, stream),
65 AstNode::IfExpr(_) => v1::expr::format_if_expr(self, stream),
66 AstNode::ImportAlias(_) => v1::import::format_import_alias(self, stream),
67 AstNode::ImportStatement(_) => v1::import::format_import_statement(self, stream),
68 AstNode::IndexExpr(_) => v1::expr::format_index_expr(self, stream),
69 AstNode::InequalityExpr(_) => v1::expr::format_inequality_expr(self, stream),
70 AstNode::InputSection(_) => v1::format_input_section(self, stream),
71 AstNode::LessEqualExpr(_) => v1::expr::format_less_equal_expr(self, stream),
72 AstNode::LessExpr(_) => v1::expr::format_less_expr(self, stream),
73 AstNode::LiteralArray(_) => v1::expr::format_literal_array(self, stream),
74 AstNode::LiteralBoolean(_) => v1::expr::format_literal_boolean(self, stream),
75 AstNode::LiteralFloat(_) => v1::expr::format_literal_float(self, stream),
76 AstNode::LiteralHints(_) => v1::format_literal_hints(self, stream),
77 AstNode::LiteralHintsItem(_) => v1::format_literal_hints_item(self, stream),
78 AstNode::LiteralInput(_) => v1::format_literal_input(self, stream),
79 AstNode::LiteralInputItem(_) => v1::format_literal_input_item(self, stream),
80 AstNode::LiteralInteger(_) => v1::expr::format_literal_integer(self, stream),
81 AstNode::LiteralMap(_) => v1::expr::format_literal_map(self, stream),
82 AstNode::LiteralMapItem(_) => v1::expr::format_literal_map_item(self, stream),
83 AstNode::LiteralNone(_) => v1::expr::format_literal_none(self, stream),
84 AstNode::LiteralNull(_) => v1::meta::format_literal_null(self, stream),
85 AstNode::LiteralObject(_) => v1::expr::format_literal_object(self, stream),
86 AstNode::LiteralObjectItem(_) => v1::expr::format_literal_object_item(self, stream),
87 AstNode::LiteralOutput(_) => v1::format_literal_output(self, stream),
88 AstNode::LiteralOutputItem(_) => v1::format_literal_output_item(self, stream),
89 AstNode::LiteralPair(_) => v1::expr::format_literal_pair(self, stream),
90 AstNode::LiteralString(_) => v1::expr::format_literal_string(self, stream),
91 AstNode::LiteralStruct(_) => v1::r#struct::format_literal_struct(self, stream),
92 AstNode::LiteralStructItem(_) => {
93 v1::r#struct::format_literal_struct_item(self, stream)
94 }
95 AstNode::LogicalAndExpr(_) => v1::expr::format_logical_and_expr(self, stream),
96 AstNode::LogicalNotExpr(_) => v1::expr::format_logical_not_expr(self, stream),
97 AstNode::LogicalOrExpr(_) => v1::expr::format_logical_or_expr(self, stream),
98 AstNode::MapType(_) => v1::decl::format_map_type(self, stream),
99 AstNode::MetadataArray(_) => v1::meta::format_metadata_array(self, stream),
100 AstNode::MetadataObject(_) => v1::meta::format_metadata_object(self, stream),
101 AstNode::MetadataObjectItem(_) => {
102 v1::meta::format_metadata_object_item(self, stream)
103 }
104 AstNode::MetadataSection(_) => v1::meta::format_metadata_section(self, stream),
105 AstNode::ModuloExpr(_) => v1::expr::format_modulo_expr(self, stream),
106 AstNode::MultiplicationExpr(_) => {
107 v1::expr::format_multiplication_expr(self, stream)
108 }
109 AstNode::NameRefExpr(_) => v1::expr::format_name_ref_expr(self, stream),
110 AstNode::NegationExpr(_) => v1::expr::format_negation_expr(self, stream),
111 AstNode::OutputSection(_) => v1::format_output_section(self, stream),
112 AstNode::PairType(_) => v1::decl::format_pair_type(self, stream),
113 AstNode::ObjectType(_) => v1::decl::format_object_type(self, stream),
114 AstNode::ParameterMetadataSection(_) => {
115 v1::meta::format_parameter_metadata_section(self, stream)
116 }
117 AstNode::ParenthesizedExpr(_) => v1::expr::format_parenthesized_expr(self, stream),
118 AstNode::Placeholder(_) => v1::expr::format_placeholder(self, stream),
119 AstNode::PrimitiveType(_) => v1::decl::format_primitive_type(self, stream),
120 AstNode::RequirementsItem(_) => v1::task::format_requirements_item(self, stream),
121 AstNode::RequirementsSection(_) => {
122 v1::task::format_requirements_section(self, stream)
123 }
124 AstNode::RuntimeItem(_) => v1::task::format_runtime_item(self, stream),
125 AstNode::RuntimeSection(_) => v1::task::format_runtime_section(self, stream),
126 AstNode::ScatterStatement(_) => {
127 v1::workflow::format_scatter_statement(self, stream)
128 }
129 AstNode::SepOption(_) => v1::expr::format_sep_option(self, stream),
130 AstNode::StructDefinition(_) => {
131 v1::r#struct::format_struct_definition(self, stream)
132 }
133 AstNode::SubtractionExpr(_) => v1::expr::format_subtraction_expr(self, stream),
134 AstNode::TaskDefinition(_) => v1::task::format_task_definition(self, stream),
135 AstNode::TaskHintsItem(_) => v1::task::format_task_hints_item(self, stream),
136 AstNode::TaskHintsSection(_) => v1::task::format_task_hints_section(self, stream),
137 AstNode::TrueFalseOption(_) => v1::expr::format_true_false_option(self, stream),
138 AstNode::TypeRef(_) => v1::decl::format_type_ref(self, stream),
139 AstNode::UnboundDecl(_) => v1::decl::format_unbound_decl(self, stream),
140 AstNode::VersionStatement(_) => v1::format_version_statement(self, stream),
141 AstNode::WorkflowDefinition(_) => {
142 v1::workflow::format_workflow_definition(self, stream)
143 }
144 AstNode::WorkflowHintsArray(_) => {
145 v1::workflow::format_workflow_hints_array(self, stream)
146 }
147 AstNode::WorkflowHintsItem(_) => {
148 v1::workflow::format_workflow_hints_item(self, stream)
149 }
150 AstNode::WorkflowHintsObject(_) => {
151 v1::workflow::format_workflow_hints_object(self, stream)
152 }
153 AstNode::WorkflowHintsObjectItem(_) => {
154 v1::workflow::format_workflow_hints_object_item(self, stream)
155 }
156 AstNode::WorkflowHintsSection(_) => {
157 v1::workflow::format_workflow_hints_section(self, stream)
158 }
159 },
160 Element::Token(token) => {
161 stream.push_ast_token(token);
162 }
163 }
164 }
165}
166
167#[derive(Debug, Default)]
169pub struct Formatter {
170 config: Config,
172}
173
174impl Formatter {
175 pub fn new(config: Config) -> Self {
177 Self { config }
178 }
179
180 pub fn config(&self) -> &Config {
182 &self.config
183 }
184
185 pub fn format<W: Writable>(&self, element: W) -> std::result::Result<String, std::fmt::Error> {
187 let mut result = String::new();
188
189 for token in self.to_stream(element) {
190 write!(result, "{token}", token = token.display(self.config()))?;
191 }
192
193 Ok(result)
194 }
195
196 fn to_stream<W: Writable>(&self, element: W) -> TokenStream<PostToken> {
198 let mut stream = TokenStream::default();
199 element.write(&mut stream);
200
201 let mut postprocessor = Postprocessor::default();
202 postprocessor.run(stream, self.config())
203 }
204}
205
206#[cfg(test)]
207mod tests {
208 use wdl_ast::Document;
209 use wdl_ast::Node;
210
211 use crate::Formatter;
212 use crate::element::node::AstNodeFormatExt as _;
213
214 #[test]
215 fn smoke() {
216 let (document, diagnostics) = Document::parse(
217 "## WDL
218version 1.2 # This is a comment attached to the version.
219
220# This is a comment attached to the task keyword.
221task foo # This is an inline comment on the task ident.
222{
223
224} # This is an inline comment on the task close brace.
225
226# This is a comment attached to the workflow keyword.
227workflow bar # This is an inline comment on the workflow ident.
228{
229 # This is attached to the call keyword.
230 call foo {}
231} # This is an inline comment on the workflow close brace.",
232 );
233
234 assert!(diagnostics.is_empty());
235 let document = Node::Ast(document.ast().into_v1().unwrap()).into_format_element();
236 let formatter = Formatter::default();
237 let result = formatter.format(&document);
238 match result {
239 Ok(s) => {
240 print!("{s}");
241 }
242 Err(err) => {
243 panic!("failed to format document: {err}");
244 }
245 }
246 }
247}