Skip to content

Commit 075bbd3

Browse files
committed
fix(flag): check if flag value is the default value for the type
1 parent 0880763 commit 075bbd3

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/lib.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub use value::Slice;
4747
pub use value::Value;
4848
pub use value::ValueError;
4949

50+
use std::any::TypeId;
5051
use std::collections::BTreeMap;
5152
use std::error;
5253
use std::fmt;
@@ -90,13 +91,48 @@ pub struct Flag {
9091
pub shorthand_deprecated: String,
9192
}
9293

94+
macro_rules! const_type_id {
95+
($name:ident, $typ:ty) => {
96+
const $name: TypeId = TypeId::of::<$typ>();
97+
};
98+
}
99+
100+
const_type_id!(BOOL_ID, bool);
101+
const_type_id!(STRING_ID, String);
102+
const_type_id!(DURATION_ID, time::Duration);
103+
const_type_id!(U8_ID, u8);
104+
const_type_id!(U16_ID, u16);
105+
const_type_id!(U32_ID, u32);
106+
const_type_id!(U64_ID, u64);
107+
const_type_id!(I8_ID, i8);
108+
const_type_id!(I16_ID, i16);
109+
const_type_id!(I32_ID, i32);
110+
const_type_id!(I64_ID, i64);
111+
const_type_id!(F32_ID, f32);
112+
const_type_id!(F64_ID, f64);
113+
const_type_id!(IP_ADDR_ID, IpAddr);
114+
const_type_id!(IP_V4_ADDR_ID, Ipv4Addr);
115+
const_type_id!(IP_V6_ADDR_ID, Ipv6Addr);
116+
93117
impl Flag {
94118
pub fn set(&mut self, val: String) -> Result<(), ValueError> {
95119
self.value.set(val)
96120
}
97121

98122
fn default_is_zero_value(&self) -> bool {
99-
self.def_value == ""
123+
let def_value = self.def_value.clone();
124+
let id = self.value.value().type_id();
125+
match id {
126+
BOOL_ID => def_value == "false",
127+
DURATION_ID => def_value == "0" || def_value == "0s",
128+
U8_ID | U16_ID | U32_ID | U64_ID | I8_ID | I16_ID | I32_ID | I64_ID | F32_ID
129+
| F64_ID => def_value == "0",
130+
STRING_ID => def_value == "",
131+
IP_ADDR_ID | IP_V4_ADDR_ID | IP_V6_ADDR_ID => {
132+
def_value == "0.0.0.0" || def_value == "0:0:0:0:0:0:0:0"
133+
}
134+
_ => def_value == "" || def_value == "[]" || def_value == "0" || def_value == "false",
135+
}
100136
}
101137
}
102138

0 commit comments

Comments
 (0)