|
| 1 | +// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> |
| 2 | +// SPDX-FileContributor: Andrew Hayzen <[email protected]> |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 5 | + |
| 6 | +use std::{ |
| 7 | + io, |
| 8 | + path::{Path, PathBuf}, |
| 9 | +}; |
| 10 | + |
| 11 | +/// A helper for building QML Language Server configuration files |
| 12 | +#[derive(Default)] |
| 13 | +pub struct QmlLsIniBuilder { |
| 14 | + build_dir: Option<PathBuf>, |
| 15 | + no_cmake_calls: Option<bool>, |
| 16 | +} |
| 17 | + |
| 18 | +impl 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!( |
| 54 | + writer, |
| 55 | + "no-cmake-calls={}", |
| 56 | + if no_cmake_calls { "true" } else { "false" } |
| 57 | + )?; |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod test { |
| 66 | + use super::*; |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn qmlls() { |
| 70 | + let mut result = Vec::new(); |
| 71 | + QmlLsIniBuilder::new() |
| 72 | + .build_dir("/a/b/c") |
| 73 | + .no_cmake_calls(true) |
| 74 | + .write(&mut result) |
| 75 | + .unwrap(); |
| 76 | + assert_eq!( |
| 77 | + String::from_utf8(result).unwrap(), |
| 78 | + "[General] |
| 79 | +buildDir=\"/a/b/c\" |
| 80 | +no-cmake-calls=true |
| 81 | +" |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments