php-literal-parser

Crates.iophp-literal-parser
lib.rsphp-literal-parser
version0.6.2
sourcesrc
created_at2020-12-01 23:42:01.22685
updated_at2024-11-05 15:10:45.124257
descriptionparser for php literals
homepage
repositoryhttps://github.com/icewind1991/php-literal-parser
max_upload_size
id318722
size120,927
Robin Appelman (icewind1991)

documentation

https://docs.rs/php-literal-parser

README

php-literal-parser

parser for php literals.

Usage

Parse into a generic representation

use php_literal_parser::{from_str, Value, ParseError};

fn main() -> Result<(), ParseError> {
    let map = from_str::<Value>(r#"["foo" => true, "nested" => ['foo' => false]]"#)?;

    assert_eq!(map["foo"], true);
    assert_eq!(map["nested"]["foo"], false);

    Ok(())
}

Or parse into a specific struct using serde

use php_literal_parser::{from_str, ParseError};
use serde::Deserialize;

#[derive(Debug, Deserialize, PartialEq)]
struct Target {
    foo: bool,
    bars: Vec<u8>
}

fn main() -> Result<(), ParseError> {
    let target = from_str(r#"["foo" => true, "bars" => [1, 2, 3, 4,]]"#)?;

    assert_eq!(Target {
        foo: true,
        bars: vec![1, 2, 3, 4]
    }, target);
    Ok(())
}
Commit count: 90

cargo fmt