mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-30 23:27:22 +00:00
37 lines
1.4 KiB
Rust
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())
|
|
}
|
|
}
|