1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
3
4use i_slint_compiler::parser::SyntaxToken;
5use std::io::Write;
6
7/// The idea is that each token need to go through this, either with no changes,
8/// or with a new content.
9pub trait TokenWriter {
10 /// Write token to the writer without any change.
11 fn no_change(&mut self, token: SyntaxToken) -> std::io::Result<()>;
12
13 /// Write just contents into the writer (replacing token).
14 fn with_new_content(&mut self, token: SyntaxToken, contents: &str) -> std::io::Result<()>;
15
16 /// Write contents and then the token to the writer.
17 fn insert_before(&mut self, token: SyntaxToken, contents: &str) -> std::io::Result<()>;
18}
19
20/// Just write the token stream to a file
21pub struct FileWriter<'a, W> {
22 pub file: &'a mut W,
23}
24
25impl<'a, W: Write> TokenWriter for FileWriter<'a, W> {
26 fn no_change(&mut self, token: SyntaxToken) -> std::io::Result<()> {
27 self.file.write_all(buf:token.text().as_bytes())
28 }
29
30 fn with_new_content(&mut self, _token: SyntaxToken, contents: &str) -> std::io::Result<()> {
31 self.file.write_all(buf:contents.as_bytes())
32 }
33
34 fn insert_before(&mut self, token: SyntaxToken, contents: &str) -> std::io::Result<()> {
35 self.file.write_all(buf:contents.as_bytes())?;
36 self.file.write_all(buf:token.text().as_bytes())
37 }
38}
39