hyperlight_host/func/
param_type.rs1use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterType, ParameterValue};
18use tracing::{instrument, Span};
19
20use crate::HyperlightError::ParameterValueConversionFailure;
21use crate::{log_then_return, Result};
22
23pub trait SupportedParameterType<T> {
29 fn get_hyperlight_type() -> ParameterType;
32 fn get_hyperlight_value(&self) -> ParameterValue;
35 fn get_inner(a: ParameterValue) -> Result<T>;
37}
38
39impl SupportedParameterType<String> for String {
41 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
42 fn get_hyperlight_type() -> ParameterType {
43 ParameterType::String
44 }
45
46 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
47 fn get_hyperlight_value(&self) -> ParameterValue {
48 ParameterValue::String(self.clone())
49 }
50
51 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
52 fn get_inner(a: ParameterValue) -> Result<String> {
53 match a {
54 ParameterValue::String(i) => Ok(i),
55 other => {
56 log_then_return!(ParameterValueConversionFailure(other.clone(), "String"));
57 }
58 }
59 }
60}
61
62impl SupportedParameterType<i32> for i32 {
63 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
64 fn get_hyperlight_type() -> ParameterType {
65 ParameterType::Int
66 }
67
68 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
69 fn get_hyperlight_value(&self) -> ParameterValue {
70 ParameterValue::Int(*self)
71 }
72
73 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
74 fn get_inner(a: ParameterValue) -> Result<i32> {
75 match a {
76 ParameterValue::Int(i) => Ok(i),
77 other => {
78 log_then_return!(ParameterValueConversionFailure(other.clone(), "i32"));
79 }
80 }
81 }
82}
83
84impl SupportedParameterType<u32> for u32 {
85 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
86 fn get_hyperlight_type() -> ParameterType {
87 ParameterType::UInt
88 }
89
90 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
91 fn get_hyperlight_value(&self) -> ParameterValue {
92 ParameterValue::UInt(*self)
93 }
94
95 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
96 fn get_inner(a: ParameterValue) -> Result<u32> {
97 match a {
98 ParameterValue::UInt(ui) => Ok(ui),
99 other => {
100 log_then_return!(ParameterValueConversionFailure(other.clone(), "u32"));
101 }
102 }
103 }
104}
105
106impl SupportedParameterType<i64> for i64 {
107 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
108 fn get_hyperlight_type() -> ParameterType {
109 ParameterType::Long
110 }
111
112 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
113 fn get_hyperlight_value(&self) -> ParameterValue {
114 ParameterValue::Long(*self)
115 }
116
117 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
118 fn get_inner(a: ParameterValue) -> Result<i64> {
119 match a {
120 ParameterValue::Long(l) => Ok(l),
121 other => {
122 log_then_return!(ParameterValueConversionFailure(other.clone(), "i64"));
123 }
124 }
125 }
126}
127
128impl SupportedParameterType<u64> for u64 {
129 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
130 fn get_hyperlight_type() -> ParameterType {
131 ParameterType::ULong
132 }
133
134 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
135 fn get_hyperlight_value(&self) -> ParameterValue {
136 ParameterValue::ULong(*self)
137 }
138
139 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
140 fn get_inner(a: ParameterValue) -> Result<u64> {
141 match a {
142 ParameterValue::ULong(ul) => Ok(ul),
143 other => {
144 log_then_return!(ParameterValueConversionFailure(other.clone(), "u64"));
145 }
146 }
147 }
148}
149
150impl SupportedParameterType<bool> for bool {
151 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
152 fn get_hyperlight_type() -> ParameterType {
153 ParameterType::Bool
154 }
155
156 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
157 fn get_hyperlight_value(&self) -> ParameterValue {
158 ParameterValue::Bool(*self)
159 }
160
161 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
162 fn get_inner(a: ParameterValue) -> Result<bool> {
163 match a {
164 ParameterValue::Bool(i) => Ok(i),
165 other => {
166 log_then_return!(ParameterValueConversionFailure(other.clone(), "bool"));
167 }
168 }
169 }
170}
171
172impl SupportedParameterType<Vec<u8>> for Vec<u8> {
173 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
174 fn get_hyperlight_type() -> ParameterType {
175 ParameterType::VecBytes
176 }
177
178 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
179 fn get_hyperlight_value(&self) -> ParameterValue {
180 ParameterValue::VecBytes(self.clone())
181 }
182
183 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
184 fn get_inner(a: ParameterValue) -> Result<Vec<u8>> {
185 match a {
186 ParameterValue::VecBytes(i) => Ok(i),
187 other => {
188 log_then_return!(ParameterValueConversionFailure(other.clone(), "Vec<u8>"));
189 }
190 }
191 }
192}