config_definition.rs 19 KB

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