config_definition.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #![allow(clippy::field_reassign_with_default)]
  2. use schemars::JsonSchema;
  3. use serde::{Deserialize, Serialize};
  4. use serde_json::Value as JsonValue;
  5. use serde_with::skip_serializing_none;
  6. use std::collections::HashMap;
  7. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  8. #[serde(untagged)]
  9. pub enum BundleTarget {
  10. All(Vec<String>),
  11. One(String),
  12. }
  13. #[skip_serializing_none]
  14. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  15. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  16. pub struct DebConfig {
  17. pub depends: Option<Vec<String>>,
  18. #[serde(default)]
  19. pub use_bootstrapper: bool,
  20. }
  21. #[skip_serializing_none]
  22. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  23. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  24. pub struct MacConfig {
  25. pub frameworks: Option<Vec<String>>,
  26. pub minimum_system_version: Option<String>,
  27. pub exception_domain: Option<String>,
  28. pub license: Option<String>,
  29. #[serde(default)]
  30. pub use_bootstrapper: bool,
  31. }
  32. #[skip_serializing_none]
  33. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  34. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  35. pub struct PackageConfig {
  36. /// App name. Automatically converted to kebab-case on Linux.
  37. pub product_name: Option<String>,
  38. /// App version.
  39. pub version: Option<String>,
  40. }
  41. #[skip_serializing_none]
  42. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  43. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  44. pub struct BundleConfig {
  45. /// Whether we should build your app with tauri-bundler or plain `cargo build`
  46. pub active: bool,
  47. /// The bundle targets, currently supports ["deb", "app", "msi", "appimage", "dmg"] or "all"
  48. pub targets: Option<BundleTarget>,
  49. /// The app's identifier
  50. pub identifier: Option<String>,
  51. /// The app's icons
  52. pub icon: Option<Vec<String>>,
  53. /// App resources to bundle.
  54. /// Each resource is a path to a file or directory.
  55. /// Glob patterns are supported.
  56. pub resources: Option<Vec<String>>,
  57. pub copyright: Option<String>,
  58. pub category: Option<String>,
  59. pub short_description: Option<String>,
  60. pub long_description: Option<String>,
  61. #[serde(default)]
  62. pub deb: DebConfig,
  63. #[serde(rename = "macOS", default)]
  64. pub macos: MacConfig,
  65. pub external_bin: Option<Vec<String>>,
  66. }
  67. /// A CLI argument definition
  68. #[skip_serializing_none]
  69. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  70. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  71. pub struct CliArg {
  72. /// The short version of the argument, without the preceding -.
  73. ///
  74. /// NOTE: Any leading - characters will be stripped, and only the first non - character will be used as the short version.
  75. pub short: Option<char>,
  76. /// The unique argument name
  77. pub name: String,
  78. /// The argument description which will be shown on the help information.
  79. /// Typically, this is a short (one line) description of the arg.
  80. pub description: Option<String>,
  81. /// The argument long description which will be shown on the help information.
  82. /// Typically this a more detailed (multi-line) message that describes the argument.
  83. pub long_description: Option<String>,
  84. /// Specifies that the argument takes a value at run time.
  85. ///
  86. /// NOTE: values for arguments may be specified in any of the following methods
  87. /// - Using a space such as -o value or --option value
  88. /// - Using an equals and no space such as -o=value or --option=value
  89. /// - Use a short and no space such as -ovalue
  90. pub takes_value: Option<bool>,
  91. /// Specifies that the argument may appear more than once.
  92. ///
  93. /// - For flags, this results in the number of occurrences of the flag being recorded.
  94. /// For example -ddd or -d -d -d would count as three occurrences.
  95. /// - For options there is a distinct difference in multiple occurrences vs multiple values.
  96. /// For example, --opt val1 val2 is one occurrence, but two values. Whereas --opt val1 --opt val2 is two occurrences.
  97. pub multiple: Option<bool>,
  98. /// specifies that the argument may appear more than once.
  99. pub multiple_occurrences: Option<bool>,
  100. ///
  101. pub number_of_values: Option<u64>,
  102. /// Specifies a list of possible values for this argument.
  103. /// At runtime, the CLI verifies that only one of the specified values was used, or fails with an error message.
  104. pub possible_values: Option<Vec<String>>,
  105. /// Specifies the minimum number of values for this argument.
  106. /// For example, if you had a -f <file> argument where you wanted at least 2 'files',
  107. /// you would set `minValues: 2`, and this argument would be satisfied if the user provided, 2 or more values.
  108. pub min_values: Option<u64>,
  109. /// Specifies the maximum number of values are for this argument.
  110. /// For example, if you had a -f <file> argument where you wanted up to 3 'files',
  111. /// you would set .max_values(3), and this argument would be satisfied if the user provided, 1, 2, or 3 values.
  112. pub max_values: Option<u64>,
  113. /// Sets whether or not the argument is required by default.
  114. ///
  115. /// - Required by default means it is required, when no other conflicting rules have been evaluated
  116. /// - Conflicting rules take precedence over being required.
  117. pub required: Option<bool>,
  118. /// Sets an arg that override this arg's required setting
  119. /// i.e. this arg will be required unless this other argument is present.
  120. pub required_unless_present: Option<String>,
  121. /// Sets args that override this arg's required setting
  122. /// i.e. this arg will be required unless all these other arguments are present.
  123. pub required_unless_present_all: Option<Vec<String>>,
  124. /// Sets args that override this arg's required setting
  125. /// i.e. this arg will be required unless at least one of these other arguments are present.
  126. pub required_unless_present_any: Option<Vec<String>>,
  127. /// Sets a conflicting argument by name
  128. /// i.e. when using this argument, the following argument can't be present and vice versa.
  129. pub conflicts_with: Option<String>,
  130. /// The same as conflictsWith but allows specifying multiple two-way conflicts per argument.
  131. pub conflicts_with_all: Option<Vec<String>>,
  132. /// Tets an argument by name that is required when this one is present
  133. /// i.e. when using this argument, the following argument must be present.
  134. pub requires: Option<String>,
  135. /// Sts multiple arguments by names that are required when this one is present
  136. /// i.e. when using this argument, the following arguments must be present.
  137. pub requires_all: Option<Vec<String>>,
  138. /// Allows a conditional requirement with the signature [arg, value]
  139. /// the requirement will only become valid if `arg`'s value equals `${value}`.
  140. pub requires_if: Option<Vec<String>>,
  141. /// Allows specifying that an argument is required conditionally with the signature [arg, value]
  142. /// the requirement will only become valid if the `arg`'s value equals `${value}`.
  143. pub required_if_eq: Option<Vec<String>>,
  144. /// Requires that options use the --option=val syntax
  145. /// i.e. an equals between the option and associated value.
  146. pub require_equals: Option<bool>,
  147. /// The positional argument index, starting at 1.
  148. ///
  149. /// The index refers to position according to other positional argument.
  150. /// It does not define position in the argument list as a whole. When utilized with multiple=true,
  151. /// only the last positional argument may be defined as multiple (i.e. the one with the highest index).
  152. pub index: Option<u64>,
  153. }
  154. /// describes a CLI configuration
  155. #[skip_serializing_none]
  156. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  157. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  158. pub struct CliConfig {
  159. /// command description which will be shown on the help information
  160. description: Option<String>,
  161. /// command long description which will be shown on the help information
  162. long_description: Option<String>,
  163. /// adds additional help information to be displayed in addition to auto-generated help
  164. /// this information is displayed before the auto-generated help information.
  165. /// this is often used for header information
  166. before_help: Option<String>,
  167. /// adds additional help information to be displayed in addition to auto-generated help
  168. /// this information is displayed after the auto-generated help information
  169. /// this is often used to describe how to use the arguments, or caveats to be noted.
  170. after_help: Option<String>,
  171. /// list of args for the command
  172. args: Option<Vec<CliArg>>,
  173. /// list of subcommands of this command.
  174. ///
  175. /// subcommands are effectively sub-apps, because they can contain their own arguments, subcommands, usage, etc.
  176. /// they also function just like the app command, in that they get their own auto generated help and usage
  177. subcommands: Option<HashMap<String, CliConfig>>,
  178. }
  179. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  180. #[serde(untagged)]
  181. pub enum Port {
  182. /// Port with a numeric value.
  183. Value(u16),
  184. /// Random port.
  185. Random,
  186. }
  187. /// The window configuration object.
  188. #[skip_serializing_none]
  189. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  190. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  191. pub struct WindowConfig {
  192. /// The window identifier.
  193. pub label: Option<String>,
  194. /// The window webview URL.
  195. pub url: Option<String>,
  196. /// The horizontal position of the window's top left corner
  197. pub x: Option<f64>,
  198. /// The vertical position of the window's top left corner
  199. pub y: Option<f64>,
  200. /// The window width.
  201. pub width: Option<f64>,
  202. /// The window height.
  203. pub height: Option<f64>,
  204. /// The min window width.
  205. pub min_width: Option<f64>,
  206. /// The min window height.
  207. pub min_height: Option<f64>,
  208. /// The max window width.
  209. pub max_width: Option<f64>,
  210. /// The max window height.
  211. pub max_height: Option<f64>,
  212. /// Whether the window is resizable or not.
  213. #[serde(default)]
  214. pub resizable: bool,
  215. /// The window title.
  216. pub title: Option<String>,
  217. /// Whether the window starts as fullscreen or not.
  218. #[serde(default)]
  219. pub fullscreen: bool,
  220. /// Whether the window is transparent or not.
  221. #[serde(default)]
  222. pub transparent: bool,
  223. /// Whether the window is maximized or not.
  224. #[serde(default)]
  225. pub maximized: bool,
  226. /// Whether the window is visible or not.
  227. #[serde(default = "default_visible")]
  228. pub visible: bool,
  229. /// Whether the window should have borders and bars.
  230. #[serde(default = "default_decorations")]
  231. pub decorations: bool,
  232. /// Whether the window should always be on top of other windows.
  233. #[serde(default)]
  234. pub always_on_top: bool,
  235. }
  236. fn default_visible() -> bool {
  237. true
  238. }
  239. fn default_decorations() -> bool {
  240. true
  241. }
  242. #[skip_serializing_none]
  243. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  244. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  245. pub struct SecurityConfig {
  246. pub csp: Option<String>,
  247. }
  248. trait Allowlist {
  249. fn to_features(&self) -> Vec<&str>;
  250. }
  251. macro_rules! check_feature {
  252. ($self:ident, $features:ident, $flag:ident, $feature_name: expr) => {
  253. if $self.$flag {
  254. $features.push($feature_name)
  255. }
  256. };
  257. }
  258. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  259. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  260. struct FsAllowlistConfig {
  261. #[serde(default)]
  262. all: bool,
  263. #[serde(default)]
  264. read_text_file: bool,
  265. #[serde(default)]
  266. read_binary_file: bool,
  267. #[serde(default)]
  268. write_file: bool,
  269. #[serde(default)]
  270. write_binary_file: bool,
  271. #[serde(default)]
  272. read_dir: bool,
  273. #[serde(default)]
  274. copy_file: bool,
  275. #[serde(default)]
  276. create_dir: bool,
  277. #[serde(default)]
  278. remove_dir: bool,
  279. #[serde(default)]
  280. remove_file: bool,
  281. #[serde(default)]
  282. rename_file: bool,
  283. #[serde(default)]
  284. path: bool,
  285. }
  286. impl Allowlist for FsAllowlistConfig {
  287. fn to_features(&self) -> Vec<&str> {
  288. if self.all {
  289. vec!["fs-all"]
  290. } else {
  291. let mut features = Vec::new();
  292. check_feature!(self, features, read_text_file, "fs-read-text-file");
  293. check_feature!(self, features, read_binary_file, "fs-read-binary-file");
  294. check_feature!(self, features, write_file, "fs-write-file");
  295. check_feature!(self, features, write_binary_file, "fs-write-binary-file");
  296. check_feature!(self, features, read_dir, "fs-read-dir");
  297. check_feature!(self, features, copy_file, "fs-copy-file");
  298. check_feature!(self, features, create_dir, "fs-create-dir");
  299. check_feature!(self, features, remove_dir, "fs-remove-dir");
  300. check_feature!(self, features, remove_file, "fs-remove-file");
  301. check_feature!(self, features, rename_file, "fs-rename-file");
  302. check_feature!(self, features, path, "fs-path");
  303. features
  304. }
  305. }
  306. }
  307. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  308. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  309. struct WindowAllowlistConfig {
  310. #[serde(default)]
  311. all: bool,
  312. #[serde(default)]
  313. create: bool,
  314. }
  315. impl Allowlist for WindowAllowlistConfig {
  316. fn to_features(&self) -> Vec<&str> {
  317. if self.all {
  318. vec!["window-all"]
  319. } else {
  320. let mut features = Vec::new();
  321. check_feature!(self, features, create, "window-create");
  322. features
  323. }
  324. }
  325. }
  326. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  327. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  328. struct ShellAllowlistConfig {
  329. #[serde(default)]
  330. all: bool,
  331. #[serde(default)]
  332. execute: bool,
  333. #[serde(default)]
  334. open: bool,
  335. }
  336. impl Allowlist for ShellAllowlistConfig {
  337. fn to_features(&self) -> Vec<&str> {
  338. if self.all {
  339. vec!["shell-all"]
  340. } else {
  341. let mut features = Vec::new();
  342. check_feature!(self, features, execute, "shell-execute");
  343. check_feature!(self, features, open, "shell-open");
  344. features
  345. }
  346. }
  347. }
  348. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  349. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  350. struct DialogAllowlistConfig {
  351. #[serde(default)]
  352. all: bool,
  353. #[serde(default)]
  354. open: bool,
  355. #[serde(default)]
  356. save: bool,
  357. }
  358. impl Allowlist for DialogAllowlistConfig {
  359. fn to_features(&self) -> Vec<&str> {
  360. if self.all {
  361. vec!["dialog-all"]
  362. } else {
  363. let mut features = Vec::new();
  364. check_feature!(self, features, open, "dialog-open");
  365. check_feature!(self, features, save, "dialog-save");
  366. features
  367. }
  368. }
  369. }
  370. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  371. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  372. struct HttpAllowlistConfig {
  373. #[serde(default)]
  374. all: bool,
  375. #[serde(default)]
  376. request: bool,
  377. }
  378. impl Allowlist for HttpAllowlistConfig {
  379. fn to_features(&self) -> Vec<&str> {
  380. if self.all {
  381. vec!["http-all"]
  382. } else {
  383. let mut features = Vec::new();
  384. check_feature!(self, features, request, "http-request");
  385. features
  386. }
  387. }
  388. }
  389. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  390. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  391. struct NotificationAllowlistConfig {
  392. #[serde(default)]
  393. all: bool,
  394. }
  395. impl Allowlist for NotificationAllowlistConfig {
  396. fn to_features(&self) -> Vec<&str> {
  397. if self.all {
  398. vec!["notification-all"]
  399. } else {
  400. vec![]
  401. }
  402. }
  403. }
  404. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  405. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  406. struct GlobalShortcutAllowlistConfig {
  407. #[serde(default)]
  408. all: bool,
  409. }
  410. impl Allowlist for GlobalShortcutAllowlistConfig {
  411. fn to_features(&self) -> Vec<&str> {
  412. if self.all {
  413. vec!["global-shortcut-all"]
  414. } else {
  415. vec![]
  416. }
  417. }
  418. }
  419. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  420. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  421. struct AllowlistConfig {
  422. #[serde(default)]
  423. all: bool,
  424. #[serde(default)]
  425. fs: FsAllowlistConfig,
  426. #[serde(default)]
  427. window: WindowAllowlistConfig,
  428. #[serde(default)]
  429. shell: ShellAllowlistConfig,
  430. #[serde(default)]
  431. dialog: DialogAllowlistConfig,
  432. #[serde(default)]
  433. http: HttpAllowlistConfig,
  434. #[serde(default)]
  435. notification: NotificationAllowlistConfig,
  436. #[serde(default)]
  437. global_shortcut: GlobalShortcutAllowlistConfig,
  438. }
  439. impl Allowlist for AllowlistConfig {
  440. fn to_features(&self) -> Vec<&str> {
  441. if self.all {
  442. vec!["api-all"]
  443. } else {
  444. let mut features = Vec::new();
  445. features.extend(self.fs.to_features());
  446. features.extend(self.window.to_features());
  447. features.extend(self.shell.to_features());
  448. features.extend(self.dialog.to_features());
  449. features.extend(self.http.to_features());
  450. features.extend(self.notification.to_features());
  451. features.extend(self.global_shortcut.to_features());
  452. features
  453. }
  454. }
  455. }
  456. /// The Tauri configuration object.
  457. #[skip_serializing_none]
  458. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  459. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  460. pub struct TauriConfig {
  461. /// The windows configuration.
  462. #[serde(default)]
  463. pub windows: Vec<WindowConfig>,
  464. /// The CLI configuration.
  465. pub cli: Option<CliConfig>,
  466. /// The bundler configuration.
  467. #[serde(default)]
  468. pub bundle: BundleConfig,
  469. #[serde(default)]
  470. allowlist: AllowlistConfig,
  471. pub security: Option<SecurityConfig>,
  472. }
  473. impl TauriConfig {
  474. #[allow(dead_code)]
  475. pub fn features(&self) -> Vec<&str> {
  476. self.allowlist.to_features()
  477. }
  478. }
  479. /// The Build configuration object.
  480. #[skip_serializing_none]
  481. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  482. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  483. pub struct BuildConfig {
  484. /// the app's dev server URL, or the path to the directory containing an index.html file
  485. #[serde(default = "default_dev_path")]
  486. pub dev_path: String,
  487. /// the path to the app's dist dir. This path must contain your index.html file.
  488. #[serde(default = "default_dist_dir")]
  489. pub dist_dir: String,
  490. /// a shell command to run before `tauri dev` kicks in
  491. pub before_dev_command: Option<String>,
  492. /// a shell command to run before `tauri build` kicks in
  493. pub before_build_command: Option<String>,
  494. /// Whether we should inject the Tauri API on `window.__TAURI__` or not.
  495. #[serde(default)]
  496. pub with_global_tauri: bool,
  497. }
  498. fn default_dev_path() -> String {
  499. "".to_string()
  500. }
  501. fn default_dist_dir() -> String {
  502. "../dist".to_string()
  503. }
  504. type JsonObject = HashMap<String, JsonValue>;
  505. /// The tauri.conf.json mapper.
  506. #[skip_serializing_none]
  507. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  508. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  509. pub struct Config {
  510. /// Package settings.
  511. #[serde(default)]
  512. pub package: PackageConfig,
  513. /// The Tauri configuration.
  514. #[serde(default)]
  515. pub tauri: TauriConfig,
  516. /// The build configuration.
  517. #[serde(default = "default_build")]
  518. pub build: BuildConfig,
  519. /// The plugins config.
  520. #[serde(default)]
  521. pub plugins: HashMap<String, JsonObject>,
  522. }
  523. fn default_build() -> BuildConfig {
  524. BuildConfig {
  525. dev_path: default_dev_path(),
  526. dist_dir: default_dist_dir(),
  527. before_dev_command: None,
  528. before_build_command: None,
  529. with_global_tauri: false,
  530. }
  531. }