slint/tools/fmt/writer.rs
Matej Zajo Kralik 231b742a6c
fmt tool improvements (#929)
Improve formatting, expression support, tests

Add sixtyfps-fmt readme
2022-02-11 18:52:15 +01:00

37 lines
1.4 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
use i_slint_compiler::parser::SyntaxToken;
use std::io::Write;
/// The idea is that each token need to go through this, either with no changes,
/// or with a new content.
pub(crate) trait TokenWriter {
/// Write token to the writer without any change.
fn no_change(&mut self, token: SyntaxToken) -> std::io::Result<()>;
/// Write just contents into the writer (replacing token).
fn with_new_content(&mut self, token: SyntaxToken, contents: &str) -> std::io::Result<()>;
/// Write contents and then the token to the writer.
fn insert_before(&mut self, token: SyntaxToken, contents: &str) -> std::io::Result<()>;
}
/// Just write the token stream to a file
pub(crate) struct FileWriter<'a, W> {
pub(super) file: &'a mut W,
}
impl<'a, W: Write> TokenWriter for FileWriter<'a, W> {
fn no_change(&mut self, token: SyntaxToken) -> std::io::Result<()> {
self.file.write_all(token.text().as_bytes())
}
fn with_new_content(&mut self, _token: SyntaxToken, contents: &str) -> std::io::Result<()> {
self.file.write_all(contents.as_bytes())
}
fn insert_before(&mut self, token: SyntaxToken, contents: &str) -> std::io::Result<()> {
self.file.write_all(contents.as_bytes())?;
self.file.write_all(token.text().as_bytes())
}
}