Parses a source text literal as a particular type
(value: &str, ty: &AlgebraicType)
| 218 | |
| 219 | /// Parses a source text literal as a particular type |
| 220 | pub(crate) fn parse(value: &str, ty: &AlgebraicType) -> anyhow::Result<AlgebraicValue> { |
| 221 | let to_timestamp = || { |
| 222 | Timestamp::parse_from_rfc3339(value)? |
| 223 | .serialize(ValueSerializer) |
| 224 | .with_context(|| "Could not parse timestamp") |
| 225 | }; |
| 226 | let to_bytes = || { |
| 227 | from_hex_pad::<Vec<u8>, _>(value) |
| 228 | .map(|v| v.into_boxed_slice()) |
| 229 | .map(AlgebraicValue::Bytes) |
| 230 | .with_context(|| "Could not parse hex value") |
| 231 | }; |
| 232 | let to_identity = || { |
| 233 | Identity::from_hex(value) |
| 234 | .map(AlgebraicValue::from) |
| 235 | .with_context(|| "Could not parse identity") |
| 236 | }; |
| 237 | let to_connection_id = || { |
| 238 | ConnectionId::from_hex(value) |
| 239 | .map(AlgebraicValue::from) |
| 240 | .with_context(|| "Could not parse connection id") |
| 241 | }; |
| 242 | let to_uuid = || { |
| 243 | Uuid::parse_str(value) |
| 244 | .map(AlgebraicValue::from) |
| 245 | .map_err(|err| anyhow!(err)) |
| 246 | }; |
| 247 | let to_i256 = |decimal: &BigDecimal| { |
| 248 | i256::from_str_radix( |
| 249 | // Convert to decimal notation |
| 250 | &decimal.to_plain_string(), |
| 251 | 10, |
| 252 | ) |
| 253 | .ok() |
| 254 | }; |
| 255 | let to_u256 = |decimal: &BigDecimal| { |
| 256 | u256::from_str_radix( |
| 257 | // Convert to decimal notation |
| 258 | &decimal.to_plain_string(), |
| 259 | 10, |
| 260 | ) |
| 261 | .ok() |
| 262 | }; |
| 263 | match ty { |
| 264 | AlgebraicType::I8 => parse_int( |
| 265 | // Parse literal as I8 |
| 266 | value, |
| 267 | AlgebraicType::I8, |
| 268 | BigDecimal::to_i8, |
| 269 | AlgebraicValue::I8, |
| 270 | ), |
| 271 | AlgebraicType::U8 => parse_int( |
| 272 | // Parse literal as U8 |
| 273 | value, |
| 274 | AlgebraicType::U8, |
| 275 | BigDecimal::to_u8, |
| 276 | AlgebraicValue::U8, |
| 277 | ), |
searching dependent graphs…