mirror of
https://github.com/nushell/nushell.git
synced 2025-08-13 02:07:55 +02:00
Add general refactorings (#3996)
This commit is contained in:
@ -144,19 +144,10 @@ impl Value {
|
||||
/// a value associated with the provided key is found. If no value is found
|
||||
/// or the `Value` is not an Object, returns None.
|
||||
pub fn search<'a>(&'a self, key: &str) -> Option<&'a Value> {
|
||||
match *self {
|
||||
Value::Object(ref map) => match map.get(key) {
|
||||
Some(json_value) => Some(json_value),
|
||||
None => {
|
||||
for (_, v) in map.iter() {
|
||||
match v.search(key) {
|
||||
x if x.is_some() => return x,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
},
|
||||
match self {
|
||||
Value::Object(map) => map
|
||||
.get(key)
|
||||
.or_else(|| map.values().find_map(|v| v.search(key))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -192,8 +183,8 @@ impl Value {
|
||||
/// If the `Value` is an Array, returns the associated vector.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_array(&self) -> Option<&Vec<Value>> {
|
||||
match *self {
|
||||
Value::Array(ref array) => Some(&*array),
|
||||
match self {
|
||||
Value::Array(array) => Some(array),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -201,8 +192,8 @@ impl Value {
|
||||
/// If the `Value` is an Array, returns the associated mutable vector.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>> {
|
||||
match *self {
|
||||
Value::Array(ref mut list) => Some(list),
|
||||
match self {
|
||||
Value::Array(list) => Some(list),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -215,30 +206,30 @@ impl Value {
|
||||
/// If the `Value` is a String, returns the associated str.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_str(&self) -> Option<&str> {
|
||||
match *self {
|
||||
Value::String(ref s) => Some(s),
|
||||
match self {
|
||||
Value::String(s) => Some(s),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Number. Returns false otherwise.
|
||||
pub fn is_number(&self) -> bool {
|
||||
matches!(*self, Value::I64(_) | Value::U64(_) | Value::F64(_))
|
||||
matches!(self, Value::I64(_) | Value::U64(_) | Value::F64(_))
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a i64. Returns false otherwise.
|
||||
pub fn is_i64(&self) -> bool {
|
||||
matches!(*self, Value::I64(_))
|
||||
matches!(self, Value::I64(_))
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a u64. Returns false otherwise.
|
||||
pub fn is_u64(&self) -> bool {
|
||||
matches!(*self, Value::U64(_))
|
||||
matches!(self, Value::U64(_))
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a f64. Returns false otherwise.
|
||||
pub fn is_f64(&self) -> bool {
|
||||
matches!(*self, Value::F64(_))
|
||||
matches!(self, Value::F64(_))
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a i64.
|
||||
@ -294,7 +285,7 @@ impl Value {
|
||||
/// If the `Value` is a Null, returns ().
|
||||
/// Returns None otherwise.
|
||||
pub fn as_null(&self) -> Option<()> {
|
||||
match *self {
|
||||
match self {
|
||||
Value::Null => Some(()),
|
||||
_ => None,
|
||||
}
|
||||
|
Reference in New Issue
Block a user