use crate::rpc_message::rpc_request_parsing_error::RpcRequestParsingError;
use crate::support::get_json_type;
use serde_json::{Map, Value};
pub(super) fn extract_value(obj: &mut Map<String, Value>, key: &str) -> Option<Value> {
obj.remove(key)
}
pub(super) fn validate_version(version_val: Option<Value>) -> Result<(), Option<Value>> {
match version_val {
Some(version) => {
if version.as_str().unwrap_or_default() == "2.0" {
Ok(())
} else {
Err(Some(version)) }
}
None => Err(None), }
}
pub(super) fn parse_method(method_val: Option<Value>) -> Result<String, Option<Value>> {
match method_val {
Some(Value::String(method_name)) => Ok(method_name),
Some(other) => Err(Some(other)), None => Err(None), }
}
pub(super) fn parse_params(params_val: Option<Value>) -> Result<Option<Value>, RpcRequestParsingError> {
match params_val {
None => Ok(None),
Some(Value::Array(_)) | Some(Value::Object(_)) => Ok(Some(params_val.unwrap())), Some(other) => Err(RpcRequestParsingError::ParamsInvalidType {
actual_type: get_json_type(&other).to_string(),
}),
}
}