novax_data/types/
option.rs1use multiversx_sc_codec::multi_types::OptionalValue;
2use multiversx_sc_codec::{NestedDecode, NestedEncode, TopDecode, TopEncode, TopEncodeMulti};
3use crate::types::managed::ManagedConvertible;
4use crate::types::native::NativeConvertible;
5
6impl<T: Clone + NativeConvertible> NativeConvertible for Option<T> {
7 type Native = Option<T::Native>;
8
9 fn to_native(&self) -> Self::Native {
10 self.clone().map(|e| e.to_native())
11 }
12}
13
14impl<T: Clone + NativeConvertible> NativeConvertible for OptionalValue<T> {
15 type Native = Option<T::Native>;
16
17 fn to_native(&self) -> Self::Native {
18 self.clone().into_option().to_native()
19 }
20}
21
22impl<N, T> ManagedConvertible<Option<T>> for Option<N>
23where
24 N: ManagedConvertible<T>,
25 T: TopEncode + TopDecode + NestedEncode + NestedDecode
26{
27 fn to_managed(&self) -> Option<T> {
28 self.as_ref().map(|e| e.to_managed())
29 }
30}
31
32impl<N, T> ManagedConvertible<OptionalValue<T>> for Option<N>
33 where
34 N: ManagedConvertible<T>,
35 T: TopEncodeMulti
36{
37 fn to_managed(&self) -> OptionalValue<T> {
38 OptionalValue::from(self.as_ref().map(|e| e.to_managed()))
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use multiversx_sc::types::ManagedBuffer;
45 use multiversx_sc_codec::multi_types::OptionalValue;
46 use multiversx_sc_scenario::api::StaticApi;
47 use crate::types::managed::ManagedConvertible;
48 use crate::types::native::NativeConvertible;
49
50 #[test]
51 fn test_none_option_to_managed_option() {
52 let option: Option<String> = None;
53 let managed: Option<ManagedBuffer<StaticApi>> = option.to_managed();
54
55 assert!(managed.is_none());
56 }
57
58 #[test]
59 fn test_some_option_to_managed_option() {
60 let option: Option<String> = Some("some".to_string());
61 let managed: Option<ManagedBuffer<StaticApi>> = option.to_managed();
62 let expected = Some(ManagedBuffer::<StaticApi>::from("some"));
63
64 assert_eq!(
65 managed,
66 expected
67 );
68 }
69
70 #[test]
71 fn test_none_option_to_managed_optional_value() {
72 let option: Option<String> = None;
73 let managed: OptionalValue<ManagedBuffer<StaticApi>> = option.to_managed();
74
75 assert!(managed.is_none());
76 }
77
78 #[test]
79 fn test_some_option_to_managed_optional_value() {
80 let option: Option<String> = Some("some".to_string());
81 let managed: OptionalValue<ManagedBuffer<StaticApi>> = option.to_managed();
82 let expected = OptionalValue::Some(ManagedBuffer::<StaticApi>::from("some"));
83
84 assert_eq!(
85 managed.into_option().unwrap(),
86 expected.into_option().unwrap()
87 );
88 }
89
90 #[test]
91 fn test_none_option_to_native() {
92 trait IsString {
93 fn is_string(&self) -> bool;
94 }
95
96 impl IsString for Option<ManagedBuffer<StaticApi>> {
97 fn is_string(&self) -> bool {
98 false
99 }
100 }
101
102 impl IsString for Option<String> {
103 fn is_string(&self) -> bool {
104 true
105 }
106 }
107
108 let result: Option<ManagedBuffer<StaticApi>> = None;
109 let native = result.to_native();
110
111 assert!(!result.is_string());
112 assert!(native.is_string());
113 assert!(native.is_none());
114 }
115
116 #[test]
117 fn test_some_option_to_native() {
118 let buffer: ManagedBuffer<StaticApi> = ManagedBuffer::from("some");
119 let result: Option<ManagedBuffer<StaticApi>> = Some(buffer);
120 let native = result.to_native();
121
122 let expected_result = String::from("some");
123
124 assert_eq!(
125 native.unwrap(),
126 expected_result
127 );
128 }
129
130 #[test]
131 fn test_none_optional_value_to_native() {
132 trait IsString {
133 fn is_string(&self) -> bool;
134 }
135
136 impl IsString for OptionalValue<ManagedBuffer<StaticApi>> {
137 fn is_string(&self) -> bool {
138 false
139 }
140 }
141
142 impl IsString for Option<String> {
143 fn is_string(&self) -> bool {
144 true
145 }
146 }
147
148 let result: OptionalValue<ManagedBuffer<StaticApi>> = OptionalValue::None;
149 let native = result.to_native();
150
151 assert!(!result.is_string());
152 assert!(native.is_string());
153 assert!(native.is_none());
154 }
155
156 #[test]
157 fn test_some_optional_value_to_native() {
158 let buffer: ManagedBuffer<StaticApi> = ManagedBuffer::from("some");
159 let result: OptionalValue<ManagedBuffer<StaticApi>> = OptionalValue::Some(buffer);
160 let native = result.to_native();
161
162 let expected_result = String::from("some");
163
164 assert_eq!(
165 native.unwrap(),
166 expected_result
167 );
168 }
169}