Performs a type conversion on the target.
(ftx: &FunctionContext, This(this): This<Value>)
| 175 | |
| 176 | // Performs a type conversion on the target. |
| 177 | pub fn uint(ftx: &FunctionContext, This(this): This<Value>) -> Result<Value> { |
| 178 | Ok(match this { |
| 179 | Value::String(v) => v |
| 180 | .parse::<u64>() |
| 181 | .map(Value::UInt) |
| 182 | .map_err(|e| ftx.error(format!("string parse error: {e}")))?, |
| 183 | Value::Float(v) => { |
| 184 | if v > u64::MAX as f64 || v < u64::MIN as f64 { |
| 185 | return Err(ftx.error("unsigned integer overflow")); |
| 186 | } |
| 187 | Value::UInt(v as u64) |
| 188 | } |
| 189 | Value::Int(v) => Value::UInt( |
| 190 | v.try_into() |
| 191 | .map_err(|_| ftx.error("unsigned integer overflow"))?, |
| 192 | ), |
| 193 | Value::UInt(v) => Value::UInt(v), |
| 194 | v => return Err(ftx.error(format!("cannot convert {v:?} to uint"))), |
| 195 | }) |
| 196 | } |
| 197 | |
| 198 | // Performs a type conversion on the target. |
| 199 | pub fn int(ftx: &FunctionContext, This(this): This<Value>) -> Result<Value> { |