Skip to content

Commit b425cfc

Browse files
authored
refactor: organize the code in different modules (#69)
We wants the code be more readable.
1 parent 3456e9c commit b425cfc

File tree

6 files changed

+489
-379
lines changed

6 files changed

+489
-379
lines changed

src/config.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use serde::Deserialize;
22
use std::{error, fs, path::Path};
33
use toml::{self};
44

5-
#[derive(Clone, Deserialize, Debug)]
5+
#[derive(Clone, Deserialize, Debug, Default)]
66
pub struct Config {
77
pub theme: Option<Theme>,
88
pub core: Option<Core>,
9-
pub info: Info,
9+
pub info: Option<Info>,
1010
}
1111

1212
#[derive(Clone, Deserialize, Debug)]
@@ -83,6 +83,17 @@ impl Default for Theme {
8383
}
8484
}
8585

86+
impl Default for Info {
87+
fn default() -> Self {
88+
Self {
89+
name: "Unknown".to_owned(),
90+
input_method: "Unknow".to_owned(),
91+
homepage: "Unknow".to_owned(),
92+
maintainors: vec!["Unknow".to_owned()],
93+
}
94+
}
95+
}
96+
8697
impl Config {
8798
pub fn from_file(filepath: &Path) -> Result<Self, Box<dyn error::Error>> {
8899
let content = fs::read_to_string(filepath)?;
@@ -99,18 +110,19 @@ mod tests {
99110
use crate::config::Config;
100111
use std::path::Path;
101112

102-
let config = Config::from_file(Path::new("./data/sample.toml"));
113+
let config = Config::from_file(Path::new("./data/blank_sample.toml"));
103114
assert!(config.is_ok());
104115

105116
// Load default core and theme.
106117
let config = config.unwrap();
107118
config.core.unwrap_or_default();
108119
config.theme.unwrap_or_default();
120+
config.info.unwrap_or_default();
109121

110122
let config = Config::from_file(Path::new("./data/full_sample.toml"));
111123
assert!(config.is_ok());
112124

113-
let config = Config::from_file(Path::new("./data/blank_sample.toml"));
114-
assert!(config.is_err());
125+
let config = Config::from_file(Path::new("./data/sample.toml"));
126+
assert!(config.is_ok());
115127
}
116128
}

0 commit comments

Comments
 (0)