Skip to content

Commit d36d9ba

Browse files
committed
Added is_number and as_number for all types of value
1 parent 775d684 commit d36d9ba

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

src/gotham_module.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ impl GothamModule {
4848
pub fn default(connection_path: &str) -> Self {
4949
let is_ip: std::result::Result<SocketAddr, AddrParseError> =
5050
connection_path.to_string().parse();
51-
if is_ip.is_ok() {
52-
let ip = is_ip.unwrap();
51+
if let Ok(ip) = is_ip {
5352
Self::from_inet_socket(&format!("{}", ip.ip()), ip.port())
5453
} else {
5554
Self::from_unix_socket(connection_path)

src/models/value.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,101 @@ pub enum Value {
1616
Array(Vec<Value>),
1717
Object(HashMap<String, Value>),
1818
}
19+
20+
impl Value {
21+
pub fn is_null(&self) -> bool {
22+
if let Value::Null = &self {
23+
true
24+
} else {
25+
false
26+
}
27+
}
28+
29+
pub fn as_null(&self) -> Option<()> {
30+
if let Value::Null = &self {
31+
Some(())
32+
} else {
33+
None
34+
}
35+
}
36+
37+
pub fn is_bool(&self) -> bool {
38+
if let Value::Bool(_) = &self {
39+
true
40+
} else {
41+
false
42+
}
43+
}
44+
45+
pub fn as_bool(&self) -> Option<&bool> {
46+
if let Value::Bool(value) = &self {
47+
Some(value)
48+
} else {
49+
None
50+
}
51+
}
52+
53+
pub fn is_number(&self) -> bool {
54+
if let Value::Number(_) = &self {
55+
true
56+
} else {
57+
false
58+
}
59+
}
60+
61+
pub fn as_number(&self) -> Option<&Number> {
62+
if let Value::Number(value) = &self {
63+
Some(value)
64+
} else {
65+
None
66+
}
67+
}
68+
69+
pub fn is_string(&self) -> bool {
70+
if let Value::String(_) = &self {
71+
true
72+
} else {
73+
false
74+
}
75+
}
76+
77+
pub fn as_string(&self) -> Option<&String> {
78+
if let Value::String(value) = &self {
79+
Some(value)
80+
} else {
81+
None
82+
}
83+
}
84+
85+
pub fn is_array(&self) -> bool {
86+
if let Value::Array(_) = &self {
87+
true
88+
} else {
89+
false
90+
}
91+
}
92+
93+
pub fn as_array(&self) -> Option<&Vec<Value>> {
94+
if let Value::Array(value) = &self {
95+
Some(value)
96+
} else {
97+
None
98+
}
99+
}
100+
101+
pub fn is_object(&self) -> bool {
102+
if let Value::Object(_) = &self {
103+
true
104+
} else {
105+
false
106+
}
107+
}
108+
109+
pub fn as_object(&self) -> Option<&HashMap<String, Value>> {
110+
if let Value::Object(value) = &self {
111+
Some(value)
112+
} else {
113+
None
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)