Skip to content

Commit 4674d64

Browse files
committed
fix: compile error
1 parent e83f57a commit 4674d64

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dotenv_config"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55
authors = ["Hengfei Yang <[email protected]>"]
66
description = "parse `env` to config struct for Rust"

examples/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct Redis {
2424

2525
fn main() {
2626
dotenv().ok();
27-
let cfg = Config::init().unwrap();
27+
let cfg = Config::init().expect("config init error");
2828
println!("{:#?}", cfg);
2929
assert!(cfg.server_addr == "192.168.2.1");
3030
assert!(!cfg.server_mode);

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mod builder;
8080
pub fn derive_env_config(input: TokenStream) -> TokenStream {
8181
// println!("{:#?}", input);
8282
builder::BuilderContext::render(input)
83-
.unwrap()
83+
.expect("render error")
8484
.parse()
85-
.unwrap()
85+
.expect("parse error")
8686
}

templates/builder.j2

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl {{ name }} {
2525
help_map
2626
}
2727

28-
pub fn init() -> Result<{{ name }}, String> {
28+
pub fn init() -> Result<{{ name }}, &'static str> {
2929
let mut key: &str;
3030
{% for field in fields %}
3131
{% if field.attr_name.is_empty() %}
@@ -37,7 +37,9 @@ impl {{ name }} {
3737
let {{ field.name }}: Option<{{ field.typ }}> = match std::env::var(key) {
3838
Ok(val) => match val.to_lowercase().parse() {
3939
Ok(v) => Some(v),
40-
Err(_) => {return Err(format!("{}: except a value of [{{ field.typ }}]", key));},
40+
Err(_) => {
41+
panic!("EnvConfig: {} except a value of [{{ field.typ }}]", key);
42+
},
4143
},
4244
Err(err) => {
4345
{% if field.attr_default.is_empty() %}
@@ -62,7 +64,9 @@ impl {{ name }} {
6264
let {{ field.name }}: Option<{{ field.typ }}> = match std::env::var(key) {
6365
Ok(val) => match val.parse::<{{ field.typ }}>() {
6466
Ok(v) => Some(v),
65-
Err(_) => {return Err(format!("{}: except a value of [{{ field.typ }}]", key));},
67+
Err(_) => {
68+
panic!("EnvConfig: {} except a value of [{{ field.typ }}]", key);
69+
},
6670
},
6771
Err(_) => {
6872
{% if field.attr_default.is_empty() %}

0 commit comments

Comments
 (0)