1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use crate::BundleConfig;
use crate::CargoError;
use core::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[non_exhaustive]
pub enum Platform {
    /// Targeting the web platform using WASM
    #[cfg_attr(feature = "cli", clap(name = "web"))]
    #[serde(rename = "web")]
    Web,

    /// Targeting the desktop platform using Tao/Wry-based webview
    #[cfg_attr(feature = "cli", clap(name = "desktop"))]
    #[serde(rename = "desktop")]
    Desktop,

    /// Targeting the server platform using Axum and Dioxus-Fullstack
    #[cfg_attr(feature = "cli", clap(name = "fullstack"))]
    #[serde(rename = "fullstack")]
    Fullstack,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DioxusConfig {
    pub application: ApplicationConfig,

    pub web: WebConfig,

    #[serde(default)]
    pub bundle: BundleConfig,

    #[cfg(feature = "cli")]
    #[serde(default = "default_plugin")]
    pub plugin: toml::Value,
}

#[cfg(feature = "cli")]
fn default_plugin() -> toml::Value {
    toml::Value::Boolean(true)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadDioxusConfigError {
    location: String,
    error: String,
}

impl std::fmt::Display for LoadDioxusConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.location, self.error)
    }
}

impl std::error::Error for LoadDioxusConfigError {}

#[derive(Debug)]
#[non_exhaustive]
pub enum CrateConfigError {
    Cargo(CargoError),
    Io(std::io::Error),
    #[cfg(feature = "cli")]
    Toml(toml::de::Error),
    LoadDioxusConfig(LoadDioxusConfigError),
}

impl From<CargoError> for CrateConfigError {
    fn from(err: CargoError) -> Self {
        Self::Cargo(err)
    }
}

impl From<std::io::Error> for CrateConfigError {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

#[cfg(feature = "cli")]
impl From<toml::de::Error> for CrateConfigError {
    fn from(err: toml::de::Error) -> Self {
        Self::Toml(err)
    }
}

impl From<LoadDioxusConfigError> for CrateConfigError {
    fn from(err: LoadDioxusConfigError) -> Self {
        Self::LoadDioxusConfig(err)
    }
}

impl Display for CrateConfigError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Cargo(err) => write!(f, "{}", err),
            Self::Io(err) => write!(f, "{}", err),
            #[cfg(feature = "cli")]
            Self::Toml(err) => write!(f, "{}", err),
            Self::LoadDioxusConfig(err) => write!(f, "{}", err),
        }
    }
}

impl std::error::Error for CrateConfigError {}

impl DioxusConfig {
    #[cfg(feature = "cli")]
    /// Load the dioxus config from a path
    #[tracing::instrument]
    pub fn load(bin: Option<PathBuf>) -> Result<Option<DioxusConfig>, CrateConfigError> {
        let crate_dir = crate::cargo::crate_root();

        let crate_dir = match crate_dir {
            Ok(dir) => {
                if let Some(bin) = bin {
                    dir.join(bin)
                } else {
                    dir
                }
            }
            Err(_) => return Ok(None),
        };
        let crate_dir = crate_dir.as_path();

        let Some(dioxus_conf_file) = acquire_dioxus_toml(crate_dir) else {
            tracing::warn!(?crate_dir, "no dioxus config found for");
            return Ok(None);
        };

        let dioxus_conf_file = dioxus_conf_file.as_path();
        let cfg = toml::from_str::<DioxusConfig>(&std::fs::read_to_string(dioxus_conf_file)?)
            .map_err(|err| {
                let error_location = dioxus_conf_file
                    .strip_prefix(crate_dir)
                    .unwrap_or(dioxus_conf_file)
                    .display();
                CrateConfigError::LoadDioxusConfig(LoadDioxusConfigError {
                    location: error_location.to_string(),
                    error: err.to_string(),
                })
            })
            .map(Some);
        match cfg {
            Ok(Some(mut cfg)) => {
                let name = cfg.application.name.clone();
                if cfg.bundle.identifier.is_none() {
                    cfg.bundle.identifier = Some(format!("io.github.{name}"));
                }
                if cfg.bundle.publisher.is_none() {
                    cfg.bundle.publisher = Some(name);
                }
                Ok(Some(cfg))
            }
            cfg => cfg,
        }
    }
}

#[cfg(feature = "cli")]
#[tracing::instrument]
fn acquire_dioxus_toml(dir: &std::path::Path) -> Option<PathBuf> {
    use tracing::trace;

    ["Dioxus.toml", "dioxus.toml"]
        .into_iter()
        .map(|file| dir.join(file))
        .inspect(|path| trace!("checking [{path:?}]"))
        .find(|path| path.is_file())
}

