|
| 1 | +use tucana::shared::{value::Kind, Value}; |
| 2 | + |
| 3 | +use crate::{context::Context, error::RuntimeError, registry::HandlerFn}; |
| 4 | + |
| 5 | +pub fn collect() -> Vec<(&'static str, HandlerFn)> { |
| 6 | + vec![ |
| 7 | + ("std::number::add", add), |
| 8 | + ("std::number::multiply", multiply), |
| 9 | + ("std::number::substract", substract), |
| 10 | + ("std::number::devide", devide), |
| 11 | + ("std::number::modulo", modulo), |
| 12 | + ("std::number::abs", abs), |
| 13 | + ("std::number::is_positive", is_positive), |
| 14 | + ("std::number::is_greater", is_greater), |
| 15 | + ("std::number::is_less", is_less), |
| 16 | + ("std::number::is_zero", is_zero), |
| 17 | + ("std::number::from_text", from_text), |
| 18 | + ("std::number::as_text", as_text), |
| 19 | + ] |
| 20 | +} |
| 21 | + |
| 22 | +fn add(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 23 | + let [Value { |
| 24 | + kind: Some(Kind::NumberValue(lhs)), |
| 25 | + .. |
| 26 | + }, Value { |
| 27 | + kind: Some(Kind::NumberValue(rhs)), |
| 28 | + .. |
| 29 | + }] = values |
| 30 | + else { |
| 31 | + return Err(RuntimeError::default()); |
| 32 | + }; |
| 33 | + |
| 34 | + Ok(Value { |
| 35 | + kind: Some(Kind::NumberValue(lhs + rhs)), |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +fn multiply(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 40 | + let [Value { |
| 41 | + kind: Some(Kind::NumberValue(lhs)), |
| 42 | + .. |
| 43 | + }, Value { |
| 44 | + kind: Some(Kind::NumberValue(rhs)), |
| 45 | + .. |
| 46 | + }] = values |
| 47 | + else { |
| 48 | + return Err(RuntimeError::default()); |
| 49 | + }; |
| 50 | + |
| 51 | + Ok(Value { |
| 52 | + kind: Some(Kind::NumberValue(lhs * rhs)), |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +fn substract(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 57 | + let [Value { |
| 58 | + kind: Some(Kind::NumberValue(lhs)), |
| 59 | + .. |
| 60 | + }, Value { |
| 61 | + kind: Some(Kind::NumberValue(rhs)), |
| 62 | + .. |
| 63 | + }] = values |
| 64 | + else { |
| 65 | + return Err(RuntimeError::default()); |
| 66 | + }; |
| 67 | + |
| 68 | + Ok(Value { |
| 69 | + kind: Some(Kind::NumberValue(lhs - rhs)), |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +fn devide(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 74 | + let [Value { |
| 75 | + kind: Some(Kind::NumberValue(lhs)), |
| 76 | + .. |
| 77 | + }, Value { |
| 78 | + kind: Some(Kind::NumberValue(rhs)), |
| 79 | + .. |
| 80 | + }] = values |
| 81 | + else { |
| 82 | + return Err(RuntimeError::default()); |
| 83 | + }; |
| 84 | + |
| 85 | + Ok(Value { |
| 86 | + kind: Some(Kind::NumberValue(lhs / rhs)), |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +fn modulo(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 91 | + let [Value { |
| 92 | + kind: Some(Kind::NumberValue(lhs)), |
| 93 | + .. |
| 94 | + }, Value { |
| 95 | + kind: Some(Kind::NumberValue(rhs)), |
| 96 | + .. |
| 97 | + }] = values |
| 98 | + else { |
| 99 | + return Err(RuntimeError::default()); |
| 100 | + }; |
| 101 | + |
| 102 | + Ok(Value { |
| 103 | + kind: Some(Kind::NumberValue(lhs % rhs)), |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +fn abs(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 108 | + let [Value { |
| 109 | + kind: Some(Kind::NumberValue(value)), |
| 110 | + .. |
| 111 | + }] = values |
| 112 | + else { |
| 113 | + return Err(RuntimeError::default()); |
| 114 | + }; |
| 115 | + |
| 116 | + Ok(Value { |
| 117 | + kind: Some(Kind::NumberValue(value.abs())), |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +fn is_positive(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 122 | + let [Value { |
| 123 | + kind: Some(Kind::NumberValue(value)), |
| 124 | + .. |
| 125 | + }] = values |
| 126 | + else { |
| 127 | + return Err(RuntimeError::default()); |
| 128 | + }; |
| 129 | + |
| 130 | + Ok(Value { |
| 131 | + kind: Some(Kind::BoolValue(!value.is_sign_negative())), |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +fn is_greater(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 136 | + let [Value { |
| 137 | + kind: Some(Kind::NumberValue(lhs)), |
| 138 | + .. |
| 139 | + }, Value { |
| 140 | + kind: Some(Kind::NumberValue(rhs)), |
| 141 | + .. |
| 142 | + }] = values |
| 143 | + else { |
| 144 | + return Err(RuntimeError::default()); |
| 145 | + }; |
| 146 | + |
| 147 | + Ok(Value { |
| 148 | + kind: Some(Kind::BoolValue(lhs > rhs)), |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | +fn is_less(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 153 | + let [Value { |
| 154 | + kind: Some(Kind::NumberValue(lhs)), |
| 155 | + .. |
| 156 | + }, Value { |
| 157 | + kind: Some(Kind::NumberValue(rhs)), |
| 158 | + .. |
| 159 | + }] = values |
| 160 | + else { |
| 161 | + return Err(RuntimeError::default()); |
| 162 | + }; |
| 163 | + |
| 164 | + Ok(Value { |
| 165 | + kind: Some(Kind::BoolValue(lhs < rhs)), |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +fn is_zero(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 170 | + let [Value { |
| 171 | + kind: Some(Kind::NumberValue(value)), |
| 172 | + .. |
| 173 | + }] = values |
| 174 | + else { |
| 175 | + return Err(RuntimeError::default()); |
| 176 | + }; |
| 177 | + |
| 178 | + Ok(Value { |
| 179 | + kind: Some(Kind::BoolValue(value == &0.0)), |
| 180 | + }) |
| 181 | +} |
| 182 | + |
| 183 | +fn from_text(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 184 | + let [Value { |
| 185 | + kind: Some(Kind::StringValue(string_value)), |
| 186 | + .. |
| 187 | + }] = values |
| 188 | + else { |
| 189 | + return Err(RuntimeError::default()); |
| 190 | + }; |
| 191 | + |
| 192 | + let value: f64 = match string_value.parse() { |
| 193 | + Ok(result) => result, |
| 194 | + Err(_) => return Err(RuntimeError::default()), |
| 195 | + }; |
| 196 | + |
| 197 | + Ok(Value { |
| 198 | + kind: Some(Kind::NumberValue(value)), |
| 199 | + }) |
| 200 | +} |
| 201 | + |
| 202 | +fn as_text(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> { |
| 203 | + let [Value { |
| 204 | + kind: Some(Kind::NumberValue(value)), |
| 205 | + .. |
| 206 | + }] = values |
| 207 | + else { |
| 208 | + return Err(RuntimeError::default()); |
| 209 | + }; |
| 210 | + |
| 211 | + Ok(Value { |
| 212 | + kind: Some(Kind::StringValue(value.to_string())), |
| 213 | + }) |
| 214 | +} |
0 commit comments