From d03ce84b6a97c8786d63e2bd8ad8d514d8d0839e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Fri, 12 Jan 2024 14:23:36 +0100 Subject: [PATCH] chore: cargo fmt --- rad/src/error.rs | 5 +- rad/src/lib.rs | 12 +-- rad/src/operators/array.rs | 31 ++++-- rad/src/operators/bytes.rs | 45 +++++---- rad/src/operators/integer.rs | 6 +- rad/src/operators/map.rs | 59 ++++++------ rad/src/operators/mod.rs | 2 +- rad/src/operators/string.rs | 179 +++++++++++++++++++---------------- rad/src/script.rs | 6 +- rad/src/types/bytes.rs | 6 +- rad/src/types/map.rs | 12 ++- rad/src/types/string.rs | 32 ++++--- 12 files changed, 226 insertions(+), 169 deletions(-) diff --git a/rad/src/error.rs b/rad/src/error.rs index c2c606ac7..3eeb11c3f 100644 --- a/rad/src/error.rs +++ b/rad/src/error.rs @@ -413,7 +413,10 @@ impl RadError { }) } } - None => Err(RadError::DecodeRadonErrorEmptyArray), + None => { + println!("None"); + Err(RadError::DecodeRadonErrorEmptyArray) + } } } diff --git a/rad/src/lib.rs b/rad/src/lib.rs index 4507a803e..c62706c34 100644 --- a/rad/src/lib.rs +++ b/rad/src/lib.rs @@ -299,11 +299,11 @@ async fn http_response( let mut response_bytes = Vec::::default(); // todo: before reading the response buffer, an error should be thrown if it was too big - body.read_to_end(&mut response_bytes).await.map_err(|x| { - RadError::HttpOther { + body.read_to_end(&mut response_bytes) + .await + .map_err(|x| RadError::HttpOther { message: x.to_string(), - } - })?; + })?; response = RadonTypes::from(RadonBytes::from(response_bytes)); } else { // response is a string @@ -323,8 +323,8 @@ async fn http_response( response = RadonTypes::from(RadonString::from(response_string)); } - let result = handle_response_with_data_report(retrieve, response, context, settings) - .map(|report| { + let result = + handle_response_with_data_report(retrieve, response, context, settings).map(|report| { let completion_ts = std::time::SystemTime::now(); RadonReport { context: ReportContext { diff --git a/rad/src/operators/array.rs b/rad/src/operators/array.rs index 757a3432f..468e0bf07 100644 --- a/rad/src/operators/array.rs +++ b/rad/src/operators/array.rs @@ -1,6 +1,6 @@ use std::{ clone::Clone, - convert::{TryFrom, TryInto} + convert::{TryFrom, TryInto}, }; use serde_cbor::value::{from_value, Value}; @@ -11,7 +11,10 @@ use crate::{ filters::{self, RadonFilters}, operators::string, reducers::{self, RadonReducers}, - script::{execute_radon_script, unpack_subscript, RadonScriptExecutionSettings, partial_results_extract}, + script::{ + execute_radon_script, partial_results_extract, unpack_subscript, + RadonScriptExecutionSettings, + }, types::{array::RadonArray, integer::RadonInteger, string::RadonString, RadonType, RadonTypes}, }; @@ -108,10 +111,7 @@ fn get_numeric_string(input: &RadonArray, args: &[Value]) -> Result Result { +pub fn join(input: &RadonArray, args: &[Value]) -> Result { // Join not applicable if the input array is not homogeneous if !input.is_homogeneous() { return Err(RadError::UnsupportedOpNonHomogeneous { @@ -140,6 +140,17 @@ pub fn join( _ => { Err(RadError::EmptyArray) } + .value() + .into_iter() + .map(|item| RadonString::try_from(item).unwrap_or_default().value()) + .collect(); + Ok(RadonTypes::from(RadonString::from( + string_list.join(separator.as_str()), + ))) + operator: "ArrayJoin".to_string(), + args: Some(args.to_vec()), + }), + _ => Err(RadError::EmptyArray), } } @@ -258,7 +269,7 @@ pub fn filter( pub fn pick( input: &RadonArray, args: &[Value], - _context: &mut ReportContext + _context: &mut ReportContext, ) -> Result { let not_found = |index: usize| RadError::ArrayIndexOutOfBounds { index: i32::try_from(index).unwrap(), @@ -286,7 +297,7 @@ pub fn pick( let index = from_value::(first_arg.clone()).map_err(|_| wrong_args())?; indexes.push(index); } - _ => return Err(wrong_args()) + _ => return Err(wrong_args()), }; } @@ -472,8 +483,8 @@ mod tests { operators::{ Operable, RadonOpCodes::{ - IntegerGreaterThan, IntegerMultiply, MapGetBoolean, MapGetFloat, MapGetInteger, - MapGetString, self, + self, IntegerGreaterThan, IntegerMultiply, MapGetBoolean, MapGetFloat, + MapGetInteger, MapGetString, }, }, types::{ diff --git a/rad/src/operators/bytes.rs b/rad/src/operators/bytes.rs index 76a0dc3c6..65ecce64f 100644 --- a/rad/src/operators/bytes.rs +++ b/rad/src/operators/bytes.rs @@ -5,7 +5,12 @@ use std::convert::TryFrom; use crate::{ error::RadError, hash_functions::{self, RadonHashFunctions}, - types::{bytes::{RadonBytes, RadonBytesEncoding}, string::RadonString, integer::RadonInteger, RadonType}, + types::{ + bytes::{RadonBytes, RadonBytesEncoding}, + integer::RadonInteger, + string::RadonString, + RadonType, + }, }; pub fn as_integer(input: &RadonBytes) -> Result { @@ -13,12 +18,14 @@ pub fn as_integer(input: &RadonBytes) -> Result { match input_value_len { 1..=16 => { let mut bytes_array = [0u8; 16]; - bytes_array[16 - input_value_len ..].copy_from_slice(&input.value()); + bytes_array[16 - input_value_len..].copy_from_slice(&input.value()); Ok(RadonInteger::from(i128::from_be_bytes(bytes_array))) } - 17.. => Err(RadError::ParseInt { message: "Input buffer too big".to_string() }), - _ => Err(RadError::EmptyArray) - } + 17.. => Err(RadError::ParseInt { + message: "Input buffer too big".to_string(), + }), + _ => Err(RadError::EmptyArray), + } } pub fn hash(input: &RadonBytes, args: &[Value]) -> Result { @@ -43,17 +50,21 @@ pub fn length(input: &RadonBytes) -> RadonInteger { } pub fn slice(input: &RadonBytes, args: &[Value]) -> Result { - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "BytesSlice".to_string(), args: args.to_vec(), }; let end_index = input.value().len(); if end_index > 0 { - let start_index = from_value::(args[0].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize; + let start_index = from_value::(args[0].clone()) + .unwrap_or_default() + .rem_euclid(end_index as i64) as usize; let mut slice = input.value().as_slice().split_at(start_index).1.to_vec(); if args.len() == 2 { - let end_index = from_value::(args[1].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize; + let end_index = from_value::(args[1].clone()) + .unwrap_or_default() + .rem_euclid(end_index as i64) as usize; slice.truncate(end_index - start_index); } Ok(RadonBytes::from(slice)) @@ -63,7 +74,7 @@ pub fn slice(input: &RadonBytes, args: &[Value]) -> Result } pub fn stringify(input: &RadonBytes, args: &Option>) -> Result { - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "Stringify".to_string(), args: args.to_owned().unwrap_or_default().to_vec(), @@ -74,20 +85,18 @@ pub fn stringify(input: &RadonBytes, args: &Option>) -> Result(arg).map_err(|_| wrong_args())?; - bytes_encoding = RadonBytesEncoding::try_from(bytes_encoding_u8).map_err(|_| wrong_args())?; + bytes_encoding = + RadonBytesEncoding::try_from(bytes_encoding_u8).map_err(|_| wrong_args())?; } } - _ => () + _ => (), } match bytes_encoding { - RadonBytesEncoding::Hex => { - RadonString::try_from(Value::Text(hex::encode(input.value()))) - } - RadonBytesEncoding::Base64 => { - RadonString::try_from(Value::Text(base64::engine::general_purpose::STANDARD.encode(input.value()))) - } + RadonBytesEncoding::Hex => RadonString::try_from(Value::Text(hex::encode(input.value()))), + RadonBytesEncoding::Base64 => RadonString::try_from(Value::Text( + base64::engine::general_purpose::STANDARD.encode(input.value()), + )), } - } #[cfg(test)] diff --git a/rad/src/operators/integer.rs b/rad/src/operators/integer.rs index 49b3a4b00..5fda41039 100644 --- a/rad/src/operators/integer.rs +++ b/rad/src/operators/integer.rs @@ -5,8 +5,8 @@ use serde_cbor::value::{from_value, Value}; use crate::{ error::RadError, types::{ - boolean::RadonBoolean, float::RadonFloat, integer::RadonInteger, string::RadonString, - RadonType, bytes::RadonBytes, + boolean::RadonBoolean, bytes::RadonBytes, float::RadonFloat, integer::RadonInteger, + string::RadonString, RadonType, }, }; @@ -26,7 +26,7 @@ pub fn to_bytes(input: RadonInteger) -> Result { let mut leading_zeros = 0; for i in 0..bytes_array.len() { if bytes_array[i] != 0u8 { - break + break; } else { leading_zeros += 1; } diff --git a/rad/src/operators/map.rs b/rad/src/operators/map.rs index b4bd959bf..b399c2584 100644 --- a/rad/src/operators/map.rs +++ b/rad/src/operators/map.rs @@ -1,4 +1,4 @@ -use std::{convert::TryInto, collections::BTreeMap}; +use std::{collections::BTreeMap, convert::TryInto}; use serde_cbor::value::{from_value, Value}; use witnet_data_structures::radon_report::ReportContext; @@ -6,14 +6,17 @@ use witnet_data_structures::radon_report::ReportContext; use crate::{ error::RadError, operators::string, - types::{array::RadonArray, map::RadonMap, string::RadonString, RadonType, RadonTypes}, - script::{RadonScriptExecutionSettings, execute_radon_script, unpack_subscript, partial_results_extract}, + script::{ + execute_radon_script, partial_results_extract, unpack_subscript, + RadonScriptExecutionSettings, + }, + types::{array::RadonArray, map::RadonMap, string::RadonString, RadonType, RadonTypes}, }; pub fn alter( - input: &RadonMap, + input: &RadonMap, args: &[Value], - context: &mut ReportContext + context: &mut ReportContext, ) -> Result { let wrong_args = || RadError::WrongArguments { input_type: RadonMap::radon_type_name(), @@ -33,7 +36,7 @@ pub fn alter( Value::Text(key) => { input_keys.push(key.clone()); } - _ => return Err(wrong_args()) + _ => return Err(wrong_args()), }; let subscript = args.get(1).ok_or_else(wrong_args)?; @@ -45,24 +48,22 @@ pub fn alter( inner: Box::new(e), }; let subscript = unpack_subscript(subscript).map_err(subscript_err)?; - - let not_found = |key_str: &str| RadError::MapKeyNotFound { key: String::from(key_str) }; + + let not_found = |key_str: &str| RadError::MapKeyNotFound { + key: String::from(key_str), + }; let input_map = input.value(); let mut output_map = input.value().clone(); let mut reports = vec![]; - + let settings = RadonScriptExecutionSettings::tailored_to_stage(&context.stage); for key in input_keys { let value = input_map .get(key.as_str()) .ok_or_else(|| not_found(key.as_str()))?; - let report = execute_radon_script( - value.clone(), - subscript.as_slice(), - context, - settings - )?; + let report = + execute_radon_script(value.clone(), subscript.as_slice(), context, settings)?; // If there is an error while altering value, short-circuit and bubble up the error as it comes // from the radon script execution if let RadonTypes::RadonError(error) = &report.result { @@ -71,14 +72,14 @@ pub fn alter( output_map.insert(key, report.result.clone()); } reports.push(report); - } - + } + // Extract the partial results from the reports and put them in the execution context if needed partial_results_extract(&subscript, &reports, context); Ok(RadonMap::from(output_map)) } - _ => Err(wrong_args()) + _ => Err(wrong_args()), } } @@ -156,10 +157,10 @@ pub fn values(input: &RadonMap) -> RadonArray { } pub fn pick(input: &RadonMap, args: &[Value]) -> Result { - let not_found = |key_str: &str| RadError::MapKeyNotFound { - key: String::from(key_str) + let not_found = |key_str: &str| RadError::MapKeyNotFound { + key: String::from(key_str), }; - + let wrong_args = || RadError::WrongArguments { input_type: RadonMap::radon_type_name(), operator: "Pick".to_string(), @@ -174,14 +175,15 @@ pub fn pick(input: &RadonMap, args: &[Value]) -> Result { match first_arg { Value::Array(keys) => { for key in keys.iter() { - let key_string = from_value::(key.to_owned()).map_err(|_| wrong_args())?; + let key_string = + from_value::(key.to_owned()).map_err(|_| wrong_args())?; input_keys.push(key_string); } } Value::Text(key) => { input_keys.push(key.clone()); } - _ => return Err(wrong_args()) + _ => return Err(wrong_args()), }; } @@ -190,18 +192,17 @@ pub fn pick(input: &RadonMap, args: &[Value]) -> Result { if let Some(value) = input.value().get(&key) { output_map.insert(key, value.clone()); } else { - return Err(not_found(key.as_str())) + return Err(not_found(key.as_str())); } } Ok(RadonMap::from(output_map)) } pub fn stringify(input: &RadonMap) -> Result { - let json_string = serde_json::to_string(&input.value()) - .map_err(|_| RadError::Decode { - from: "RadonMap", - to: "RadonString" - })?; + let json_string = serde_json::to_string(&input.value()).map_err(|_| RadError::Decode { + from: "RadonMap", + to: "RadonString", + })?; Ok(RadonString::from(json_string)) } diff --git a/rad/src/operators/mod.rs b/rad/src/operators/mod.rs index 9afdbb399..f169c2e2d 100644 --- a/rad/src/operators/mod.rs +++ b/rad/src/operators/mod.rs @@ -48,7 +48,7 @@ pub enum RadonOpCodes { BooleanNegate = 0x22, BooleanToString = 0x20, /////////////////////////////////////////////////////////////////////// - // Bytes operator codes (start at 0x30) + // Bytes operator codes (start at 0x30) BytesAsInteger = 0x32, BytesHash = 0x31, BytesLength = 0x34, diff --git a/rad/src/operators/string.rs b/rad/src/operators/string.rs index e4adc233f..b05b28f49 100644 --- a/rad/src/operators/string.rs +++ b/rad/src/operators/string.rs @@ -5,20 +5,26 @@ use std::{ }; use base64::Engine; +use jsonpath::Selector; use serde_cbor::value::{from_value, Value}; use serde_json::Value as JsonValue; -use jsonpath::Selector; -use slicestring::Slice; use regex::Regex; +use slicestring::Slice; use witnet_data_structures::radon_error::RadonError; use crate::{ error::RadError, hash_functions::{self, RadonHashFunctions}, types::{ - array::RadonArray, boolean::RadonBoolean, bytes::{RadonBytes, RadonBytesEncoding}, float::RadonFloat, - integer::RadonInteger, map::RadonMap, string::RadonString, RadonType, RadonTypes, + array::RadonArray, + boolean::RadonBoolean, + bytes::{RadonBytes, RadonBytesEncoding}, + float::RadonFloat, + integer::RadonInteger, + map::RadonMap, + string::RadonString, + RadonType, RadonTypes, }, }; @@ -34,7 +40,7 @@ pub fn as_bool(input: &RadonString) -> Result { } pub fn as_bytes(input: &RadonString, args: &Option>) -> Result { - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "AsBytes".to_string(), args: args.to_owned().unwrap_or_default().to_vec(), @@ -52,30 +58,27 @@ pub fn as_bytes(input: &RadonString, args: &Option>) -> Result(arg).map_err(|_| wrong_args())?; - bytes_encoding = RadonBytesEncoding::try_from(bytes_encoding_u8).map_err(|_| wrong_args())?; + bytes_encoding = + RadonBytesEncoding::try_from(bytes_encoding_u8).map_err(|_| wrong_args())?; } } - _ => () + _ => (), } match bytes_encoding { - RadonBytesEncoding::Hex => { - Ok(RadonBytes::from( - hex::decode(input_string.as_str()) + RadonBytesEncoding::Hex => Ok(RadonBytes::from( + hex::decode(input_string.as_str()).map_err(|_err| RadError::Decode { + from: "RadonString", + to: "RadonBytes", + })?, + )), + RadonBytesEncoding::Base64 => Ok(RadonBytes::from( + base64::engine::general_purpose::STANDARD + .decode(input.value()) .map_err(|_err| RadError::Decode { from: "RadonString", to: "RadonBytes", - })? - )) - } - RadonBytesEncoding::Base64 => { - Ok(RadonBytes::from( - base64::engine::general_purpose::STANDARD.decode(input.value()) - .map_err(|_err| RadError::Decode { - from: "RadonString", - to: "RadonBytes" - })? - )) - } + })?, + )), } } @@ -147,64 +150,69 @@ pub fn parse_json(input: &RadonString) -> Result { RadonTypes::try_from(json_value) } -pub fn parse_json_map(input: &RadonString, args: &Option>) -> Result { - let not_found = |json_path: &str| RadError::JsonPathNotFound { - path: String::from(json_path) +pub fn parse_json_map( + input: &RadonString, + args: &Option>, +) -> Result { + let not_found = |json_path: &str| RadError::JsonPathNotFound { + path: String::from(json_path), }; - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "ParseJsonMap".to_string(), args: args.to_owned().unwrap_or_default(), }; - let json_input: JsonValue = serde_json::from_str(&input.value()) - .map_err(|err| RadError::JsonParse { - description: err.to_string(), - })?; - + let json_input: JsonValue = + serde_json::from_str(&input.value()).map_err(|err| RadError::JsonParse { + description: err.to_string(), + })?; + match args.to_owned().unwrap_or_default().get(0) { Some(Value::Text(json_path)) => { - let selector = Selector::new(json_path.as_str()) - .map_err(|err| RadError::JsonPathParse { + let selector = + Selector::new(json_path.as_str()).map_err(|err| RadError::JsonPathParse { description: err.to_string(), })?; - let item = selector.find(&json_input) + let item = selector + .find(&json_input) .next() .ok_or_else(|| not_found(json_path.as_str()))?; RadonTypes::try_from(item.to_owned())?.try_into() - }, - None => { - RadonTypes::try_from(json_input)?.try_into() - }, - _ => Err(wrong_args()) + } + None => RadonTypes::try_from(json_input)?.try_into(), + _ => Err(wrong_args()), } } -pub fn parse_json_array(input: &RadonString, args: &Option>) -> Result { - let wrong_args = || RadError::WrongArguments { +pub fn parse_json_array( + input: &RadonString, + args: &Option>, +) -> Result { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "ParseJsonArray".to_string(), args: args.to_owned().unwrap_or_default(), }; - let json_input: JsonValue = serde_json::from_str(&input.value()) - .map_err(|err| RadError::JsonParse { - description: err.to_string(), - })?; + let json_input: JsonValue = + serde_json::from_str(&input.value()).map_err(|err| RadError::JsonParse { + description: err.to_string(), + })?; match args.to_owned().unwrap_or_default().get(0) { Some(Value::Array(values)) => { let mut items: Vec = vec![]; for path in values { if let Value::Text(json_path) = path { - let selector = Selector::new(json_path.as_str()) - .map_err(|err| RadError::JsonPathParse { + let selector = Selector::new(json_path.as_str()).map_err(|err| { + RadError::JsonPathParse { description: err.to_string(), - })?; - let mut subitems: Vec = selector.find(&json_input) - .map(into_radon_types) - .collect(); + } + })?; + let mut subitems: Vec = + selector.find(&json_input).map(into_radon_types).collect(); if subitems.len() > 1 { items.insert(items.len(), RadonArray::from(subitems).into()); } else { @@ -217,19 +225,15 @@ pub fn parse_json_array(input: &RadonString, args: &Option>) -> Resul Ok(RadonArray::from(items)) } Some(Value::Text(json_path)) => { - let selector = Selector::new(json_path.as_str()) - .map_err(|err| RadError::JsonPathParse { + let selector = + Selector::new(json_path.as_str()).map_err(|err| RadError::JsonPathParse { description: err.to_string(), })?; - let items: Vec = selector.find(&json_input) - .map(into_radon_types) - .collect(); + let items: Vec = selector.find(&json_input).map(into_radon_types).collect(); Ok(RadonArray::from(items)) } - None => { - RadonTypes::try_from(json_input)?.try_into() - } - _ => Err(wrong_args()) + None => RadonTypes::try_from(json_input)?.try_into(), + _ => Err(wrong_args()), } } @@ -239,9 +243,11 @@ fn into_radon_types(value: &serde_json::Value) -> RadonTypes { if value.is_f64() { RadonTypes::from(RadonFloat::from(value.as_f64().unwrap_or_default())) } else { - RadonTypes::from(RadonInteger::from(value.as_i64().unwrap_or_default() as i128)) + RadonTypes::from(RadonInteger::from( + value.as_i64().unwrap_or_default() as i128 + )) } - }, + } serde_json::Value::Bool(value) => RadonTypes::from(RadonBoolean::from(*value)), serde_json::Value::String(value) => RadonTypes::from(RadonString::from(value.clone())), serde_json::Value::Object(entries) => { @@ -252,13 +258,12 @@ fn into_radon_types(value: &serde_json::Value) -> RadonTypes { RadonTypes::from(RadonMap::from(object)) } serde_json::Value::Array(values) => { - let items: Vec = values - .iter() - .map(into_radon_types) - .collect(); + let items: Vec = values.iter().map(into_radon_types).collect(); RadonTypes::from(RadonArray::from(items)) } - _ => RadonTypes::from(RadonError::new(RadError::JsonParse { description: value.to_string() })) + _ => RadonTypes::from(RadonError::new(RadError::JsonParse { + description: value.to_string(), + })), } } @@ -372,20 +377,22 @@ pub fn radon_trim(input: &RadonString) -> String { } } - pub fn replace(input: &RadonString, args: &[Value]) -> Result { - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "StringReplace".to_string(), args: args.to_vec(), }; let regex = RadonString::try_from(args.first().ok_or_else(wrong_args)?.to_owned())?; let replacement = RadonString::try_from(args.get(1).ok_or_else(wrong_args)?.to_owned())?; - Ok(RadonString::from(input.value().as_str().replace(regex.value().as_str(), replacement.value().as_str()))) + Ok(RadonString::from(input.value().as_str().replace( + regex.value().as_str(), + replacement.value().as_str(), + ))) } pub fn slice(input: &RadonString, args: &[Value]) -> Result { - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "StringSlice".to_string(), args: args.to_vec(), @@ -393,26 +400,40 @@ pub fn slice(input: &RadonString, args: &[Value]) -> Result { - let start_index = from_value::(args[0].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize; - end_index = from_value::(args[1].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize; - Ok(RadonString::from(input.value().as_str().slice(start_index..end_index))) + let start_index = from_value::(args[0].clone()) + .unwrap_or_default() + .rem_euclid(end_index as i64) as usize; + end_index = from_value::(args[1].clone()) + .unwrap_or_default() + .rem_euclid(end_index as i64) as usize; + Ok(RadonString::from( + input.value().as_str().slice(start_index..end_index), + )) } 1 => { - let start_index = from_value::(args[0].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize; - Ok(RadonString::from(input.value().as_str().slice(start_index..end_index))) + let start_index = from_value::(args[0].clone()) + .unwrap_or_default() + .rem_euclid(end_index as i64) as usize; + Ok(RadonString::from( + input.value().as_str().slice(start_index..end_index), + )) } - _ => Err(wrong_args()) + _ => Err(wrong_args()), } } pub fn split(input: &RadonString, args: &[Value]) -> Result { - let wrong_args = || RadError::WrongArguments { + let wrong_args = || RadError::WrongArguments { input_type: RadonString::radon_type_name(), operator: "StringSplit".to_string(), args: args.to_vec(), }; let pattern = RadonString::try_from(args.first().ok_or_else(wrong_args)?.to_owned())?; - let parts: Vec = Regex::new(pattern.value().as_str()).unwrap().split(input.value().as_str()).map(|part| RadonTypes::from(RadonString::from(part))).collect(); + let parts: Vec = Regex::new(pattern.value().as_str()) + .unwrap() + .split(input.value().as_str()) + .map(|part| RadonTypes::from(RadonString::from(part))) + .collect(); Ok(RadonArray::from(parts)) } diff --git a/rad/src/script.rs b/rad/src/script.rs index a203ef477..33150cc66 100644 --- a/rad/src/script.rs +++ b/rad/src/script.rs @@ -1,8 +1,4 @@ -use std::{ - clone::Clone, - convert::TryFrom, - iter, -}; +use std::{clone::Clone, convert::TryFrom, iter}; use serde_cbor::{ self as cbor, diff --git a/rad/src/types/bytes.rs b/rad/src/types/bytes.rs index 6f9637f01..03f02f6e2 100644 --- a/rad/src/types/bytes.rs +++ b/rad/src/types/bytes.rs @@ -104,9 +104,9 @@ impl Operable for RadonBytes { (RadonOpCodes::BytesLength, None) => { Ok(RadonTypes::from(bytes_operators::length(self))) } - (RadonOpCodes::BytesSlice, Some(args)) => { - bytes_operators::slice(self, args.as_slice()).map(RadonTypes::from).map_err(Into::into) - }, + (RadonOpCodes::BytesSlice, Some(args)) => bytes_operators::slice(self, args.as_slice()) + .map(RadonTypes::from) + .map_err(Into::into), (RadonOpCodes::BytesStringify, args) => bytes_operators::stringify(self, args) .map(RadonTypes::from) .map_err(Into::into), diff --git a/rad/src/types/map.rs b/rad/src/types/map.rs index c86f294a1..ec9963d15 100644 --- a/rad/src/types/map.rs +++ b/rad/src/types/map.rs @@ -161,9 +161,15 @@ impl Operable for RadonMap { } (RadonOpCodes::MapKeys, None) => Ok(RadonTypes::from(map_operators::keys(self))), (RadonOpCodes::MapValues, None) => Ok(RadonTypes::from(map_operators::values(self))), - (RadonOpCodes::MapAlter, Some(args)) => map_operators::alter(self, args, context).map(RadonTypes::from), - (RadonOpCodes::MapPick, Some(args)) => map_operators::pick(self, args).map(RadonTypes::from), - (RadonOpCodes::MapStringify, None) => map_operators::stringify(self).map(RadonTypes::from), + (RadonOpCodes::MapAlter, Some(args)) => { + map_operators::alter(self, args, context).map(RadonTypes::from) + } + (RadonOpCodes::MapPick, Some(args)) => { + map_operators::pick(self, args).map(RadonTypes::from) + } + (RadonOpCodes::MapStringify, None) => { + map_operators::stringify(self).map(RadonTypes::from) + } (op_code, args) => Err(RadError::UnsupportedOperator { input_type: RADON_MAP_TYPE_NAME.to_string(), operator: op_code.to_string(), diff --git a/rad/src/types/string.rs b/rad/src/types/string.rs index 3148e5baf..be0f66982 100644 --- a/rad/src/types/string.rs +++ b/rad/src/types/string.rs @@ -126,17 +126,21 @@ impl Operable for RadonString { .map(RadonTypes::from) .map_err(Into::into), (RadonOpCodes::StringLength, None) => { - Ok(RadonTypes::from(string_operators::length(self))) - } + Ok(RadonTypes::from(string_operators::length(self))) + } (RadonOpCodes::StringMatch, Some(args)) => { string_operators::string_match(self, args.as_slice()).map(RadonTypes::from) } - (RadonOpCodes::StringParseJSONArray, args) => string_operators::parse_json_array(self, args) - .map(RadonTypes::from) - .map_err(Into::into), - (RadonOpCodes::StringParseJSONMap, args) => string_operators::parse_json_map(self, args) - .map(RadonTypes::from) - .map_err(Into::into), + (RadonOpCodes::StringParseJSONArray, args) => { + string_operators::parse_json_array(self, args) + .map(RadonTypes::from) + .map_err(Into::into) + } + (RadonOpCodes::StringParseJSONMap, args) => { + string_operators::parse_json_map(self, args) + .map(RadonTypes::from) + .map_err(Into::into) + } (RadonOpCodes::StringParseXMLMap, None) => string_operators::parse_xml_map(self) .map(RadonTypes::from) .map_err(Into::into), @@ -147,13 +151,19 @@ impl Operable for RadonString { Ok(RadonTypes::from(string_operators::to_uppercase(self))) } (RadonOpCodes::StringReplace, Some(args)) => { - string_operators::replace(self, args.as_slice()).map(RadonTypes::from).map_err(Into::into) + string_operators::replace(self, args.as_slice()) + .map(RadonTypes::from) + .map_err(Into::into) } (RadonOpCodes::StringSlice, Some(args)) => { - string_operators::slice(self, args.as_slice()).map(RadonTypes::from).map_err(Into::into) + string_operators::slice(self, args.as_slice()) + .map(RadonTypes::from) + .map_err(Into::into) } (RadonOpCodes::StringSplit, Some(args)) => { - string_operators::split(self, args.as_slice()).map(RadonTypes::from).map_err(Into::into) + string_operators::split(self, args.as_slice()) + .map(RadonTypes::from) + .map_err(Into::into) } (op_code, args) => Err(RadError::UnsupportedOperator { input_type: RADON_STRING_TYPE_NAME.to_string(),