impl Default for DioxusConfig {
    fn default() -> Self {
        let name = default_name();
        Self {
            application: ApplicationConfig {
                name: name.clone(),
                default_platform: default_platform(),
                out_dir: out_dir_default(),
                asset_dir: asset_dir_default(),
                hot_reload: hot_reload_default(),

                #[cfg(feature = "cli")]
                tools: Default::default(),

                sub_package: None,
            },
            web: WebConfig {
                app: WebAppConfig {
                    title: default_title(),
                    base_path: None,
                },
                proxy: vec![],
                watcher: Default::default(),
                resource: WebResourceConfig {
                    dev: WebDevResourceConfig {
                        style: vec![],
                        script: vec![],
                    },
                    style: Some(vec![]),
                    script: Some(vec![]),
                },
                https: WebHttpsConfig {
                    enabled: None,
                    mkcert: None,
                    key_path: None,
                    cert_path: None,
                },
                pre_compress: true,
            },
            bundle: BundleConfig {
                identifier: Some(format!("io.github.{name}")),
                publisher: Some(name),
                ..Default::default()
            },
            #[cfg(feature = "cli")]
            plugin: toml::Value::Table(toml::map::Map::new()),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationConfig {
    #[serde(default = "default_name")]
    pub name: String,

    #[serde(default = "default_platform")]
    pub default_platform: Platform,

    #[serde(default = "out_dir_default")]
    pub out_dir: PathBuf,

    #[serde(default = "asset_dir_default")]
    pub asset_dir: PathBuf,

    #[serde(default = "hot_reload_default")]
    pub hot_reload: bool,

    #[cfg(feature = "cli")]
    #[serde(default)]
    pub tools: std::collections::HashMap<String, toml::Value>,

    #[serde(default)]
    pub sub_package: Option<String>,
}

fn default_name() -> String {
    "name".into()
}

fn default_platform() -> Platform {
    Platform::Web
}

fn hot_reload_default() -> bool {
    true
}

fn asset_dir_default() -> PathBuf {
    PathBuf::from("public")
}

fn out_dir_default() -> PathBuf {
    PathBuf::from("dist")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebConfig {
    #[serde(default)]
    pub app: WebAppConfig,
    #[serde(default)]
    pub proxy: Vec<WebProxyConfig>,
    #[serde(default)]
    pub watcher: WebWatcherConfig,
    #[serde(default)]
    pub resource: WebResourceConfig,
    #[serde(default)]
    pub https: WebHttpsConfig,
    /// Whether to enable pre-compression of assets and wasm during a web build in release mode
    #[serde(default = "true_bool")]
    pub pre_compress: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebAppConfig {
    #[serde(default = "default_title")]
    pub title: String,
    pub base_path: Option<String>,
}

impl Default for WebAppConfig {
    fn default() -> Self {
        Self {
            title: default_title(),
            base_path: None,
        }
    }
}

fn default_title() -> String {
    "dioxus | ⛺".into()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebProxyConfig {
    pub backend: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebWatcherConfig {
    #[serde(default = "watch_path_default")]
    pub watch_path: Vec<PathBuf>,

    #[serde(default)]
    pub reload_html: bool,

    #[serde(default = "true_bool")]
    pub index_on_404: bool,
}

impl Default for WebWatcherConfig {
    fn default() -> Self {
        Self {
            watch_path: watch_path_default(),
            reload_html: false,
            index_on_404: true,
        }
    }
}

fn watch_path_default() -> Vec<PathBuf> {
    vec![PathBuf::from("src"), PathBuf::from("examples")]
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct WebResourceConfig {
    pub dev: WebDevResourceConfig,
    pub style: Option<Vec<PathBuf>>,
    pub script: Option<Vec<PathBuf>>,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct WebDevResourceConfig {
    #[serde(default)]
    pub style: Vec<PathBuf>,
    #[serde(default)]
    pub script: Vec<PathBuf>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct WebHttpsConfig {
    pub enabled: Option<bool>,
    pub mkcert: Option<bool>,
    pub key_path: Option<String>,
    pub cert_path: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrateConfig {
    pub crate_dir: PathBuf,
    pub workspace_dir: PathBuf,
    pub target_dir: PathBuf,
    #[cfg(feature = "cli")]
    pub manifest: cargo_toml::Manifest<cargo_toml::Value>,
    pub executable: ExecutableType,
    pub dioxus_config: DioxusConfig,
    pub release: bool,
    pub hot_reload: bool,
    pub cross_origin_policy: bool,
    pub verbose: bool,
    pub custom_profile: Option<String>,
    pub features: Option<Vec<String>>,
    pub target: Option<String>,
    pub cargo_args: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExecutableType {
    Binary(String),
    Lib(String),
    Example(String),
}

impl ExecutableType {
    /// Get the name of the executable if it is a binary or an example.
    pub fn executable(&self) -> Option<&str> {
        match self {
            Self::Binary(bin) | Self::Example(bin) => Some(bin),
            _ => None,
        }
    }
}

impl CrateConfig {
    #[cfg(feature = "cli")]
    pub fn new(bin: Option<PathBuf>) -> Result<Self, CrateConfigError> {
        let dioxus_config = DioxusConfig::load(bin.clone())?.unwrap_or_default();

        let crate_root = crate::crate_root()?;

        let crate_dir = if let Some(package) = &dioxus_config.application.sub_package {
            crate_root.join(package)
        } else if let Some(bin) = bin {
            crate_root.join(bin)
        } else {
            crate_root
        };

        let meta = crate::Metadata::get()?;
        let workspace_dir = meta.workspace_root;
        let target_dir = meta.target_directory;

        let cargo_def = &crate_dir.join("Cargo.toml");

        let manifest = cargo_toml::Manifest::from_path(cargo_def).unwrap();

        let mut output_filename = String::from("dioxus_app");
        if let Some(package) = &manifest.package.as_ref() {
            output_filename = match &package.default_run {
                Some(default_run_target) => default_run_target.to_owned(),
                None => manifest
                    .bin
                    .iter()
                    .find(|b| {
                        #[allow(clippy::useless_asref)]
                        let matching_bin =
                            b.name == manifest.package.as_ref().map(|pkg| pkg.name.clone());
                        matching_bin
                    })
                    .or(manifest
                        .bin
                        .iter()
                        .find(|b| b.path == Some("src/main.rs".to_owned())))
                    .or(manifest.bin.first())
                    .or(manifest.lib.as_ref())
                    .and_then(|prod| prod.name.clone())
                    .unwrap_or(String::from("dioxus_app")),
            };
        }

        let executable = ExecutableType::Binary(output_filename);

        let release = false;
        let hot_reload = false;
        let cross_origin_policy = false;
        let verbose = false;
        let custom_profile = None;
        let features = None;
        let target = None;
        let cargo_args = vec![];

        Ok(Self {
            crate_dir,
            workspace_dir,
            target_dir,
            #[cfg(feature = "cli")]
            manifest,
            executable,
            dioxus_config,
            release,
            hot_reload,
            cross_origin_policy,
            verbose,
            custom_profile,
            features,
            target,
            cargo_args,
        })
    }

    /// Compose an asset directory. Represents the typical "public" directory
    /// with publicly available resources (configurable in the `Dioxus.toml`).
    pub fn asset_dir(&self) -> PathBuf {
        self.crate_dir
            .join(&self.dioxus_config.application.asset_dir)
    }

    /// Compose an out directory. Represents the typical "dist" directory that
    /// is "distributed" after building an application (configurable in the
    /// `Dioxus.toml`).
    pub fn out_dir(&self) -> PathBuf {
        self.crate_dir.join(&self.dioxus_config.application.out_dir)
    }

    /// Compose an out directory for the fullstack platform. See `out_dir()`
    /// method.
    pub fn fullstack_out_dir(&self) -> PathBuf {
        self.crate_dir.join(".dioxus")
    }

    /// Compose a target directory for the server (fullstack-only?).
    pub fn server_target_dir(&self) -> PathBuf {
        self.fullstack_out_dir().join("ssr")
    }

    /// Compose a target directory for the client (fullstack-only?).
    pub fn client_target_dir(&self) -> PathBuf {
        self.fullstack_out_dir().join("web")
    }

    pub fn as_example(&mut self, example_name: String) -> &mut Self {
        self.executable = ExecutableType::Example(example_name);
        self
    }

    pub fn with_release(&mut self, release: bool) -> &mut Self {
        self.release = release;
        self
    }

    pub fn with_hot_reload(&mut self, hot_reload: bool) -> &mut Self {
        self.hot_reload = hot_reload;
        self
    }

    pub fn with_cross_origin_policy(&mut self, cross_origin_policy: bool) -> &mut Self {
        self.cross_origin_policy = cross_origin_policy;
        self
    }

    pub fn with_verbose(&mut self, verbose: bool) -> &mut Self {
        self.verbose = verbose;
        self
    }

    pub fn set_profile(&mut self, profile: String) -> &mut Self {
        self.custom_profile = Some(profile);
        self
    }

    pub fn set_features(&mut self, features: Vec<String>) -> &mut Self {
        self.features = Some(features);
        self
    }

    pub fn set_target(&mut self, target: String) -> &mut Self {
        self.target = Some(target);
        self
    }

    pub fn set_cargo_args(&mut self, cargo_args: Vec<String>) -> &mut Self {
        self.cargo_args = cargo_args;
        self
    }

    pub fn add_features(&mut self, feature: Vec<String>) -> &mut Self {
        if let Some(features) = &mut self.features {
            features.extend(feature);
        } else {
            self.features = Some(feature);
        }
        self
    }

    #[cfg(feature = "cli")]
    pub fn extend_with_platform(&mut self, platform: Platform) -> &mut Self {
        let manifest = &self.manifest;
        let features = match platform {
            Platform::Web if manifest.features.contains_key("web") => {
                vec!["web".to_string()]
            }
            Platform::Desktop if manifest.features.contains_key("desktop") => {
                vec!["desktop".to_string()]
            }
            _ => {
                // fullstack has its own feature insertion - we use a different featureset for the client and server
                vec![]
            }
        };
        self.add_features(features);
        self
    }

    /// Check if assets should be pre_compressed. This will only be true in release mode if the user has enabled pre_compress in the web config.
    pub fn should_pre_compress_web_assets(&self) -> bool {
        self.dioxus_config.web.pre_compress && self.release
    }
}

fn true_bool() -> bool {
    true
}