config_definition.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![allow(clippy::field_reassign_with_default)]
  5. #[cfg(target_os = "linux")]
  6. use heck::KebabCase;
  7. use schemars::JsonSchema;
  8. use serde::{Deserialize, Serialize};
  9. use serde_json::Value as JsonValue;
  10. use serde_with::skip_serializing_none;
  11. use std::{collections::HashMap, path::PathBuf};
  12. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  13. #[serde(untagged)]
  14. pub enum BundleTarget {
  15. All(Vec<String>),
  16. One(String),
  17. }
  18. impl BundleTarget {
  19. #[allow(dead_code)]
  20. pub fn to_vec(&self) -> Vec<String> {
  21. match self {
  22. Self::All(list) => list.clone(),
  23. Self::One(i) => vec![i.clone()],
  24. }
  25. }
  26. }
  27. #[skip_serializing_none]
  28. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  29. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  30. pub struct DebConfig {
  31. pub depends: Option<Vec<String>>,
  32. #[serde(default)]
  33. pub use_bootstrapper: bool,
  34. #[serde(default)]
  35. pub files: HashMap<PathBuf, PathBuf>,
  36. }
  37. #[skip_serializing_none]
  38. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  39. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  40. pub struct MacConfig {
  41. pub frameworks: Option<Vec<String>>,
  42. pub minimum_system_version: Option<String>,
  43. pub exception_domain: Option<String>,
  44. pub license: Option<String>,
  45. #[serde(default)]
  46. pub use_bootstrapper: bool,
  47. pub signing_identity: Option<String>,
  48. pub entitlements: Option<String>,
  49. }
  50. fn default_language() -> String {
  51. "en-US".into()
  52. }
  53. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  54. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  55. pub struct WixConfig {
  56. /// App language. See https://docs.microsoft.com/en-us/windows/win32/msi/localizing-the-error-and-actiontext-tables.
  57. #[serde(default = "default_language")]
  58. pub language: String,
  59. pub template: Option<PathBuf>,
  60. #[serde(default)]
  61. pub fragment_paths: Vec<PathBuf>,
  62. #[serde(default)]
  63. pub component_group_refs: Vec<String>,
  64. #[serde(default)]
  65. pub component_refs: Vec<String>,
  66. #[serde(default)]
  67. pub feature_group_refs: Vec<String>,
  68. #[serde(default)]
  69. pub feature_refs: Vec<String>,
  70. #[serde(default)]
  71. pub merge_refs: Vec<String>,
  72. #[serde(default)]
  73. pub skip_webview_install: bool,
  74. /// Path to the license file.
  75. pub license: Option<String>,
  76. #[serde(default)]
  77. pub enable_elevated_update_task: bool,
  78. }
  79. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  80. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  81. pub struct WindowsConfig {
  82. pub digest_algorithm: Option<String>,
  83. pub certificate_thumbprint: Option<String>,
  84. pub timestamp_url: Option<String>,
  85. pub wix: Option<WixConfig>,
  86. }
  87. #[skip_serializing_none]
  88. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  89. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  90. pub struct PackageConfig {
  91. /// App name. Automatically converted to kebab-case on Linux.
  92. pub product_name: Option<String>,
  93. /// App version.
  94. pub version: Option<String>,
  95. }
  96. impl PackageConfig {
  97. #[allow(dead_code)]
  98. pub fn binary_name(&self) -> Option<String> {
  99. #[cfg(target_os = "linux")]
  100. {
  101. self.product_name.as_ref().map(|n| n.to_kebab_case())
  102. }
  103. #[cfg(not(target_os = "linux"))]
  104. {
  105. self.product_name.clone()
  106. }
  107. }
  108. }
  109. #[skip_serializing_none]
  110. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  111. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  112. pub struct BundleConfig {
  113. /// Whether we should build your app with tauri-bundler or plain `cargo build`
  114. #[serde(default)]
  115. pub active: bool,
  116. /// The bundle targets, currently supports ["deb", "app", "msi", "appimage", "dmg"] or "all"
  117. pub targets: Option<BundleTarget>,
  118. /// The app's identifier
  119. pub identifier: Option<String>,
  120. /// The app's icons
  121. pub icon: Option<Vec<String>>,
  122. /// App resources to bundle.
  123. /// Each resource is a path to a file or directory.
  124. /// Glob patterns are supported.
  125. pub resources: Option<Vec<String>>,
  126. pub copyright: Option<String>,
  127. pub category: Option<String>,
  128. pub short_description: Option<String>,
  129. pub long_description: Option<String>,
  130. #[serde(default)]
  131. pub deb: DebConfig,
  132. #[serde(rename = "macOS", default)]
  133. pub macos: MacConfig,
  134. pub external_bin: Option<Vec<String>>,
  135. #[serde(default)]
  136. pub windows: WindowsConfig,
  137. }
  138. /// A CLI argument definition
  139. #[skip_serializing_none]
  140. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  141. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  142. pub struct CliArg {
  143. /// The short version of the argument, without the preceding -.
  144. ///
  145. /// NOTE: Any leading - characters will be stripped, and only the first non - character will be used as the short version.
  146. pub short: Option<char>,
  147. /// The unique argument name
  148. pub name: String,
  149. /// The argument description which will be shown on the help information.
  150. /// Typically, this is a short (one line) description of the arg.
  151. pub description: Option<String>,
  152. /// The argument long description which will be shown on the help information.
  153. /// Typically this a more detailed (multi-line) message that describes the argument.
  154. pub long_description: Option<String>,
  155. /// Specifies that the argument takes a value at run time.
  156. ///
  157. /// NOTE: values for arguments may be specified in any of the following methods
  158. /// - Using a space such as -o value or --option value
  159. /// - Using an equals and no space such as -o=value or --option=value
  160. /// - Use a short and no space such as -ovalue
  161. pub takes_value: Option<bool>,
  162. /// Specifies that the argument may appear more than once.
  163. ///
  164. /// - For flags, this results in the number of occurrences of the flag being recorded.
  165. /// For example -ddd or -d -d -d would count as three occurrences.
  166. /// - For options there is a distinct difference in multiple occurrences vs multiple values.
  167. /// For example, --opt val1 val2 is one occurrence, but two values. Whereas --opt val1 --opt val2 is two occurrences.
  168. pub multiple: Option<bool>,
  169. /// specifies that the argument may appear more than once.
  170. pub multiple_occurrences: Option<bool>,
  171. ///
  172. pub number_of_values: Option<u64>,
  173. /// Specifies a list of possible values for this argument.
  174. /// At runtime, the CLI verifies that only one of the specified values was used, or fails with an error message.
  175. pub possible_values: Option<Vec<String>>,
  176. /// Specifies the minimum number of values for this argument.
  177. /// For example, if you had a -f <file> argument where you wanted at least 2 'files',
  178. /// you would set `minValues: 2`, and this argument would be satisfied if the user provided, 2 or more values.
  179. pub min_values: Option<u64>,
  180. /// Specifies the maximum number of values are for this argument.
  181. /// For example, if you had a -f <file> argument where you wanted up to 3 'files',
  182. /// you would set .max_values(3), and this argument would be satisfied if the user provided, 1, 2, or 3 values.
  183. pub max_values: Option<u64>,
  184. /// Sets whether or not the argument is required by default.
  185. ///
  186. /// - Required by default means it is required, when no other conflicting rules have been evaluated
  187. /// - Conflicting rules take precedence over being required.
  188. pub required: Option<bool>,
  189. /// Sets an arg that override this arg's required setting
  190. /// i.e. this arg will be required unless this other argument is present.
  191. pub required_unless_present: Option<String>,
  192. /// Sets args that override this arg's required setting
  193. /// i.e. this arg will be required unless all these other arguments are present.
  194. pub required_unless_present_all: Option<Vec<String>>,
  195. /// Sets args that override this arg's required setting
  196. /// i.e. this arg will be required unless at least one of these other arguments are present.
  197. pub required_unless_present_any: Option<Vec<String>>,
  198. /// Sets a conflicting argument by name
  199. /// i.e. when using this argument, the following argument can't be present and vice versa.
  200. pub conflicts_with: Option<String>,
  201. /// The same as conflictsWith but allows specifying multiple two-way conflicts per argument.
  202. pub conflicts_with_all: Option<Vec<String>>,
  203. /// Tets an argument by name that is required when this one is present
  204. /// i.e. when using this argument, the following argument must be present.
  205. pub requires: Option<String>,
  206. /// Sts multiple arguments by names that are required when this one is present
  207. /// i.e. when using this argument, the following arguments must be present.
  208. pub requires_all: Option<Vec<String>>,
  209. /// Allows a conditional requirement with the signature [arg, value]
  210. /// the requirement will only become valid if `arg`'s value equals `${value}`.
  211. pub requires_if: Option<Vec<String>>,
  212. /// Allows specifying that an argument is required conditionally with the signature [arg, value]
  213. /// the requirement will only become valid if the `arg`'s value equals `${value}`.
  214. pub required_if_eq: Option<Vec<String>>,
  215. /// Requires that options use the --option=val syntax
  216. /// i.e. an equals between the option and associated value.
  217. pub require_equals: Option<bool>,
  218. /// The positional argument index, starting at 1.
  219. ///
  220. /// The index refers to position according to other positional argument.
  221. /// It does not define position in the argument list as a whole. When utilized with multiple=true,
  222. /// only the last positional argument may be defined as multiple (i.e. the one with the highest index).
  223. pub index: Option<u64>,
  224. }
  225. /// describes a CLI configuration
  226. #[skip_serializing_none]
  227. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  228. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  229. pub struct CliConfig {
  230. /// command description which will be shown on the help information
  231. description: Option<String>,
  232. /// command long description which will be shown on the help information
  233. long_description: Option<String>,
  234. /// adds additional help information to be displayed in addition to auto-generated help
  235. /// this information is displayed before the auto-generated help information.
  236. /// this is often used for header information
  237. before_help: Option<String>,
  238. /// adds additional help information to be displayed in addition to auto-generated help
  239. /// this information is displayed after the auto-generated help information
  240. /// this is often used to describe how to use the arguments, or caveats to be noted.
  241. after_help: Option<String>,
  242. /// list of args for the command
  243. args: Option<Vec<CliArg>>,
  244. /// list of subcommands of this command.
  245. ///
  246. /// subcommands are effectively sub-apps, because they can contain their own arguments, subcommands, usage, etc.
  247. /// they also function just like the app command, in that they get their own auto generated help and usage
  248. subcommands: Option<HashMap<String, CliConfig>>,
  249. }
  250. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  251. #[serde(untagged)]
  252. pub enum Port {
  253. /// Port with a numeric value.
  254. Value(u16),
  255. /// Random port.
  256. Random,
  257. }
  258. /// The window configuration object.
  259. #[skip_serializing_none]
  260. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  261. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  262. pub struct WindowConfig {
  263. /// The window identifier.
  264. pub label: Option<String>,
  265. /// The window webview URL.
  266. pub url: Option<String>,
  267. /// Whether the file drop is enabled or not on the webview. By default it is enabled.
  268. ///
  269. /// Disabling it is required to use drag and drop on the frontend on Windows.
  270. #[serde(default = "default_file_drop_enabled")]
  271. pub file_drop_enabled: bool,
  272. /// Whether or not the window starts centered or not.
  273. #[serde(default)]
  274. pub center: bool,
  275. /// The horizontal position of the window's top left corner
  276. pub x: Option<f64>,
  277. /// The vertical position of the window's top left corner
  278. pub y: Option<f64>,
  279. /// The window width.
  280. pub width: Option<f64>,
  281. /// The window height.
  282. pub height: Option<f64>,
  283. /// The min window width.
  284. pub min_width: Option<f64>,
  285. /// The min window height.
  286. pub min_height: Option<f64>,
  287. /// The max window width.
  288. pub max_width: Option<f64>,
  289. /// The max window height.
  290. pub max_height: Option<f64>,
  291. /// Whether the window is resizable or not.
  292. #[serde(default)]
  293. pub resizable: bool,
  294. /// The window title.
  295. pub title: Option<String>,
  296. /// Whether the window starts as fullscreen or not.
  297. #[serde(default)]
  298. pub fullscreen: bool,
  299. /// Whether the window will be initially hidden or focused.
  300. #[serde(default = "default_focus")]
  301. pub focus: bool,
  302. /// Whether the window is transparent or not.
  303. #[serde(default)]
  304. pub transparent: bool,
  305. /// Whether the window is maximized or not.
  306. #[serde(default)]
  307. pub maximized: bool,
  308. /// Whether the window is visible or not.
  309. #[serde(default = "default_visible")]
  310. pub visible: bool,
  311. /// Whether the window should have borders and bars.
  312. #[serde(default = "default_decorations")]
  313. pub decorations: bool,
  314. /// Whether the window should always be on top of other windows.
  315. #[serde(default)]
  316. pub always_on_top: bool,
  317. /// Whether or not the window icon should be added to the taskbar.
  318. #[serde(default)]
  319. pub skip_taskbar: bool,
  320. }
  321. fn default_focus() -> bool {
  322. true
  323. }
  324. fn default_visible() -> bool {
  325. true
  326. }
  327. fn default_decorations() -> bool {
  328. true
  329. }
  330. fn default_file_drop_enabled() -> bool {
  331. true
  332. }
  333. #[skip_serializing_none]
  334. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  335. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  336. pub struct SecurityConfig {
  337. pub csp: Option<String>,
  338. }
  339. pub trait Allowlist {
  340. fn to_features(&self) -> Vec<&str>;
  341. }
  342. macro_rules! check_feature {
  343. ($self:ident, $features:ident, $flag:ident, $feature_name: expr) => {
  344. if $self.$flag {
  345. $features.push($feature_name)
  346. }
  347. };
  348. }
  349. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  350. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  351. pub struct FsAllowlistConfig {
  352. #[serde(default)]
  353. pub all: bool,
  354. #[serde(default)]
  355. pub read_text_file: bool,
  356. #[serde(default)]
  357. pub read_binary_file: bool,
  358. #[serde(default)]
  359. pub write_file: bool,
  360. #[serde(default)]
  361. pub write_binary_file: bool,
  362. #[serde(default)]
  363. pub read_dir: bool,
  364. #[serde(default)]
  365. pub copy_file: bool,
  366. #[serde(default)]
  367. pub create_dir: bool,
  368. #[serde(default)]
  369. pub remove_dir: bool,
  370. #[serde(default)]
  371. pub remove_file: bool,
  372. #[serde(default)]
  373. pub rename_file: bool,
  374. }
  375. impl Allowlist for FsAllowlistConfig {
  376. fn to_features(&self) -> Vec<&str> {
  377. if self.all {
  378. vec!["fs-all"]
  379. } else {
  380. let mut features = Vec::new();
  381. check_feature!(self, features, read_text_file, "fs-read-text-file");
  382. check_feature!(self, features, read_binary_file, "fs-read-binary-file");
  383. check_feature!(self, features, write_file, "fs-write-file");
  384. check_feature!(self, features, write_binary_file, "fs-write-binary-file");
  385. check_feature!(self, features, read_dir, "fs-read-dir");
  386. check_feature!(self, features, copy_file, "fs-copy-file");
  387. check_feature!(self, features, create_dir, "fs-create-dir");
  388. check_feature!(self, features, remove_dir, "fs-remove-dir");
  389. check_feature!(self, features, remove_file, "fs-remove-file");
  390. check_feature!(self, features, rename_file, "fs-rename-file");
  391. features
  392. }
  393. }
  394. }
  395. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  396. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  397. pub struct WindowAllowlistConfig {
  398. #[serde(default)]
  399. pub all: bool,
  400. #[serde(default)]
  401. pub create: bool,
  402. }
  403. impl Allowlist for WindowAllowlistConfig {
  404. fn to_features(&self) -> Vec<&str> {
  405. if self.all {
  406. vec!["window-all"]
  407. } else {
  408. let mut features = Vec::new();
  409. check_feature!(self, features, create, "window-create");
  410. features
  411. }
  412. }
  413. }
  414. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  415. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  416. pub struct ShellAllowlistConfig {
  417. #[serde(default)]
  418. pub all: bool,
  419. #[serde(default)]
  420. pub execute: bool,
  421. #[serde(default)]
  422. pub open: bool,
  423. }
  424. impl Allowlist for ShellAllowlistConfig {
  425. fn to_features(&self) -> Vec<&str> {
  426. if self.all {
  427. vec!["shell-all"]
  428. } else {
  429. let mut features = Vec::new();
  430. check_feature!(self, features, execute, "shell-execute");
  431. check_feature!(self, features, open, "shell-open");
  432. features
  433. }
  434. }
  435. }
  436. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  437. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  438. pub struct DialogAllowlistConfig {
  439. #[serde(default)]
  440. pub all: bool,
  441. #[serde(default)]
  442. pub open: bool,
  443. #[serde(default)]
  444. pub save: bool,
  445. }
  446. impl Allowlist for DialogAllowlistConfig {
  447. fn to_features(&self) -> Vec<&str> {
  448. if self.all {
  449. vec!["dialog-all"]
  450. } else {
  451. let mut features = Vec::new();
  452. check_feature!(self, features, open, "dialog-open");
  453. check_feature!(self, features, save, "dialog-save");
  454. features
  455. }
  456. }
  457. }
  458. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  459. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  460. pub struct HttpAllowlistConfig {
  461. #[serde(default)]
  462. pub all: bool,
  463. #[serde(default)]
  464. pub request: bool,
  465. }
  466. impl Allowlist for HttpAllowlistConfig {
  467. fn to_features(&self) -> Vec<&str> {
  468. if self.all {
  469. vec!["http-all"]
  470. } else {
  471. let mut features = Vec::new();
  472. check_feature!(self, features, request, "http-request");
  473. features
  474. }
  475. }
  476. }
  477. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  478. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  479. pub struct NotificationAllowlistConfig {
  480. #[serde(default)]
  481. pub all: bool,
  482. }
  483. impl Allowlist for NotificationAllowlistConfig {
  484. fn to_features(&self) -> Vec<&str> {
  485. if self.all {
  486. vec!["notification-all"]
  487. } else {
  488. vec![]
  489. }
  490. }
  491. }
  492. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  493. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  494. pub struct GlobalShortcutAllowlistConfig {
  495. #[serde(default)]
  496. pub all: bool,
  497. }
  498. impl Allowlist for GlobalShortcutAllowlistConfig {
  499. fn to_features(&self) -> Vec<&str> {
  500. if self.all {
  501. vec!["global-shortcut-all"]
  502. } else {
  503. vec![]
  504. }
  505. }
  506. }
  507. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  508. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  509. pub struct OsAllowlistConfig {
  510. #[serde(default)]
  511. pub all: bool,
  512. }
  513. impl Allowlist for OsAllowlistConfig {
  514. fn to_features(&self) -> Vec<&str> {
  515. if self.all {
  516. vec!["os-all"]
  517. } else {
  518. vec![]
  519. }
  520. }
  521. }
  522. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  523. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  524. pub struct PathAllowlistConfig {
  525. #[serde(default)]
  526. pub all: bool,
  527. }
  528. impl Allowlist for PathAllowlistConfig {
  529. fn to_features(&self) -> Vec<&str> {
  530. if self.all {
  531. vec!["path-all"]
  532. } else {
  533. vec![]
  534. }
  535. }
  536. }
  537. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  538. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  539. pub struct AllowlistConfig {
  540. #[serde(default)]
  541. pub all: bool,
  542. #[serde(default)]
  543. pub fs: FsAllowlistConfig,
  544. #[serde(default)]
  545. pub window: WindowAllowlistConfig,
  546. #[serde(default)]
  547. pub shell: ShellAllowlistConfig,
  548. #[serde(default)]
  549. pub dialog: DialogAllowlistConfig,
  550. #[serde(default)]
  551. pub http: HttpAllowlistConfig,
  552. #[serde(default)]
  553. pub notification: NotificationAllowlistConfig,
  554. #[serde(default)]
  555. pub global_shortcut: GlobalShortcutAllowlistConfig,
  556. #[serde(default)]
  557. pub os: OsAllowlistConfig,
  558. #[serde(default)]
  559. pub path: PathAllowlistConfig,
  560. }
  561. impl Allowlist for AllowlistConfig {
  562. fn to_features(&self) -> Vec<&str> {
  563. if self.all {
  564. vec!["api-all"]
  565. } else {
  566. let mut features = Vec::new();
  567. features.extend(self.fs.to_features());
  568. features.extend(self.window.to_features());
  569. features.extend(self.shell.to_features());
  570. features.extend(self.dialog.to_features());
  571. features.extend(self.http.to_features());
  572. features.extend(self.notification.to_features());
  573. features.extend(self.global_shortcut.to_features());
  574. features.extend(self.os.to_features());
  575. features.extend(self.path.to_features());
  576. features
  577. }
  578. }
  579. }
  580. /// The Tauri configuration object.
  581. #[skip_serializing_none]
  582. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  583. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  584. pub struct TauriConfig {
  585. /// The windows configuration.
  586. #[serde(default)]
  587. pub windows: Vec<WindowConfig>,
  588. /// The CLI configuration.
  589. pub cli: Option<CliConfig>,
  590. /// The bundler configuration.
  591. #[serde(default)]
  592. pub bundle: BundleConfig,
  593. #[serde(default)]
  594. allowlist: AllowlistConfig,
  595. pub security: Option<SecurityConfig>,
  596. /// The updater configuration.
  597. #[serde(default = "default_updater")]
  598. pub updater: UpdaterConfig,
  599. /// Configuration for app system tray.
  600. pub system_tray: Option<SystemTrayConfig>,
  601. }
  602. impl TauriConfig {
  603. #[allow(dead_code)]
  604. pub fn features(&self) -> Vec<&str> {
  605. self.allowlist.to_features()
  606. }
  607. }
  608. #[skip_serializing_none]
  609. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  610. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  611. pub struct UpdaterConfig {
  612. /// Whether the updater is active or not.
  613. #[serde(default)]
  614. pub active: bool,
  615. /// Display built-in dialog or use event system if disabled.
  616. #[serde(default = "default_dialog")]
  617. pub dialog: Option<bool>,
  618. /// The updater endpoints.
  619. pub endpoints: Option<Vec<String>>,
  620. /// Optional pubkey.
  621. pub pubkey: Option<String>,
  622. }
  623. #[skip_serializing_none]
  624. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  625. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  626. pub struct SystemTrayConfig {
  627. /// Path to the icon to use on the system tray.
  628. ///
  629. /// It is forced to be a `.png` file on Linux and macOS, and a `.ico` file on Windows.
  630. pub icon_path: PathBuf,
  631. /// A Boolean value that determines whether the image represents a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) image on macOS.
  632. #[serde(default)]
  633. pub icon_as_template: bool,
  634. }
  635. // We enable the unnecessary_wraps because we need
  636. // to use an Option for dialog otherwise the CLI schema will mark
  637. // the dialog as a required field which is not as we default it to true.
  638. #[allow(clippy::unnecessary_wraps)]
  639. fn default_dialog() -> Option<bool> {
  640. Some(true)
  641. }
  642. /// The `dev_path` and `dist_dir` options.
  643. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  644. #[serde(untagged, deny_unknown_fields)]
  645. pub enum AppUrl {
  646. /// The app's external URL, or the path to the directory containing the app assets.
  647. Url(String),
  648. /// An array of files to embed on the app.
  649. Files(Vec<PathBuf>),
  650. }
  651. impl std::fmt::Display for AppUrl {
  652. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  653. match self {
  654. Self::Url(url) => write!(f, "{}", url),
  655. Self::Files(files) => write!(f, "{}", serde_json::to_string(files).unwrap()),
  656. }
  657. }
  658. }
  659. /// The Build configuration object.
  660. #[skip_serializing_none]
  661. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  662. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  663. pub struct BuildConfig {
  664. /// The binary used to build and run the application.
  665. pub runner: Option<String>,
  666. /// The path or URL to use on development.
  667. #[serde(default = "default_dev_path")]
  668. pub dev_path: AppUrl,
  669. /// the path to the app's dist dir. This path must contain your index.html file.
  670. #[serde(default = "default_dist_dir")]
  671. pub dist_dir: AppUrl,
  672. /// a shell command to run before `tauri dev` kicks in
  673. pub before_dev_command: Option<String>,
  674. /// a shell command to run before `tauri build` kicks in
  675. pub before_build_command: Option<String>,
  676. /// features passed to `cargo` commands
  677. pub features: Option<Vec<String>>,
  678. /// Whether we should inject the Tauri API on `window.__TAURI__` or not.
  679. #[serde(default)]
  680. pub with_global_tauri: bool,
  681. }
  682. fn default_dev_path() -> AppUrl {
  683. AppUrl::Url("".to_string())
  684. }
  685. fn default_dist_dir() -> AppUrl {
  686. AppUrl::Url("../dist".to_string())
  687. }
  688. type JsonObject = HashMap<String, JsonValue>;
  689. /// The tauri.conf.json mapper.
  690. #[skip_serializing_none]
  691. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  692. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  693. pub struct Config {
  694. /// Package settings.
  695. #[serde(default)]
  696. pub package: PackageConfig,
  697. /// The Tauri configuration.
  698. #[serde(default)]
  699. pub tauri: TauriConfig,
  700. /// The build configuration.
  701. #[serde(default = "default_build")]
  702. pub build: BuildConfig,
  703. /// The plugins config.
  704. #[serde(default)]
  705. pub plugins: HashMap<String, JsonObject>,
  706. }
  707. fn default_build() -> BuildConfig {
  708. BuildConfig {
  709. runner: None,
  710. dev_path: default_dev_path(),
  711. dist_dir: default_dist_dir(),
  712. before_dev_command: None,
  713. before_build_command: None,
  714. features: None,
  715. with_global_tauri: false,
  716. }
  717. }
  718. fn default_updater() -> UpdaterConfig {
  719. UpdaterConfig {
  720. active: false,
  721. dialog: Some(true),
  722. endpoints: None,
  723. pubkey: None,
  724. }
  725. }