Skip to main content

parse_value

Function parse_value 

Source
pub fn parse_value(input: &str) -> IResult<&str, Value>
Expand description

Parse string and numeric literals for magic rule values

Supports:

  • Quoted strings with escape sequences: “Hello\nWorld”, “ELF\0”
  • Numeric literals (decimal): 123, -456
  • Numeric literals (hexadecimal): 0x1a2b, -0xFF
  • Hex byte sequences: \x7f\x45\x4c\x46 or 7f454c46

§Examples

use libmagic_rs::parser::grammar::parse_value;
use libmagic_rs::parser::ast::Value;

// String values
assert_eq!(parse_value("\"Hello\""), Ok(("", Value::String("Hello".to_string()))));
assert_eq!(parse_value("\"Line1\\nLine2\""), Ok(("", Value::String("Line1\nLine2".to_string()))));

// Numeric values
assert_eq!(parse_value("123"), Ok(("", Value::Uint(123))));
assert_eq!(parse_value("-456"), Ok(("", Value::Int(-456))));
assert_eq!(parse_value("0x1a"), Ok(("", Value::Uint(26))));
assert_eq!(parse_value("-0xFF"), Ok(("", Value::Int(-255))));

// Hex byte sequences
assert_eq!(parse_value("\\x7f\\x45"), Ok(("", Value::Bytes(vec![0x7f, 0x45]))));

§Errors

Returns a nom parsing error if:

  • Input is empty or contains no valid value
  • Quoted string is not properly terminated
  • Numeric value cannot be parsed as a valid integer
  • Hex byte sequence contains invalid hex digits
  • Input contains invalid characters for the detected value format