Skip to main content

literal_expr

Function literal_expr 

Source
pub fn literal_expr(lit: &Literal) -> Result<Expr>
Expand description

Converts one syntax literal into retained expression syntax without executing code.

Preserves the token’s source span and concrete numeric width using the same validation as expression parsing. Floating-point overflow produces infinity.

§Errors

Rejects unsupported literal kinds, invalid suffixes, misplaced exponent signs, and numeric values that cannot be parsed as their types.

  • Complexity: O(n) literal bytes.

§Examples

let token: syn::Lit = syn::parse_str("42i32")?;
assert!(matches!(cel_parser::literal_expr(&token)?,
    cel_parser::Expr::Literal { value: cel_parser::Literal::I32(42), .. }));
let overflow: syn::Lit = syn::parse_str("340282366920938463463374607431768211456f32")?;
cel_parser::validate_literal(&overflow)?;
assert!(matches!(cel_parser::literal_expr(&overflow)?,
    cel_parser::Expr::Literal { value: cel_parser::Literal::F32(value), .. }
    if value.is_infinite() && value.is_sign_positive()));