tauri_api/config.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
use serde::de::{Deserializer, Error as DeError, Visitor};
use serde::Deserialize;
use serde_json::Value as JsonValue;
use once_cell::sync::OnceCell;
use std::collections::HashMap;
static CONFIG: OnceCell<Config> = OnceCell::new();
/// The window configuration object.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(tag = "window", rename_all = "camelCase")]
pub struct WindowConfig {
/// The window width.
#[serde(default = "default_width")]
pub width: i32,
/// The window height.
#[serde(default = "default_height")]
pub height: i32,
/// Whether the window is resizable or not.
#[serde(default = "default_resizable")]
pub resizable: bool,
/// The window title.
#[serde(default = "default_title")]
pub title: String,
/// Whether the window starts as fullscreen or not.
#[serde(default)]
pub fullscreen: bool,
}
fn default_width() -> i32 {
800
}
fn default_height() -> i32 {
600
}
fn default_resizable() -> bool {
true
}
fn default_title() -> String {
"Tauri App".to_string()
}
fn default_window() -> WindowConfig {
WindowConfig {
width: default_width(),
height: default_height(),
resizable: default_resizable(),
title: default_title(),
fullscreen: false,
}
}
/// The embedded server port.
#[derive(PartialEq, Debug, Deserialize)]
pub enum Port {
/// Port with a numeric value.
Value(u16),
/// Random port.
Random,
}
/// The embeddedServer configuration object.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(tag = "embeddedServer", rename_all = "camelCase")]
pub struct EmbeddedServerConfig {
/// The embedded server host.
#[serde(default = "default_host")]
pub host: String,
/// The embedded server port.
/// If it's `random`, we'll generate one at runtime.
#[serde(default = "default_port", deserialize_with = "port_deserializer")]
pub port: Port,
}
fn default_host() -> String {
"http://127.0.0.1".to_string()
}
fn port_deserializer<'de, D>(deserializer: D) -> Result<Port, D::Error>
where
D: Deserializer<'de>,
{
struct PortDeserializer;
impl<'de> Visitor<'de> for PortDeserializer {
type Value = Port;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a port number or the 'random' string")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: DeError,
{
if value != "random" {
Err(DeError::custom(
"expected a 'random' string or a port number",
))
} else {
Ok(Port::Random)
}
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: DeError,
{
Ok(Port::Value(value as u16))
}
}
deserializer.deserialize_any(PortDeserializer {})
}
fn default_port() -> Port {
Port::Random
}
fn default_embedded_server() -> EmbeddedServerConfig {
EmbeddedServerConfig {
host: default_host(),
port: default_port(),
}
}
/// A CLI argument definition
#[derive(PartialEq, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct CliArg {
/// The short version of the argument, without the preceding -.
///
/// NOTE: Any leading - characters will be stripped, and only the first non - character will be used as the short version.
pub short: Option<char>,
/// The unique argument name
pub name: String,
/// The argument description which will be shown on the help information.
/// Typically, this is a short (one line) description of the arg.
pub description: Option<String>,
/// The argument long description which will be shown on the help information.
/// Typically this a more detailed (multi-line) message that describes the argument.
pub long_description: Option<String>,
/// Specifies that the argument takes a value at run time.
///
/// NOTE: values for arguments may be specified in any of the following methods
/// - Using a space such as -o value or --option value
/// - Using an equals and no space such as -o=value or --option=value
/// - Use a short and no space such as -ovalue
pub takes_value: Option<bool>,
/// Specifies that the argument may appear more than once.
///
/// - For flags, this results in the number of occurrences of the flag being recorded.
/// For example -ddd or -d -d -d would count as three occurrences.
/// - For options there is a distinct difference in multiple occurrences vs multiple values.
/// For example, --opt val1 val2 is one occurrence, but two values. Whereas --opt val1 --opt val2 is two occurrences.
pub multiple: Option<bool>,
///
pub multiple_occurrences: Option<bool>,
///
pub number_of_values: Option<u64>,
/// Specifies a list of possible values for this argument.
/// At runtime, the CLI verifies that only one of the specified values was used, or fails with an error message.
pub possible_values: Option<Vec<String>>,
/// Specifies the minimum number of values for this argument.
/// For example, if you had a -f <file> argument where you wanted at least 2 'files',
/// you would set `minValues: 2`, and this argument would be satisfied if the user provided, 2 or more values.
pub min_values: Option<u64>,
/// Specifies the maximum number of values are for this argument.
/// For example, if you had a -f <file> argument where you wanted up to 3 'files',
/// you would set .max_values(3), and this argument would be satisfied if the user provided, 1, 2, or 3 values.
pub max_values: Option<u64>,
/// Sets whether or not the argument is required by default.
///
/// - Required by default means it is required, when no other conflicting rules have been evaluated
/// - Conflicting rules take precedence over being required.
pub required: Option<bool>,
/// Sets an arg that override this arg's required setting
/// i.e. this arg will be required unless this other argument is present.
pub required_unless: Option<String>,
/// Sets args that override this arg's required setting
/// i.e. this arg will be required unless all these other arguments are present.
pub required_unless_all: Option<Vec<String>>,
/// Sets args that override this arg's required setting
/// i.e. this arg will be required unless at least one of these other arguments are present.
pub required_unless_one: Option<Vec<String>>,
/// Sets a conflicting argument by name
/// i.e. when using this argument, the following argument can't be present and vice versa.
pub conflicts_with: Option<String>,
/// The same as conflictsWith but allows specifying multiple two-way conflicts per argument.
pub conflicts_with_all: Option<Vec<String>>,
/// Tets an argument by name that is required when this one is present
/// i.e. when using this argument, the following argument must be present.
pub requires: Option<String>,
/// Sts multiple arguments by names that are required when this one is present
/// i.e. when using this argument, the following arguments must be present.
pub requires_all: Option<Vec<String>>,
/// Allows a conditional requirement with the signature [arg, value]
/// the requirement will only become valid if `arg`'s value equals `${value}`.
pub requires_if: Option<Vec<String>>,
/// Allows specifying that an argument is required conditionally with the signature [arg, value]
/// the requirement will only become valid if the `arg`'s value equals `${value}`.
pub required_if: Option<Vec<String>>,
/// Requires that options use the --option=val syntax
/// i.e. an equals between the option and associated value.
pub require_equals: Option<bool>,
/// The positional argument index, starting at 1.
///
/// The index refers to position according to other positional argument.
/// It does not define position in the argument list as a whole. When utilized with multiple=true,
/// only the last positional argument may be defined as multiple (i.e. the one with the highest index).
pub index: Option<u64>,
}
/// The CLI root command definition.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(tag = "cli", rename_all = "camelCase")]
pub struct CliConfig {
description: Option<String>,
long_description: Option<String>,
before_help: Option<String>,
after_help: Option<String>,
args: Option<Vec<CliArg>>,
subcommands: Option<HashMap<String, CliConfig>>,
}
impl CliConfig {
/// List of args for the command
pub fn args(&self) -> Option<&Vec<CliArg>> {
self.args.as_ref()
}
/// List of subcommands of this command
pub fn subcommands(&self) -> Option<&HashMap<String, CliConfig>> {
self.subcommands.as_ref()
}
/// Command description which will be shown on the help information.
pub fn description(&self) -> Option<&String> {
self.description.as_ref()
}
/// Command long description which will be shown on the help information.
pub fn long_description(&self) -> Option<&String> {
self.description.as_ref()
}
/// Adds additional help information to be displayed in addition to auto-generated help.
/// This information is displayed before the auto-generated help information.
/// This is often used for header information.
pub fn before_help(&self) -> Option<&String> {
self.before_help.as_ref()
}
/// Adds additional help information to be displayed in addition to auto-generated help.
/// This information is displayed after the auto-generated help information.
/// This is often used to describe how to use the arguments, or caveats to be noted.
pub fn after_help(&self) -> Option<&String> {
self.after_help.as_ref()
}
}
/// The bundler configuration object.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(tag = "bundle", rename_all = "camelCase")]
pub struct BundleConfig {
/// The bundle identifier.
pub identifier: String,
}
fn default_bundle() -> BundleConfig {
BundleConfig {
identifier: String::from(""),
}
}
/// The Tauri configuration object.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(tag = "tauri", rename_all = "camelCase")]
pub struct TauriConfig {
/// The window configuration.
#[serde(default = "default_window")]
pub window: WindowConfig,
/// The embeddedServer configuration.
#[serde(default = "default_embedded_server")]
pub embedded_server: EmbeddedServerConfig,
/// The CLI configuration.
#[serde(default)]
pub cli: Option<CliConfig>,
/// The bundler configuration.
#[serde(default = "default_bundle")]
pub bundle: BundleConfig,
}
/// The Build configuration object.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(tag = "build", rename_all = "camelCase")]
pub struct BuildConfig {
/// the devPath config.
#[serde(default = "default_dev_path")]
pub dev_path: String,
}
fn default_dev_path() -> String {
"".to_string()
}
type JsonObject = HashMap<String, JsonValue>;
/// The tauri.conf.json mapper.
#[derive(PartialEq, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Config {
/// The Tauri configuration.
#[serde(default = "default_tauri")]
pub tauri: TauriConfig,
/// The build configuration.
#[serde(default = "default_build")]
pub build: BuildConfig,
/// The plugins config.
#[serde(default)]
plugins: HashMap<String, JsonObject>,
}
impl Config {
/// Gets a plugin configuration.
pub fn plugin_config<S: AsRef<str>>(&self, plugin_name: S) -> Option<&JsonObject> {
self.plugins.get(plugin_name.as_ref())
}
}
fn default_tauri() -> TauriConfig {
TauriConfig {
window: default_window(),
embedded_server: default_embedded_server(),
cli: None,
bundle: default_bundle(),
}
}
fn default_build() -> BuildConfig {
BuildConfig {
dev_path: default_dev_path(),
}
}
/// Gets the static parsed config from `tauri.conf.json`.
pub fn get() -> crate::Result<&'static Config> {
if let Some(config) = CONFIG.get() {
return Ok(config);
}
let config: Config = match option_env!("TAURI_CONFIG") {
Some(config) => serde_json::from_str(config).expect("failed to parse TAURI_CONFIG env"),
None => {
let config = include_str!(concat!(env!("OUT_DIR"), "/tauri.conf.json"));
serde_json::from_str(&config).expect("failed to read tauri.conf.json")
}
};
CONFIG
.set(config)
.map_err(|_| anyhow::anyhow!("failed to set CONFIG"))?;
let config = CONFIG.get().unwrap();
Ok(config)
}
#[cfg(test)]
mod test {
use super::*;
// generate a test_config based on the test fixture
fn create_test_config() -> Config {
let mut subcommands = std::collections::HashMap::new();
subcommands.insert(
"update".to_string(),
CliConfig {
description: Some("Updates the app".to_string()),
long_description: None,
before_help: None,
after_help: None,
args: Some(vec![CliArg {
short: Some('b'),
name: "background".to_string(),
description: Some("Update in background".to_string()),
..Default::default()
}]),
subcommands: None,
},
);
Config {
tauri: TauriConfig {
window: WindowConfig {
width: 800,
height: 600,
resizable: true,
title: String::from("Tauri API Validation"),
fullscreen: false,
},
embedded_server: EmbeddedServerConfig {
host: String::from("http://127.0.0.1"),
port: Port::Random,
},
bundle: BundleConfig {
identifier: String::from("com.tauri.communication"),
},
cli: Some(CliConfig {
description: Some("Tauri communication example".to_string()),
long_description: None,
before_help: None,
after_help: None,
args: Some(vec![
CliArg {
short: Some('c'),
name: "config".to_string(),
takes_value: Some(true),
description: Some("Config path".to_string()),
..Default::default()
},
CliArg {
short: Some('t'),
name: "theme".to_string(),
takes_value: Some(true),
description: Some("App theme".to_string()),
possible_values: Some(vec![
"light".to_string(),
"dark".to_string(),
"system".to_string(),
]),
..Default::default()
},
CliArg {
short: Some('v'),
name: "verbose".to_string(),
multiple_occurrences: Some(true),
description: Some("Verbosity level".to_string()),
..Default::default()
},
]),
subcommands: Some(subcommands),
}),
},
build: BuildConfig {
dev_path: String::from("../dist"),
},
plugins: Default::default(),
}
}
#[test]
// test the get function. Will only resolve to true if the TAURI_CONFIG variable is set properly to the fixture.
fn test_get() {
// get test_config
let test_config = create_test_config();
// call get();
let config = get();
// check to see if there is an OK or Err, on Err fail test.
match config {
// On Ok, check that the config is the same as the test config.
Ok(c) => {
println!("{:?}", c);
assert_eq!(c, &test_config)
}
Err(e) => panic!("get config failed: {:?}", e.to_string()),
}
}
#[test]
// test all of the default functions
fn test_defaults() {
// get default tauri config
let t_config = default_tauri();
// get default build config
let b_config = default_build();
// get default dev path
let d_path = default_dev_path();
// get default embedded server
let de_server = default_embedded_server();
// get default window
let d_window = default_window();
// get default title
let d_title = default_title();
// get default bundle
let d_bundle = default_bundle();
// create a tauri config.
let tauri = TauriConfig {
window: WindowConfig {
width: 800,
height: 600,
resizable: true,
title: String::from("Tauri App"),
fullscreen: false,
},
embedded_server: EmbeddedServerConfig {
host: String::from("http://127.0.0.1"),
port: Port::Random,
},
bundle: BundleConfig {
identifier: String::from(""),
},
cli: None,
};
// create a build config
let build = BuildConfig {
dev_path: String::from(""),
};
// test the configs
assert_eq!(t_config, tauri);
assert_eq!(b_config, build);
assert_eq!(de_server, tauri.embedded_server);
assert_eq!(d_bundle, tauri.bundle);
assert_eq!(d_path, String::from(""));
assert_eq!(d_title, tauri.window.title);
assert_eq!(d_window, tauri.window);
}
}