|
| 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 | +/// An individial <file> line within a [QResourceBuilder] |
| 7 | +pub struct QResourceFile { |
| 8 | + alias: Option<String>, |
| 9 | + // TODO: compression |
| 10 | + // TODO: empty |
| 11 | + path: String, |
| 12 | +} |
| 13 | + |
| 14 | +impl QResourceFile { |
| 15 | + /// Construct a [QResourceFile] |
| 16 | + fn new(path: String) -> Self { |
| 17 | + Self { alias: None, path } |
| 18 | + } |
| 19 | + |
| 20 | + /// Specify an alias for the [QResourceFile] |
| 21 | + pub fn with_alias(mut self, alias: String) -> Self { |
| 22 | + self.alias = Some(alias); |
| 23 | + self |
| 24 | + } |
| 25 | + |
| 26 | + fn build(self) -> String { |
| 27 | + // TODO: add slashes / sanitize |
| 28 | + let alias = self |
| 29 | + .alias |
| 30 | + .map(|alias| format!("alias=\"{alias}\"")) |
| 31 | + .unwrap_or_default(); |
| 32 | + let path = self.path; |
| 33 | + format!("<file {alias}>{path}</file>") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// A <qresource> block within a [QResourceBuilder] |
| 38 | +pub struct QResource { |
| 39 | + language: Option<String>, |
| 40 | + prefix: Option<String>, |
| 41 | + files: Vec<QResourceFile>, |
| 42 | +} |
| 43 | + |
| 44 | +impl Default for QResource { |
| 45 | + fn default() -> Self { |
| 46 | + Self::new() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl QResource { |
| 51 | + /// Construct a [QResource] |
| 52 | + fn new() -> Self { |
| 53 | + Self { |
| 54 | + language: None, |
| 55 | + prefix: None, |
| 56 | + files: vec![], |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Add a [QResourceFile] with the given path |
| 61 | + pub fn file(self, path: String) -> Self { |
| 62 | + self.file_with_opts(path, |file| file) |
| 63 | + } |
| 64 | + |
| 65 | + /// Add a [QResourceFile] with the given path and apply additional options |
| 66 | + pub fn file_with_opts( |
| 67 | + mut self, |
| 68 | + path: String, |
| 69 | + opts: impl FnOnce(QResourceFile) -> QResourceFile, |
| 70 | + ) -> Self { |
| 71 | + self.files.push(opts(QResourceFile::new(path))); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + /// Specify a language for the <qresource> |
| 76 | + pub fn with_language(mut self, language: String) -> Self { |
| 77 | + self.language = Some(language); |
| 78 | + self |
| 79 | + } |
| 80 | + |
| 81 | + /// Specify a prefix for the <qresource> |
| 82 | + pub fn with_prefix(mut self, prefix: String) -> Self { |
| 83 | + self.prefix = Some(prefix); |
| 84 | + self |
| 85 | + } |
| 86 | + |
| 87 | + fn build(self) -> String { |
| 88 | + // TODO: add slashes / sanitise |
| 89 | + let language = self |
| 90 | + .language |
| 91 | + .map(|language| format!("language=\"{language}\"")) |
| 92 | + .unwrap_or_default(); |
| 93 | + let prefix = self |
| 94 | + .prefix |
| 95 | + .map(|prefix| format!("prefix=\"{prefix}\"")) |
| 96 | + .unwrap_or_default(); |
| 97 | + let files = self |
| 98 | + .files |
| 99 | + .into_iter() |
| 100 | + .map(QResourceFile::build) |
| 101 | + .collect::<Vec<_>>() |
| 102 | + .join(""); |
| 103 | + format!("<qresource {language} {prefix}>{files}</qresource>") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// A helper for building Qt resource collection files |
| 108 | +pub struct QResourceBuilder { |
| 109 | + resources: Vec<QResource>, |
| 110 | +} |
| 111 | + |
| 112 | +impl Default for QResourceBuilder { |
| 113 | + fn default() -> Self { |
| 114 | + Self::new() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl QResourceBuilder { |
| 119 | + /// Construct a [QResourceBuilder] |
| 120 | + pub fn new() -> Self { |
| 121 | + Self { resources: vec![] } |
| 122 | + } |
| 123 | + |
| 124 | + /// Add a [QResource] to the builder with given options |
| 125 | + pub fn with_resource_opts(mut self, opts: impl FnOnce(QResource) -> QResource) -> Self { |
| 126 | + self.resources.push(opts(QResource::new())); |
| 127 | + self |
| 128 | + } |
| 129 | + |
| 130 | + /// Convert to a string representation |
| 131 | + pub fn build(self) -> String { |
| 132 | + let resources = self |
| 133 | + .resources |
| 134 | + .into_iter() |
| 135 | + .map(QResource::build) |
| 136 | + .collect::<Vec<_>>() |
| 137 | + .join(""); |
| 138 | + format!("<RCC>{resources}</RCC>") |
| 139 | + } |
| 140 | +} |
0 commit comments