-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
385 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::value::Value; | ||
use crate::types::*; | ||
|
||
impl Value { | ||
pub fn cast_json_to_value(&self) -> Result<Value, Box<dyn std::error::Error>> { | ||
if ! self.dt == JSON { | ||
return Err(format!("This Dynamic type is not json: {}", self.dt).into()); | ||
} | ||
match &self.data { | ||
Val::Json(j_val) => { | ||
if j_val.is_string() { | ||
match j_val.as_str() { | ||
Some(j_string) => { | ||
return Ok(Value::from_string(j_string)); | ||
} | ||
None => { | ||
return Err(format!("This JSON is having a data that is not STRING").into()); | ||
} | ||
} | ||
} else if j_val.is_i64() { | ||
match j_val.as_i64() { | ||
Some(j_int) => { | ||
return Ok(Value::from_int(j_int)); | ||
} | ||
None => { | ||
return Err(format!("This JSON is having a data that is not INT").into()); | ||
} | ||
} | ||
} else if j_val.is_f64() { | ||
match j_val.as_f64() { | ||
Some(j_float) => { | ||
return Ok(Value::from_float(j_float)); | ||
} | ||
None => { | ||
return Err(format!("This JSON is having a data that is not FLOAT").into()); | ||
} | ||
} | ||
} else if j_val.is_null() { | ||
return Ok(Value::none()); | ||
} else if j_val.is_boolean() { | ||
match j_val.as_bool() { | ||
Some(j_bool) => { | ||
return Ok(Value::from_bool(j_bool)); | ||
} | ||
None => { | ||
return Err(format!("This JSON is having a data that is not BOOL").into()); | ||
} | ||
} | ||
} else if j_val.is_array() { | ||
let mut res = Value::list(); | ||
match j_val.as_array() { | ||
Some(j_arr) => { | ||
for v in j_arr { | ||
let jv = Value::json(v.clone()); | ||
match jv.cast_json_to_value() { | ||
Ok(ja_val) => { | ||
res.push(ja_val); | ||
} | ||
Err(err) => { | ||
return Err(format!("JSON returns error during casting: {}", err).into()); | ||
} | ||
} | ||
} | ||
} | ||
None => { | ||
return Err(format!("This JSON is having a data that is not ARRAY").into()); | ||
} | ||
} | ||
return Ok(res); | ||
} else if j_val.is_object() { | ||
let mut res = Value::dict(); | ||
match j_val.as_object() { | ||
Some(j_val_obj) => { | ||
for n in j_val_obj.keys() { | ||
let j_val_value = match j_val_obj.get(n) { | ||
Some(j_val_value) => j_val_value.clone(), | ||
None => serde_json::json!(null), | ||
}; | ||
let jv = Value::json(j_val_value); | ||
match jv.cast_json_to_value() { | ||
Ok(ja_val) => { | ||
res.set(n, ja_val); | ||
} | ||
Err(err) => { | ||
return Err(format!("JSON returns error during casting: {}", err).into()); | ||
} | ||
} | ||
} | ||
} | ||
None => { | ||
return Err(format!("This JSON is having a data that is not OBJECT").into()); | ||
} | ||
} | ||
return Ok(res); | ||
} else { | ||
return Err(format!("This JSON is having a data that is not supportable").into()); | ||
} | ||
} | ||
_ => return Err(format!("This Dynamic type is not JSON: {}", self.dt).into()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
use crate::value::Value; | ||
use nanoid::nanoid; | ||
use easy_error::{Error, bail}; | ||
|
||
impl Value { | ||
pub fn dup(&self) -> Result<Value, bincode2::Error> { | ||
pub fn dup(&self) -> Result<Value, Error> { | ||
match self.to_binary() { | ||
Ok(bin_rep) => { | ||
match Value::from_binary(bin_rep) { | ||
Ok(mut res) => { | ||
res.id = nanoid!(); | ||
return Result::Ok(res); | ||
} | ||
Err(err) => Err(err), | ||
Err(err) => bail!("dup() returns: {}", err), | ||
} | ||
} | ||
Err(err) => Err(err), | ||
Err(err) => bail!("dup() returns: {}", err), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.