config_definition.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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)]
  222. pub visible: bool,
  223. /// Whether the window should have borders and bars.
  224. #[serde(default)]
  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. #[skip_serializing_none]
  231. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  232. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  233. pub struct SecurityConfig {
  234. csp: Option<String>,
  235. }
  236. trait Allowlist {
  237. fn to_features(&self) -> Vec<&str>;
  238. }
  239. macro_rules! check_feature {
  240. ($self:ident, $features:ident, $flag:ident, $feature_name: expr) => {
  241. if $self.$flag {
  242. $features.push($feature_name)
  243. }
  244. };
  245. }
  246. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  247. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  248. struct FsAllowlistConfig {
  249. #[serde(default)]
  250. all: bool,
  251. #[serde(default)]
  252. read_text_file: bool,
  253. #[serde(default)]
  254. read_binary_file: bool,
  255. #[serde(default)]
  256. write_file: bool,
  257. #[serde(default)]
  258. write_binary_file: bool,
  259. #[serde(default)]
  260. read_dir: bool,
  261. #[serde(default)]
  262. copy_file: bool,
  263. #[serde(default)]
  264. create_dir: bool,
  265. #[serde(default)]
  266. remove_dir: bool,
  267. #[serde(default)]
  268. remove_file: bool,
  269. #[serde(default)]
  270. rename_file: bool,
  271. #[serde(default)]
  272. path: bool,
  273. }
  274. impl Allowlist for FsAllowlistConfig {
  275. fn to_features(&self) -> Vec<&str> {
  276. if self.all {
  277. vec!["fs-all"]
  278. } else {
  279. let mut features = Vec::new();
  280. check_feature!(self, features, read_text_file, "fs-read-text-file");
  281. check_feature!(self, features, read_binary_file, "fs-read-binary-file");
  282. check_feature!(self, features, write_file, "fs-write-file");
  283. check_feature!(self, features, write_binary_file, "fs-write-binary-file");
  284. check_feature!(self, features, read_dir, "fs-read-dir");
  285. check_feature!(self, features, copy_file, "fs-copy-file");
  286. check_feature!(self, features, create_dir, "fs-create-dir");
  287. check_feature!(self, features, remove_dir, "fs-remove-dir");
  288. check_feature!(self, features, remove_file, "fs-remove-file");
  289. check_feature!(self, features, rename_file, "fs-rename-file");
  290. check_feature!(self, features, path, "fs-path");
  291. features
  292. }
  293. }
  294. }
  295. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  296. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  297. struct WindowAllowlistConfig {
  298. #[serde(default)]
  299. all: bool,
  300. #[serde(default)]
  301. create: bool,
  302. }
  303. impl Allowlist for WindowAllowlistConfig {
  304. fn to_features(&self) -> Vec<&str> {
  305. if self.all {
  306. vec!["window-all"]
  307. } else {
  308. let mut features = Vec::new();
  309. check_feature!(self, features, create, "window-create");
  310. features
  311. }
  312. }
  313. }
  314. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  315. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  316. struct ShellAllowlistConfig {
  317. #[serde(default)]
  318. all: bool,
  319. #[serde(default)]
  320. execute: bool,
  321. #[serde(default)]
  322. open: bool,
  323. }
  324. impl Allowlist for ShellAllowlistConfig {
  325. fn to_features(&self) -> Vec<&str> {
  326. if self.all {
  327. vec!["shell-all"]
  328. } else {
  329. let mut features = Vec::new();
  330. check_feature!(self, features, execute, "shell-execute");
  331. check_feature!(self, features, open, "shell-open");
  332. features
  333. }
  334. }
  335. }
  336. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  337. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  338. struct DialogAllowlistConfig {
  339. #[serde(default)]
  340. all: bool,
  341. #[serde(default)]
  342. open: bool,
  343. #[serde(default)]
  344. save: bool,
  345. }
  346. impl Allowlist for DialogAllowlistConfig {
  347. fn to_features(&self) -> Vec<&str> {
  348. if self.all {
  349. vec!["dialog-all"]
  350. } else {
  351. let mut features = Vec::new();
  352. check_feature!(self, features, open, "dialog-open");
  353. check_feature!(self, features, save, "dialog-save");
  354. features
  355. }
  356. }
  357. }
  358. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  359. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  360. struct HttpAllowlistConfig {
  361. #[serde(default)]
  362. all: bool,
  363. #[serde(default)]
  364. request: bool,
  365. }
  366. impl Allowlist for HttpAllowlistConfig {
  367. fn to_features(&self) -> Vec<&str> {
  368. if self.all {
  369. vec!["http-all"]
  370. } else {
  371. let mut features = Vec::new();
  372. check_feature!(self, features, request, "http-request");
  373. features
  374. }
  375. }
  376. }
  377. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  378. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  379. struct NotificationAllowlistConfig {
  380. #[serde(default)]
  381. all: bool,
  382. }
  383. impl Allowlist for NotificationAllowlistConfig {
  384. fn to_features(&self) -> Vec<&str> {
  385. if self.all {
  386. vec!["notification-all"]
  387. } else {
  388. vec![]
  389. }
  390. }
  391. }
  392. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  393. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  394. struct GlobalShortcutAllowlistConfig {
  395. #[serde(default)]
  396. all: bool,
  397. }
  398. impl Allowlist for GlobalShortcutAllowlistConfig {
  399. fn to_features(&self) -> Vec<&str> {
  400. if self.all {
  401. vec!["global-shortcut-all"]
  402. } else {
  403. vec![]
  404. }
  405. }
  406. }
  407. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  408. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  409. struct AllowlistConfig {
  410. #[serde(default)]
  411. all: bool,
  412. #[serde(default)]
  413. fs: FsAllowlistConfig,
  414. #[serde(default)]
  415. window: WindowAllowlistConfig,
  416. #[serde(default)]
  417. shell: ShellAllowlistConfig,
  418. #[serde(default)]
  419. dialog: DialogAllowlistConfig,
  420. #[serde(default)]
  421. http: HttpAllowlistConfig,
  422. #[serde(default)]
  423. notification: NotificationAllowlistConfig,
  424. #[serde(default)]
  425. global_shortcut: GlobalShortcutAllowlistConfig,
  426. }
  427. impl Allowlist for AllowlistConfig {
  428. fn to_features(&self) -> Vec<&str> {
  429. if self.all {
  430. vec!["api-all"]
  431. } else {
  432. let mut features = Vec::new();
  433. features.extend(self.fs.to_features());
  434. features.extend(self.window.to_features());
  435. features.extend(self.shell.to_features());
  436. features.extend(self.dialog.to_features());
  437. features.extend(self.http.to_features());
  438. features.extend(self.notification.to_features());
  439. features.extend(self.global_shortcut.to_features());
  440. features
  441. }
  442. }
  443. }
  444. /// The Tauri configuration object.
  445. #[skip_serializing_none]
  446. #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  447. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  448. pub struct TauriConfig {
  449. /// The windows configuration.
  450. #[serde(default)]
  451. pub windows: Vec<WindowConfig>,
  452. /// The CLI configuration.
  453. pub cli: Option<CliConfig>,
  454. /// The bundler configuration.
  455. #[serde(default)]
  456. pub bundle: BundleConfig,
  457. #[serde(default)]
  458. allowlist: AllowlistConfig,
  459. pub security: Option<SecurityConfig>,
  460. }
  461. impl TauriConfig {
  462. #[allow(dead_code)]
  463. pub fn features(&self) -> Vec<&str> {
  464. self.allowlist.to_features()
  465. }
  466. }
  467. /// The Build configuration object.
  468. #[skip_serializing_none]
  469. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  470. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  471. pub struct BuildConfig {
  472. /// the app's dev server URL, or the path to the directory containing an index.html file
  473. #[serde(default = "default_dev_path")]
  474. pub dev_path: String,
  475. /// the path to the app's dist dir. This path must contain your index.html file.
  476. #[serde(default = "default_dist_dir")]
  477. pub dist_dir: String,
  478. /// a shell command to run before `tauri dev` kicks in
  479. pub before_dev_command: Option<String>,
  480. /// a shell command to run before `tauri build` kicks in
  481. pub before_build_command: Option<String>,
  482. /// Whether we should inject the Tauri API on `window.__TAURI__` or not.
  483. #[serde(default)]
  484. pub with_global_tauri: bool,
  485. }
  486. fn default_dev_path() -> String {
  487. "".to_string()
  488. }
  489. fn default_dist_dir() -> String {
  490. "../dist".to_string()
  491. }
  492. type JsonObject = HashMap<String, JsonValue>;
  493. /// The tauri.conf.json mapper.
  494. #[skip_serializing_none]
  495. #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
  496. #[serde(rename_all = "camelCase", deny_unknown_fields)]
  497. pub struct Config {
  498. /// The Tauri configuration.
  499. #[serde(default)]
  500. pub tauri: TauriConfig,
  501. /// The build configuration.
  502. #[serde(default = "default_build")]
  503. pub build: BuildConfig,
  504. /// The plugins config.
  505. #[serde(default)]
  506. pub plugins: HashMap<String, JsonObject>,
  507. }
  508. fn default_build() -> BuildConfig {
  509. BuildConfig {
  510. dev_path: default_dev_path(),
  511. dist_dir: default_dist_dir(),
  512. before_dev_command: None,
  513. before_build_command: None,
  514. with_global_tauri: false,
  515. }
  516. }