21 个版本
0.6.1 | 2024年7月20日 |
---|---|
0.6.0 | 2024年3月11日 |
0.5.1 | 2023年2月23日 |
0.5.0 | 2022年10月6日 |
0.2.0 | 2020年12月13日 |
#1040 在 解析器实现
每月250 次下载
用于 nextcloud-config-parser
80KB
2.5K SLoC
php-literal-parser
php 字面量解析器。
用法
解析为通用表示
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(())
}
或使用 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(())
}
lib.rs
:
php 字面量解析器。
允许解析 php 字符串、布尔值、数字和数组字面量。
用法
解析为通用表示
use php_literal_parser::{from_str, Value};
let map = from_str::<Value>(r#"["foo" => true, "nested" => ['foo' => false]]"#)?;
assert_eq!(map["foo"], true);
assert_eq!(map["nested"]["foo"], false);
或使用 serde 解析为特定结构
use php_literal_parser::from_str;
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct Target {
foo: bool,
bars: Vec<u8>
}
let target = from_str(r#"["foo" => true, "bars" => [1, 2, 3, 4,]]"#)?;
assert_eq!(Target {
foo: true,
bars: vec![1,2,3,4]
}, target);
依赖项
~5–6.5MB
~87K SLoC