qt_build_utils/qml/
qmlls.rs

1// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use std::{
7    io,
8    path::{Path, PathBuf},
9};
10
11/// A helper for building QML Language Server configuration files
12#[derive(Default)]
13pub struct QmlLsIniBuilder {
14    build_dir: Option<PathBuf>,
15    no_cmake_calls: Option<bool>,
16}
17
18impl QmlLsIniBuilder {
19    /// Construct a [QmlLsIniBuilder]
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    /// Use the given build_dir
25    pub fn build_dir(mut self, build_dir: impl AsRef<Path>) -> Self {
26        self.build_dir = Some(build_dir.as_ref().to_path_buf());
27        self
28    }
29
30    /// Enable or disable cmake calls
31    pub fn no_cmake_calls(mut self, no_cmake_calls: bool) -> Self {
32        self.no_cmake_calls = Some(no_cmake_calls);
33        self
34    }
35
36    /// Write the resultant qmlls ini file contents
37    pub fn write(self, writer: &mut impl io::Write) -> io::Result<()> {
38        if self.build_dir.is_none() && self.no_cmake_calls.is_none() {
39            return Ok(());
40        }
41
42        writeln!(writer, "[General]")?;
43
44        if let Some(build_dir) = self.build_dir {
45            writeln!(
46                writer,
47                "buildDir=\"{}\"",
48                build_dir.to_string_lossy().escape_default()
49            )?;
50        }
51
52        if let Some(no_cmake_calls) = self.no_cmake_calls {
53            writeln!(writer, "no-cmake-calls={no_cmake_calls}",)?;
54        }
55
56        Ok(())
57    }
58}
59
60#[cfg(test)]
61mod test {
62    use super::*;
63
64    #[test]
65    fn qmlls() {
66        let mut result = Vec::new();
67        QmlLsIniBuilder::new()
68            .build_dir("/a/b/c")
69            .no_cmake_calls(true)
70            .write(&mut result)
71            .unwrap();
72        assert_eq!(
73            String::from_utf8(result).unwrap(),
74            "[General]
75buildDir=\"/a/b/c\"
76no-cmake-calls=true
77"
78        );
79    }
80}