Skip to content

Commit d03ce84

Browse files
committed
chore: cargo fmt
1 parent 2506613 commit d03ce84

File tree

12 files changed

+226
-169
lines changed

12 files changed

+226
-169
lines changed

rad/src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,10 @@ impl RadError {
413413
})
414414
}
415415
}
416-
None => Err(RadError::DecodeRadonErrorEmptyArray),
416+
None => {
417+
println!("None");
418+
Err(RadError::DecodeRadonErrorEmptyArray)
419+
}
417420
}
418421
}
419422

rad/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ async fn http_response(
299299
let mut response_bytes = Vec::<u8>::default();
300300

301301
// todo: before reading the response buffer, an error should be thrown if it was too big
302-
body.read_to_end(&mut response_bytes).await.map_err(|x| {
303-
RadError::HttpOther {
302+
body.read_to_end(&mut response_bytes)
303+
.await
304+
.map_err(|x| RadError::HttpOther {
304305
message: x.to_string(),
305-
}
306-
})?;
306+
})?;
307307
response = RadonTypes::from(RadonBytes::from(response_bytes));
308308
} else {
309309
// response is a string
@@ -323,8 +323,8 @@ async fn http_response(
323323
response = RadonTypes::from(RadonString::from(response_string));
324324
}
325325

326-
let result = handle_response_with_data_report(retrieve, response, context, settings)
327-
.map(|report| {
326+
let result =
327+
handle_response_with_data_report(retrieve, response, context, settings).map(|report| {
328328
let completion_ts = std::time::SystemTime::now();
329329
RadonReport {
330330
context: ReportContext {

rad/src/operators/array.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
clone::Clone,
3-
convert::{TryFrom, TryInto}
3+
convert::{TryFrom, TryInto},
44
};
55

66
use serde_cbor::value::{from_value, Value};
@@ -11,7 +11,10 @@ use crate::{
1111
filters::{self, RadonFilters},
1212
operators::string,
1313
reducers::{self, RadonReducers},
14-
script::{execute_radon_script, unpack_subscript, RadonScriptExecutionSettings, partial_results_extract},
14+
script::{
15+
execute_radon_script, partial_results_extract, unpack_subscript,
16+
RadonScriptExecutionSettings,
17+
},
1518
types::{array::RadonArray, integer::RadonInteger, string::RadonString, RadonType, RadonTypes},
1619
};
1720

@@ -108,10 +111,7 @@ fn get_numeric_string(input: &RadonArray, args: &[Value]) -> Result<RadonString,
108111
)))
109112
}
110113

111-
pub fn join(
112-
input: &RadonArray,
113-
args: &[Value]
114-
) -> Result<RadonTypes, RadError> {
114+
pub fn join(input: &RadonArray, args: &[Value]) -> Result<RadonTypes, RadError> {
115115
// Join not applicable if the input array is not homogeneous
116116
if !input.is_homogeneous() {
117117
return Err(RadError::UnsupportedOpNonHomogeneous {
@@ -140,6 +140,17 @@ pub fn join(
140140
_ => {
141141
Err(RadError::EmptyArray)
142142
}
143+
.value()
144+
.into_iter()
145+
.map(|item| RadonString::try_from(item).unwrap_or_default().value())
146+
.collect();
147+
Ok(RadonTypes::from(RadonString::from(
148+
string_list.join(separator.as_str()),
149+
)))
150+
operator: "ArrayJoin".to_string(),
151+
args: Some(args.to_vec()),
152+
}),
153+
_ => Err(RadError::EmptyArray),
143154
}
144155
}
145156

@@ -258,7 +269,7 @@ pub fn filter(
258269
pub fn pick(
259270
input: &RadonArray,
260271
args: &[Value],
261-
_context: &mut ReportContext<RadonTypes>
272+
_context: &mut ReportContext<RadonTypes>,
262273
) -> Result<RadonTypes, RadError> {
263274
let not_found = |index: usize| RadError::ArrayIndexOutOfBounds {
264275
index: i32::try_from(index).unwrap(),
@@ -286,7 +297,7 @@ pub fn pick(
286297
let index = from_value::<usize>(first_arg.clone()).map_err(|_| wrong_args())?;
287298
indexes.push(index);
288299
}
289-
_ => return Err(wrong_args())
300+
_ => return Err(wrong_args()),
290301
};
291302
}
292303

@@ -472,8 +483,8 @@ mod tests {
472483
operators::{
473484
Operable,
474485
RadonOpCodes::{
475-
IntegerGreaterThan, IntegerMultiply, MapGetBoolean, MapGetFloat, MapGetInteger,
476-
MapGetString, self,
486+
self, IntegerGreaterThan, IntegerMultiply, MapGetBoolean, MapGetFloat,
487+
MapGetInteger, MapGetString,
477488
},
478489
},
479490
types::{

rad/src/operators/bytes.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@ use std::convert::TryFrom;
55
use crate::{
66
error::RadError,
77
hash_functions::{self, RadonHashFunctions},
8-
types::{bytes::{RadonBytes, RadonBytesEncoding}, string::RadonString, integer::RadonInteger, RadonType},
8+
types::{
9+
bytes::{RadonBytes, RadonBytesEncoding},
10+
integer::RadonInteger,
11+
string::RadonString,
12+
RadonType,
13+
},
914
};
1015

1116
pub fn as_integer(input: &RadonBytes) -> Result<RadonInteger, RadError> {
1217
let input_value_len = input.value().len();
1318
match input_value_len {
1419
1..=16 => {
1520
let mut bytes_array = [0u8; 16];
16-
bytes_array[16 - input_value_len ..].copy_from_slice(&input.value());
21+
bytes_array[16 - input_value_len..].copy_from_slice(&input.value());
1722
Ok(RadonInteger::from(i128::from_be_bytes(bytes_array)))
1823
}
19-
17.. => Err(RadError::ParseInt { message: "Input buffer too big".to_string() }),
20-
_ => Err(RadError::EmptyArray)
21-
}
24+
17.. => Err(RadError::ParseInt {
25+
message: "Input buffer too big".to_string(),
26+
}),
27+
_ => Err(RadError::EmptyArray),
28+
}
2229
}
2330

2431
pub fn hash(input: &RadonBytes, args: &[Value]) -> Result<RadonBytes, RadError> {
@@ -43,17 +50,21 @@ pub fn length(input: &RadonBytes) -> RadonInteger {
4350
}
4451

4552
pub fn slice(input: &RadonBytes, args: &[Value]) -> Result<RadonBytes, RadError> {
46-
let wrong_args = || RadError::WrongArguments {
53+
let wrong_args = || RadError::WrongArguments {
4754
input_type: RadonString::radon_type_name(),
4855
operator: "BytesSlice".to_string(),
4956
args: args.to_vec(),
5057
};
5158
let end_index = input.value().len();
5259
if end_index > 0 {
53-
let start_index = from_value::<i64>(args[0].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize;
60+
let start_index = from_value::<i64>(args[0].clone())
61+
.unwrap_or_default()
62+
.rem_euclid(end_index as i64) as usize;
5463
let mut slice = input.value().as_slice().split_at(start_index).1.to_vec();
5564
if args.len() == 2 {
56-
let end_index = from_value::<i64>(args[1].clone()).unwrap_or_default().rem_euclid(end_index as i64) as usize;
65+
let end_index = from_value::<i64>(args[1].clone())
66+
.unwrap_or_default()
67+
.rem_euclid(end_index as i64) as usize;
5768
slice.truncate(end_index - start_index);
5869
}
5970
Ok(RadonBytes::from(slice))
@@ -63,7 +74,7 @@ pub fn slice(input: &RadonBytes, args: &[Value]) -> Result<RadonBytes, RadError>
6374
}
6475

6576
pub fn stringify(input: &RadonBytes, args: &Option<Vec<Value>>) -> Result<RadonString, RadError> {
66-
let wrong_args = || RadError::WrongArguments {
77+
let wrong_args = || RadError::WrongArguments {
6778
input_type: RadonString::radon_type_name(),
6879
operator: "Stringify".to_string(),
6980
args: args.to_owned().unwrap_or_default().to_vec(),
@@ -74,20 +85,18 @@ pub fn stringify(input: &RadonBytes, args: &Option<Vec<Value>>) -> Result<RadonS
7485
if !args.is_empty() {
7586
let arg = args.first().ok_or_else(wrong_args)?.to_owned();
7687
let bytes_encoding_u8 = from_value::<u8>(arg).map_err(|_| wrong_args())?;
77-
bytes_encoding = RadonBytesEncoding::try_from(bytes_encoding_u8).map_err(|_| wrong_args())?;
88+
bytes_encoding =
89+
RadonBytesEncoding::try_from(bytes_encoding_u8).map_err(|_| wrong_args())?;
7890
}
7991
}
80-
_ => ()
92+
_ => (),
8193
}
8294
match bytes_encoding {
83-
RadonBytesEncoding::Hex => {
84-
RadonString::try_from(Value::Text(hex::encode(input.value())))
85-
}
86-
RadonBytesEncoding::Base64 => {
87-
RadonString::try_from(Value::Text(base64::engine::general_purpose::STANDARD.encode(input.value())))
88-
}
95+
RadonBytesEncoding::Hex => RadonString::try_from(Value::Text(hex::encode(input.value()))),
96+
RadonBytesEncoding::Base64 => RadonString::try_from(Value::Text(
97+
base64::engine::general_purpose::STANDARD.encode(input.value()),
98+
)),
8999
}
90-
91100
}
92101

93102
#[cfg(test)]

rad/src/operators/integer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use serde_cbor::value::{from_value, Value};
55
use crate::{
66
error::RadError,
77
types::{
8-
boolean::RadonBoolean, float::RadonFloat, integer::RadonInteger, string::RadonString,
9-
RadonType, bytes::RadonBytes,
8+
boolean::RadonBoolean, bytes::RadonBytes, float::RadonFloat, integer::RadonInteger,
9+
string::RadonString, RadonType,
1010
},
1111
};
1212

@@ -26,7 +26,7 @@ pub fn to_bytes(input: RadonInteger) -> Result<RadonBytes, RadError> {
2626
let mut leading_zeros = 0;
2727
for i in 0..bytes_array.len() {
2828
if bytes_array[i] != 0u8 {
29-
break
29+
break;
3030
} else {
3131
leading_zeros += 1;
3232
}

rad/src/operators/map.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
use std::{convert::TryInto, collections::BTreeMap};
1+
use std::{collections::BTreeMap, convert::TryInto};
22

33
use serde_cbor::value::{from_value, Value};
44
use witnet_data_structures::radon_report::ReportContext;
55

66
use crate::{
77
error::RadError,
88
operators::string,
9-
types::{array::RadonArray, map::RadonMap, string::RadonString, RadonType, RadonTypes},
10-
script::{RadonScriptExecutionSettings, execute_radon_script, unpack_subscript, partial_results_extract},
9+
script::{
10+
execute_radon_script, partial_results_extract, unpack_subscript,
11+
RadonScriptExecutionSettings,
12+
},
13+
types::{array::RadonArray, map::RadonMap, string::RadonString, RadonType, RadonTypes},
1114
};
1215

1316
pub fn alter(
14-
input: &RadonMap,
17+
input: &RadonMap,
1518
args: &[Value],
16-
context: &mut ReportContext<RadonTypes>
19+
context: &mut ReportContext<RadonTypes>,
1720
) -> Result<RadonMap, RadError> {
1821
let wrong_args = || RadError::WrongArguments {
1922
input_type: RadonMap::radon_type_name(),
@@ -33,7 +36,7 @@ pub fn alter(
3336
Value::Text(key) => {
3437
input_keys.push(key.clone());
3538
}
36-
_ => return Err(wrong_args())
39+
_ => return Err(wrong_args()),
3740
};
3841

3942
let subscript = args.get(1).ok_or_else(wrong_args)?;
@@ -45,24 +48,22 @@ pub fn alter(
4548
inner: Box::new(e),
4649
};
4750
let subscript = unpack_subscript(subscript).map_err(subscript_err)?;
48-
49-
let not_found = |key_str: &str| RadError::MapKeyNotFound { key: String::from(key_str) };
51+
52+
let not_found = |key_str: &str| RadError::MapKeyNotFound {
53+
key: String::from(key_str),
54+
};
5055

5156
let input_map = input.value();
5257
let mut output_map = input.value().clone();
5358
let mut reports = vec![];
54-
59+
5560
let settings = RadonScriptExecutionSettings::tailored_to_stage(&context.stage);
5661
for key in input_keys {
5762
let value = input_map
5863
.get(key.as_str())
5964
.ok_or_else(|| not_found(key.as_str()))?;
60-
let report = execute_radon_script(
61-
value.clone(),
62-
subscript.as_slice(),
63-
context,
64-
settings
65-
)?;
65+
let report =
66+
execute_radon_script(value.clone(), subscript.as_slice(), context, settings)?;
6667
// If there is an error while altering value, short-circuit and bubble up the error as it comes
6768
// from the radon script execution
6869
if let RadonTypes::RadonError(error) = &report.result {
@@ -71,14 +72,14 @@ pub fn alter(
7172
output_map.insert(key, report.result.clone());
7273
}
7374
reports.push(report);
74-
}
75-
75+
}
76+
7677
// Extract the partial results from the reports and put them in the execution context if needed
7778
partial_results_extract(&subscript, &reports, context);
7879

7980
Ok(RadonMap::from(output_map))
8081
}
81-
_ => Err(wrong_args())
82+
_ => Err(wrong_args()),
8283
}
8384
}
8485

@@ -156,10 +157,10 @@ pub fn values(input: &RadonMap) -> RadonArray {
156157
}
157158

158159
pub fn pick(input: &RadonMap, args: &[Value]) -> Result<RadonMap, RadError> {
159-
let not_found = |key_str: &str| RadError::MapKeyNotFound {
160-
key: String::from(key_str)
160+
let not_found = |key_str: &str| RadError::MapKeyNotFound {
161+
key: String::from(key_str),
161162
};
162-
163+
163164
let wrong_args = || RadError::WrongArguments {
164165
input_type: RadonMap::radon_type_name(),
165166
operator: "Pick".to_string(),
@@ -174,14 +175,15 @@ pub fn pick(input: &RadonMap, args: &[Value]) -> Result<RadonMap, RadError> {
174175
match first_arg {
175176
Value::Array(keys) => {
176177
for key in keys.iter() {
177-
let key_string = from_value::<String>(key.to_owned()).map_err(|_| wrong_args())?;
178+
let key_string =
179+
from_value::<String>(key.to_owned()).map_err(|_| wrong_args())?;
178180
input_keys.push(key_string);
179181
}
180182
}
181183
Value::Text(key) => {
182184
input_keys.push(key.clone());
183185
}
184-
_ => return Err(wrong_args())
186+
_ => return Err(wrong_args()),
185187
};
186188
}
187189

@@ -190,18 +192,17 @@ pub fn pick(input: &RadonMap, args: &[Value]) -> Result<RadonMap, RadError> {
190192
if let Some(value) = input.value().get(&key) {
191193
output_map.insert(key, value.clone());
192194
} else {
193-
return Err(not_found(key.as_str()))
195+
return Err(not_found(key.as_str()));
194196
}
195197
}
196198
Ok(RadonMap::from(output_map))
197199
}
198200

199201
pub fn stringify(input: &RadonMap) -> Result<RadonString, RadError> {
200-
let json_string = serde_json::to_string(&input.value())
201-
.map_err(|_| RadError::Decode {
202-
from: "RadonMap",
203-
to: "RadonString"
204-
})?;
202+
let json_string = serde_json::to_string(&input.value()).map_err(|_| RadError::Decode {
203+
from: "RadonMap",
204+
to: "RadonString",
205+
})?;
205206
Ok(RadonString::from(json_string))
206207
}
207208

rad/src/operators/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum RadonOpCodes {
4848
BooleanNegate = 0x22,
4949
BooleanToString = 0x20,
5050
///////////////////////////////////////////////////////////////////////
51-
// Bytes operator codes (start at 0x30)
51+
// Bytes operator codes (start at 0x30)
5252
BytesAsInteger = 0x32,
5353
BytesHash = 0x31,
5454
BytesLength = 0x34,

0 commit comments

Comments
 (0)