config_definition.rs 25 KB

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