mirror of
https://codeberg.org/icewind/vdf-reader.git
synced 2026-06-03 18:14:07 +02:00
make bare sequences worth with Entry
This commit is contained in:
parent
fe7bc149d6
commit
d29d06166c
10 changed files with 145 additions and 24 deletions
|
|
@ -10,6 +10,7 @@ pub use statement::Statement;
|
|||
use std::any::type_name;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Formatter;
|
||||
use std::mem::swap;
|
||||
use std::slice;
|
||||
pub use table::Table;
|
||||
pub use value::Value;
|
||||
|
|
@ -116,9 +117,7 @@ impl Entry {
|
|||
pub fn as_str(&self) -> Option<&str> {
|
||||
match self {
|
||||
Entry::Value(value) => Some(value),
|
||||
|
||||
Entry::Statement(value) => Some(value),
|
||||
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
@ -136,6 +135,32 @@ impl Entry {
|
|||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn into_array(self) -> Result<Self, ParseEntryError> {
|
||||
match self {
|
||||
Entry::Array(array) => Ok(Entry::Array(array)),
|
||||
Entry::Value(value) => {
|
||||
let mut array = Array::default();
|
||||
array.push(value.into());
|
||||
Ok(Entry::Array(array))
|
||||
}
|
||||
entry => Err(ParseEntryError::new("array", entry)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to take the entry as a slice.
|
||||
pub fn push(&mut self, value: Entry) -> Result<(), ParseEntryError> {
|
||||
let mut tmp = Entry::Value(Value::default());
|
||||
|
||||
swap(self, &mut tmp);
|
||||
*self = tmp.into_array()?;
|
||||
if let Entry::Array(array) = self {
|
||||
array.push(value);
|
||||
} else {
|
||||
panic!("into_array ensured this is an array")
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parsable types.
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use super::{Array, Entry};
|
||||
use crate::entry::{string_is_array, Statement, Value};
|
||||
use crate::error::UnknownError;
|
||||
use crate::event::{EntryEvent, GroupStartEvent};
|
||||
use crate::event::{EntryEvent, GroupStartEvent, ValueContinuationEvent};
|
||||
use crate::{Event, Item, Reader, Result, VdfError};
|
||||
use serde::de::{DeserializeSeed, MapAccess};
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
|
|
@ -63,15 +63,17 @@ impl Table {
|
|||
/// Load a table from the given `Reader`.
|
||||
pub fn load(reader: &mut Reader) -> Result<Table> {
|
||||
let mut map = HashMap::new();
|
||||
let mut last_key = None;
|
||||
|
||||
while let Some(event) = reader.event() {
|
||||
match event? {
|
||||
last_key = match event? {
|
||||
Event::Entry(EntryEvent {
|
||||
key: Item::Item { content: key, .. },
|
||||
value,
|
||||
..
|
||||
}) => {
|
||||
let str = value.as_str();
|
||||
let key_clone = key.clone();
|
||||
if string_is_array(str) {
|
||||
insert(
|
||||
&mut map,
|
||||
|
|
@ -81,16 +83,31 @@ impl Table {
|
|||
} else {
|
||||
insert(&mut map, key, Value::from(value.into_content()))
|
||||
}
|
||||
Some(key_clone)
|
||||
}
|
||||
|
||||
Event::Entry(EntryEvent {
|
||||
key: Item::Statement { content: key, .. },
|
||||
value,
|
||||
..
|
||||
}) => insert(&mut map, key, Statement::from(value.into_content())),
|
||||
}) => {
|
||||
let key_clone = key.clone();
|
||||
insert(&mut map, key, Statement::from(value.into_content()));
|
||||
Some(key_clone)
|
||||
}
|
||||
|
||||
Event::ValueContinuation(ValueContinuationEvent { value, .. }) => {
|
||||
if let Some(key) = last_key.as_ref() {
|
||||
if let Some(last_value) = map.get_mut(key.as_ref()) {
|
||||
last_value.push(Value::from(value.into_content()).into())?;
|
||||
}
|
||||
}
|
||||
last_key
|
||||
}
|
||||
|
||||
Event::GroupStart(GroupStartEvent { name, .. }) => {
|
||||
insert(&mut map, name, Table::load(reader)?)
|
||||
insert(&mut map, name, Table::load(reader)?);
|
||||
None
|
||||
}
|
||||
|
||||
Event::GroupEnd(_) => break,
|
||||
|
|
|
|||
|
|
@ -82,6 +82,13 @@ impl<'de> Deserialize<'de> for Value {
|
|||
write!(formatter, "any string like value")
|
||||
}
|
||||
|
||||
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(if v { "1".into() } else { "0".into() })
|
||||
}
|
||||
|
||||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
|
|
@ -116,13 +123,6 @@ impl<'de> Deserialize<'de> for Value {
|
|||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
Ok(if v { "1".into() } else { "0".into() })
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(ValueVisitor).map(Value)
|
||||
|
|
|
|||
10
src/error.rs
10
src/error.rs
|
|
@ -21,7 +21,7 @@ pub enum VdfError {
|
|||
#[error(transparent)]
|
||||
#[diagnostic(transparent)]
|
||||
/// Wrong event to for conversion
|
||||
WrongEntryType(Box<WrongEventTypeError>),
|
||||
WrongEventType(Box<WrongEventTypeError>),
|
||||
#[error(transparent)]
|
||||
#[diagnostic(transparent)]
|
||||
/// Failed to parse entry into type
|
||||
|
|
@ -49,7 +49,7 @@ pub enum VdfError {
|
|||
|
||||
impl From<WrongEventTypeError> for VdfError {
|
||||
fn from(value: WrongEventTypeError) -> Self {
|
||||
Self::WrongEntryType(value.into())
|
||||
Self::WrongEventType(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ impl VdfError {
|
|||
VdfError::Other(e) => e.src.as_str(),
|
||||
VdfError::UnexpectedToken(e) => e.src.as_str(),
|
||||
VdfError::NoValidToken(e) => e.src.as_str(),
|
||||
VdfError::WrongEntryType(e) => e.src.as_str(),
|
||||
VdfError::WrongEventType(e) => e.src.as_str(),
|
||||
VdfError::SerdeParse(e) => e.src.as_str(),
|
||||
VdfError::UnknownVariant(e) => e.src.as_str(),
|
||||
_ => {
|
||||
|
|
@ -73,7 +73,7 @@ impl VdfError {
|
|||
VdfError::Other(e) => e.err_span,
|
||||
VdfError::UnexpectedToken(e) => e.err_span,
|
||||
VdfError::NoValidToken(e) => e.err_span,
|
||||
VdfError::WrongEntryType(e) => e.err_span,
|
||||
VdfError::WrongEventType(e) => e.err_span,
|
||||
VdfError::SerdeParse(e) => e.err_span,
|
||||
VdfError::UnknownVariant(e) => e.err_span,
|
||||
_ => {
|
||||
|
|
@ -118,7 +118,7 @@ impl VdfError {
|
|||
..e
|
||||
}
|
||||
.into(),
|
||||
VdfError::WrongEntryType(e) => WrongEventTypeError {
|
||||
VdfError::WrongEventType(e) => WrongEventTypeError {
|
||||
src: source.into(),
|
||||
err_span: span.into(),
|
||||
..*e
|
||||
|
|
|
|||
46
src/event.rs
46
src/event.rs
|
|
@ -60,6 +60,17 @@ pub enum Event<'a> {
|
|||
|
||||
/// An entry.
|
||||
Entry(EntryEvent<'a>),
|
||||
|
||||
/// An additional value for the previous entry.
|
||||
ValueContinuation(ValueContinuationEvent<'a>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum EventType {
|
||||
GroupStart,
|
||||
GroupEnd,
|
||||
Entry,
|
||||
ValueContinuation,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
|
|
@ -87,6 +98,9 @@ impl<'a> TryFrom<Event<'a>> for GroupStartEvent<'a> {
|
|||
Err(WrongEventTypeError::new(event, "group start", "group end").into())
|
||||
}
|
||||
Event::Entry(_) => Err(WrongEventTypeError::new(event, "group start", "entry").into()),
|
||||
Event::ValueContinuation(_) => {
|
||||
Err(WrongEventTypeError::new(event, "group start", "value continuation").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -106,6 +120,9 @@ impl<'a> TryFrom<Event<'a>> for GroupEndEvent {
|
|||
Err(WrongEventTypeError::new(event, "group end", "group start").into())
|
||||
}
|
||||
Event::Entry(_) => Err(WrongEventTypeError::new(event, "group start", "entry").into()),
|
||||
Event::ValueContinuation(_) => {
|
||||
Err(WrongEventTypeError::new(event, "group start", "value continuation").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -137,6 +154,24 @@ impl<'a> TryFrom<Event<'a>> for EntryEvent<'a> {
|
|||
Event::GroupStart(_) => {
|
||||
Err(WrongEventTypeError::new(event, "entry", "group start").into())
|
||||
}
|
||||
Event::ValueContinuation(_) => {
|
||||
Err(WrongEventTypeError::new(event, "entry", "value continuation").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct ValueContinuationEvent<'a> {
|
||||
pub value: Item<'a>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl ValueContinuationEvent<'_> {
|
||||
pub fn into_owned(self) -> ValueContinuationEvent<'static> {
|
||||
ValueContinuationEvent {
|
||||
value: self.value.into_owned(),
|
||||
span: self.span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -148,6 +183,7 @@ impl Event<'_> {
|
|||
Event::GroupStart(GroupStartEvent { span, .. }) => span.clone(),
|
||||
Event::GroupEnd(GroupEndEvent { span, .. }) => span.clone(),
|
||||
Event::Entry(EntryEvent { span, .. }) => span.clone(),
|
||||
Event::ValueContinuation(ValueContinuationEvent { span, .. }) => span.clone(),
|
||||
}
|
||||
}
|
||||
pub fn into_owned(self) -> Event<'static> {
|
||||
|
|
@ -155,6 +191,16 @@ impl Event<'_> {
|
|||
Event::GroupStart(event) => Event::GroupStart(event.into_owned()),
|
||||
Event::GroupEnd(event) => Event::GroupEnd(event),
|
||||
Event::Entry(event) => Event::Entry(event.into_owned()),
|
||||
Event::ValueContinuation(event) => Event::ValueContinuation(event.into_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ty(&self) -> EventType {
|
||||
match self {
|
||||
Event::GroupStart(GroupStartEvent { .. }) => EventType::GroupStart,
|
||||
Event::GroupEnd(GroupEndEvent { .. }) => EventType::GroupEnd,
|
||||
Event::Entry(EntryEvent { .. }) => EventType::Entry,
|
||||
Event::ValueContinuation(ValueContinuationEvent { .. }) => EventType::ValueContinuation,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,15 @@
|
|||
use super::{Result, Token};
|
||||
use crate::error::{NoValidTokenError, UnexpectedTokenError};
|
||||
use crate::event::{EntryEvent, Event, GroupEndEvent, GroupStartEvent, Item};
|
||||
use crate::event::{
|
||||
EntryEvent, Event, EventType, GroupEndEvent, GroupStartEvent, Item, ValueContinuationEvent,
|
||||
};
|
||||
use logos::{Lexer, Logos, Span, SpannedIter};
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// A VDF token reader.
|
||||
pub struct Reader<'a> {
|
||||
pub source: &'a str,
|
||||
pub last_event: Option<EventType>,
|
||||
lexer: SpannedIter<'a, Token>,
|
||||
}
|
||||
|
||||
|
|
@ -14,6 +17,7 @@ impl<'a> From<&'a str> for Reader<'a> {
|
|||
fn from(content: &'a str) -> Self {
|
||||
Reader {
|
||||
source: content,
|
||||
last_event: None,
|
||||
lexer: Lexer::new(content).spanned(),
|
||||
}
|
||||
}
|
||||
|
|
@ -29,8 +33,16 @@ impl<'a> Reader<'a> {
|
|||
}
|
||||
|
||||
/// Get the next event, this does copies.
|
||||
#[allow(dead_code)]
|
||||
pub fn event(&mut self) -> Option<Result<Event<'a>>> {
|
||||
let result = self.event_inner();
|
||||
if let Some(Ok(event)) = &result {
|
||||
self.last_event = Some(event.ty());
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn event_inner(&mut self) -> Option<Result<Event<'a>>> {
|
||||
const VALID_KEY: &[Token] = &[
|
||||
Token::Item,
|
||||
Token::QuotedItem,
|
||||
|
|
@ -39,6 +51,8 @@ impl<'a> Reader<'a> {
|
|||
Token::QuotedStatement,
|
||||
];
|
||||
|
||||
let whitespace_start = self.span().end;
|
||||
|
||||
let key = match self.token() {
|
||||
None => {
|
||||
return None;
|
||||
|
|
@ -86,6 +100,21 @@ impl<'a> Reader<'a> {
|
|||
}
|
||||
};
|
||||
|
||||
let whitespace_end = self.span().start;
|
||||
let skipped_newline = self.source[whitespace_start..whitespace_end].contains('\n');
|
||||
let last_event_has_value = matches!(
|
||||
self.last_event,
|
||||
Some(EventType::Entry | EventType::ValueContinuation)
|
||||
);
|
||||
|
||||
// multiple values on the same line create an array
|
||||
if last_event_has_value && !skipped_newline {
|
||||
return Some(Ok(Event::ValueContinuation(ValueContinuationEvent {
|
||||
value: key,
|
||||
span: self.span(),
|
||||
})));
|
||||
}
|
||||
|
||||
const VALID_VALUE: &[Token] = &[
|
||||
Token::Item,
|
||||
Token::QuotedItem,
|
||||
|
|
|
|||
|
|
@ -250,6 +250,7 @@ fn test_serde_table(path: &str) {
|
|||
fn test_serde_from_table(path: &str) {
|
||||
let raw = read_to_string(path).unwrap();
|
||||
let result = Table::load_from_str(&raw).unwrap();
|
||||
dbg!(&result);
|
||||
|
||||
let material: Expected = from_entry(result.into()).expect("table to material");
|
||||
insta::assert_ron_snapshot!(format!("table_to_material__{}", path), material);
|
||||
|
|
|
|||
|
|
@ -4,8 +4,11 @@ expression: parsed
|
|||
---
|
||||
{
|
||||
"Resource/specificPanel.res": {
|
||||
"$envmaptint": ".5",
|
||||
".5": ".5",
|
||||
"$envmaptint": [
|
||||
".5",
|
||||
".5",
|
||||
".5",
|
||||
],
|
||||
"\\\\\"$translucent\"": "1",
|
||||
"array": [
|
||||
"1",
|
||||
|
|
|
|||
|
|
@ -11,6 +11,5 @@ r#Resource/specificPanel.res(
|
|||
],
|
||||
windows_path: "C:\\test\\no newline",
|
||||
r#\\"$translucent": true,
|
||||
r#$envmaptint: 0.5,
|
||||
r#.5: 0.5,
|
||||
r#$envmaptint: (0.5, 0.5, 0.5),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -12,4 +12,5 @@ Types(
|
|||
single: 1.2,
|
||||
triple: (1.2, 1.3, 1.4),
|
||||
single_int: 2.0,
|
||||
another_tuple: (8, "foo", false),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue