mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-07-08 01:31:53 +08:00
Fix parse bug
This commit is contained in:
@@ -195,7 +195,13 @@ impl<'a> TextParser<'a> {
|
|||||||
let key = self.parse_key()?;
|
let key = self.parse_key()?;
|
||||||
let v = if self.is_indent("=") {
|
let v = if self.is_indent("=") {
|
||||||
self.parse_indent("=")?;
|
self.parse_indent("=")?;
|
||||||
Value::KeyVal((key, Box::new(self.parse_str()?)))
|
let v = match self.peek().ok_or(self.error2("Unexpected eof"))? {
|
||||||
|
"\"" => self.parse_str()?,
|
||||||
|
"-" | "." | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8"
|
||||||
|
| "9" => self.parse_any_number()?,
|
||||||
|
_ => return self.error("Expected value after key"),
|
||||||
|
};
|
||||||
|
Value::KeyVal((key, Box::new(v)))
|
||||||
} else {
|
} else {
|
||||||
Value::Str(key)
|
Value::Str(key)
|
||||||
};
|
};
|
||||||
@@ -353,3 +359,47 @@ impl<'a> TextParser<'a> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gen() {
|
||||||
|
let v = Value::Array(vec![
|
||||||
|
Value::Str("\"Hello<Dat>".to_string()),
|
||||||
|
Value::Array(vec![
|
||||||
|
Value::Str("title".to_string()),
|
||||||
|
Value::Int(1),
|
||||||
|
Value::Float(2.0),
|
||||||
|
Value::KeyVal((
|
||||||
|
"name".to_string(),
|
||||||
|
Box::new(Value::Str("World".to_string())),
|
||||||
|
)),
|
||||||
|
Value::KeyVal(("int".to_string(), Box::new(Value::Int(42)))),
|
||||||
|
Value::KeyVal(("float".to_string(), Box::new(Value::Float(3.0)))),
|
||||||
|
]),
|
||||||
|
Value::Str(">World".to_string()),
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
TextGenerator::new().generate(&v).unwrap(),
|
||||||
|
"\"Hello<Dat><title 1 2.0 name=\"World\" int=42 float=3.0>>World"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse() {
|
||||||
|
let text = "\"Hello<Dat><title 1 2.0 name=\"World\" int=42 float=3.0>>World";
|
||||||
|
let v = Value::Array(vec![
|
||||||
|
Value::Str("\"Hello<Dat>".to_string()),
|
||||||
|
Value::Array(vec![
|
||||||
|
Value::Str("title".to_string()),
|
||||||
|
Value::Int(1),
|
||||||
|
Value::Float(2.0),
|
||||||
|
Value::KeyVal((
|
||||||
|
"name".to_string(),
|
||||||
|
Box::new(Value::Str("World".to_string())),
|
||||||
|
)),
|
||||||
|
Value::KeyVal(("int".to_string(), Box::new(Value::Int(42)))),
|
||||||
|
Value::KeyVal(("float".to_string(), Box::new(Value::Float(3.0)))),
|
||||||
|
]),
|
||||||
|
Value::Str(">World".to_string()),
|
||||||
|
]);
|
||||||
|
assert_eq!(TextParser::new(text).parse().unwrap(), v);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::cmp::{PartialEq, PartialOrd};
|
|||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std::ops::{Deref, Index, IndexMut};
|
use std::ops::{Deref, Index, IndexMut};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Int(i64),
|
Int(i64),
|
||||||
|
|||||||
Reference in New Issue
Block a user