MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / parse_int

Function parse_int

crates/expr/src/lib.rs:177–197  ·  view source on GitHub ↗

Parse an integer literal into an [AlgebraicValue]

(
    literal: &str,
    ty: AlgebraicType,
    to_int: ToInt,
    to_val: ToVal,
)

Source from the content-addressed store, hash-verified

175
176/// Parse an integer literal into an [AlgebraicValue]
177fn parse_int<Int, Val, ToInt, ToVal>(
178 literal: &str,
179 ty: AlgebraicType,
180 to_int: ToInt,
181 to_val: ToVal,
182) -> anyhow::Result<AlgebraicValue>
183where
184 Int: Into<Val>,
185 ToInt: FnOnce(&BigDecimal) -> Option<Int>,
186 ToVal: FnOnce(Val) -> AlgebraicValue,
187{
188 // Why are we using an arbitrary precision type?
189 // For scientific notation as well as i256 and u256.
190 BigDecimal::from_str(literal)
191 .ok()
192 .filter(|decimal| decimal.is_integer())
193 .ok_or_else(|| anyhow!("{literal} is not an integer"))
194 .map(|decimal| to_int(&decimal).map(|val| val.into()).map(to_val))
195 .transpose()
196 .ok_or_else(|| anyhow!("{literal} is out of bounds for type {}", fmt_algebraic_type(&ty)))?
197}
198
199/// Parse a floating point literal into an [AlgebraicValue]
200fn parse_float<Float, Value, ToFloat, ToValue>(

Callers 2

type_limitFunction · 0.85
parseFunction · 0.85

Calls 5

from_strFunction · 0.85
is_integerMethod · 0.80
filterMethod · 0.65
mapMethod · 0.45
okMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…