22// SPDX-License-Identifier: Apache-2.0
33// SPDX-License-Identifier: MIT
44
5- pub use context:: { context_codegen, ContextData } ;
5+ pub use self :: context:: { context_codegen, ContextData } ;
66use std:: {
77 borrow:: Cow ,
8- fs:: File ,
9- io:: BufReader ,
108 path:: { Path , PathBuf } ,
119} ;
12- use tauri_utils:: config:: Config ;
13- use thiserror:: Error ;
10+ pub use tauri_utils:: config:: { parse:: ConfigError , Config } ;
1411
1512mod context;
1613pub mod embedded_assets;
1714
18- /// Represents all the errors that can happen while reading the config.
19- #[ derive( Debug , Error ) ]
15+ /// Represents all the errors that can happen while reading the config during codegen .
16+ #[ derive( Debug , thiserror :: Error ) ]
2017#[ non_exhaustive]
21- pub enum ConfigError {
18+ pub enum CodegenConfigError {
2219 #[ error( "unable to access current working directory: {0}" ) ]
2320 CurrentDir ( std:: io:: Error ) ,
2421
2522 // this error should be "impossible" because we use std::env::current_dir() - cover it anyways
2623 #[ error( "Tauri config file has no parent, this shouldn't be possible. file an issue on https://github.com/tauri-apps/tauri - target {0}" ) ]
2724 Parent ( PathBuf ) ,
2825
29- #[ error( "unable to parse inline TAURI_CONFIG env var: {0}" ) ]
26+ #[ error( "unable to parse inline JSON TAURI_CONFIG env var: {0}" ) ]
3027 FormatInline ( serde_json:: Error ) ,
3128
32- #[ error( "unable to parse Tauri config file at {path} because {error}" ) ]
33- Format {
34- path : PathBuf ,
35- error : serde_json:: Error ,
36- } ,
37-
38- #[ error( "unable to read Tauri config file at {path} because {error}" ) ]
39- Io {
40- path : PathBuf ,
41- error : std:: io:: Error ,
42- } ,
29+ #[ error( "{0}" ) ]
30+ ConfigError ( #[ from] ConfigError ) ,
4331}
4432
4533/// Get the [`Config`] from the `TAURI_CONFIG` environmental variable, or read from the passed path.
4634///
4735/// If the passed path is relative, it should be relative to the current working directory of the
4836/// compiling crate.
49- pub fn get_config ( path : & Path ) -> Result < ( Config , PathBuf ) , ConfigError > {
37+ pub fn get_config ( path : & Path ) -> Result < ( Config , PathBuf ) , CodegenConfigError > {
5038 let path = if path. is_relative ( ) {
51- let cwd = std:: env:: current_dir ( ) . map_err ( ConfigError :: CurrentDir ) ?;
39+ let cwd = std:: env:: current_dir ( ) . map_err ( CodegenConfigError :: CurrentDir ) ?;
5240 Cow :: Owned ( cwd. join ( path) )
5341 } else {
5442 Cow :: Borrowed ( path)
@@ -59,27 +47,16 @@ pub fn get_config(path: &Path) -> Result<(Config, PathBuf), ConfigError> {
5947 // already unlikely unless the developer goes out of their way to run the cli on a different
6048 // project than the target crate.
6149 let config = if let Ok ( env) = std:: env:: var ( "TAURI_CONFIG" ) {
62- serde_json:: from_str ( & env) . map_err ( ConfigError :: FormatInline ) ?
50+ serde_json:: from_str ( & env) . map_err ( CodegenConfigError :: FormatInline ) ?
6351 } else {
64- File :: open ( & path)
65- . map_err ( |error| ConfigError :: Io {
66- path : path. clone ( ) . into_owned ( ) ,
67- error,
68- } )
69- . map ( BufReader :: new)
70- . and_then ( |file| {
71- serde_json:: from_reader ( file) . map_err ( |error| ConfigError :: Format {
72- path : path. clone ( ) . into_owned ( ) ,
73- error,
74- } )
75- } ) ?
52+ tauri_utils:: config:: parse ( path. to_path_buf ( ) ) ?
7653 } ;
7754
7855 // this should be impossible because of the use of `current_dir()` above, but handle it anyways
7956 let parent = path
8057 . parent ( )
8158 . map ( ToOwned :: to_owned)
82- . ok_or_else ( || ConfigError :: Parent ( path. into_owned ( ) ) ) ?;
59+ . ok_or_else ( || CodegenConfigError :: Parent ( path. into_owned ( ) ) ) ?;
8360
8461 Ok ( ( config, parent) )
8562}
0 commit comments