forked from witnet/witnet-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.rs
More file actions
128 lines (111 loc) · 3.52 KB
/
Copy pathbytes.rs
File metadata and controls
128 lines (111 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{
error::RadError,
operators::{Operable, RadonOpCodes, bytes as bytes_operators, identity},
script::RadonCall,
types::{RadonType, RadonTypes},
};
use num_enum::TryFromPrimitive;
use serde::Serialize;
use serde_cbor::value::Value;
use std::{
convert::{TryFrom, TryInto},
fmt,
};
use witnet_data_structures::radon_report::ReportContext;
const RADON_BYTES_TYPE_NAME: &str = "RadonBytes";
/// List of support string-encoding algorithms for buffers
#[derive(Debug, Default, PartialEq, Eq, Serialize, TryFromPrimitive)]
#[repr(u8)]
pub enum RadonBytesEncoding {
#[default]
Hex = 0,
Base64 = 1,
Utf8 = 2,
}
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct RadonBytes {
value: Vec<u8>,
}
impl RadonType<Vec<u8>> for RadonBytes {
fn value(&self) -> Vec<u8> {
self.value.clone()
}
#[inline]
fn radon_type_name() -> &'static str {
RADON_BYTES_TYPE_NAME
}
}
impl TryFrom<Value> for RadonBytes {
type Error = RadError;
fn try_from(value: Value) -> Result<Self, Self::Error> {
let error = || RadError::Decode {
from: "cbor::value::Value",
to: RadonBytes::radon_type_name(),
};
match value {
Value::Bytes(bytes_value) => Ok(Self::from(bytes_value)),
_ => Err(error()),
}
}
}
impl TryFrom<RadonTypes> for RadonBytes {
type Error = RadError;
fn try_from(item: RadonTypes) -> Result<Self, Self::Error> {
if let RadonTypes::Bytes(rad_bytes) = item {
Ok(rad_bytes)
} else {
let value = Value::try_from(item)?;
value.try_into()
}
}
}
impl TryInto<Value> for RadonBytes {
type Error = RadError;
fn try_into(self) -> Result<Value, Self::Error> {
Ok(Value::from(self.value()))
}
}
impl From<Vec<u8>> for RadonBytes {
fn from(value: Vec<u8>) -> Self {
RadonBytes { value }
}
}
impl fmt::Display for RadonBytes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let hex_value = hex::encode(&self.value);
write!(f, "{RADON_BYTES_TYPE_NAME}({hex_value:?})")
}
}
impl Operable for RadonBytes {
fn operate(&self, call: &RadonCall) -> Result<RadonTypes, RadError> {
match call {
// Identity
(RadonOpCodes::Identity, None) => identity(RadonTypes::from(self.clone())),
(RadonOpCodes::BytesAsInteger, None) => bytes_operators::as_integer(self)
.map(RadonTypes::from),
(RadonOpCodes::BytesLength, None) => {
Ok(RadonTypes::from(bytes_operators::length(self)))
}
(RadonOpCodes::BytesHash, Some(args)) => {
bytes_operators::hash(self, args.as_slice()).map(RadonTypes::from)
}
(RadonOpCodes::BytesSlice, Some(args)) => bytes_operators::slice(self, args.as_slice())
.map(RadonTypes::from),
(RadonOpCodes::BytesToString, args) => bytes_operators::to_string(self, args)
.map(RadonTypes::from),
// Unsupported / unimplemented
(op_code, args) => Err(RadError::UnsupportedOperator {
input_type: RADON_BYTES_TYPE_NAME.to_string(),
operator: op_code.to_string(),
args: args.to_owned(),
}),
}
}
fn operate_in_context(
&self,
call: &RadonCall,
_context: &mut ReportContext<RadonTypes>,
) -> Result<RadonTypes, RadError> {
self.operate(call)
}
}