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
pub use context::{context_codegen, ContextData};
use std::{
borrow::Cow,
fs::File,
io::BufReader,
path::{Path, PathBuf},
};
pub use tauri_utils::config::Config;
use thiserror::Error;
mod context;
pub mod embedded_assets;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("unable to access current working directory: {0}")]
CurrentDir(std::io::Error),
#[error("Tauri config file has no parent, this shouldn't be possible. file an issue on https://github.com/tauri-apps/tauri - target {0}")]
Parent(PathBuf),
#[error("unable to parse inline TAURI_CONFIG env var: {0}")]
FormatInline(serde_json::Error),
#[error("unable to parse Tauri config file at {path} because {error}")]
Format {
path: PathBuf,
error: serde_json::Error,
},
#[error("unable to read Tauri config file at {path} because {error}")]
Io {
path: PathBuf,
error: std::io::Error,
},
}
pub fn get_config(path: &Path) -> Result<(Config, PathBuf), ConfigError> {
let path = if path.is_relative() {
let cwd = std::env::current_dir().map_err(ConfigError::CurrentDir)?;
Cow::Owned(cwd.join(path))
} else {
Cow::Borrowed(path)
};
let config = if let Ok(env) = std::env::var("TAURI_CONFIG") {
serde_json::from_str(&env).map_err(ConfigError::FormatInline)?
} else {
File::open(&path)
.map_err(|error| ConfigError::Io {
path: path.clone().into_owned(),
error,
})
.map(BufReader::new)
.and_then(|file| {
serde_json::from_reader(file).map_err(|error| ConfigError::Format {
path: path.clone().into_owned(),
error,
})
})?
};
let parent = path
.parent()
.map(ToOwned::to_owned)
.ok_or_else(|| ConfigError::Parent(path.into_owned()))?;
Ok((config, parent))
}