[core] API: compiling Seam 1 skeleton (types, signatures, stubs)
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! A strict tokenizer, parser and writer for Valve's KeyValues text format (VDF), as used by Steam's
|
||||
//! `libraryfolders.vdf` and `appmanifest_<id>.acf`.
|
||||
//!
|
||||
//! Accepted: quoted strings with the escapes `\\`, `\"`, `\n`, `\t` and `\r`; `{` and `}`; whitespace (space, tab,
|
||||
//! CR, LF); `//` comments to the end of the line; one UTF-8 byte-order mark at the start. Refused: unquoted
|
||||
//! tokens, `#include` / `#base`, `[$CONDITION]` tags, any other escape, raw control characters or line breaks inside
|
||||
//! a string, NUL anywhere, empty keys, a key repeated on the same level (ignoring ASCII case), and anything over the
|
||||
//! caps in [`crate::limits`].
|
||||
|
||||
use crate::error::ParseError;
|
||||
|
||||
/// One token and the byte offset where it starts.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub at: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TokenKind {
|
||||
/// `{`
|
||||
Open,
|
||||
/// `}`
|
||||
Close,
|
||||
/// A quoted string, unescaped.
|
||||
Str(String),
|
||||
}
|
||||
|
||||
/// A value: a string or a nested object.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Value {
|
||||
Str(String),
|
||||
Obj(Obj),
|
||||
}
|
||||
|
||||
/// An ordered list of key/value pairs with unique keys (ignoring ASCII case).
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct Obj {
|
||||
entries: Vec<(String, Value)>,
|
||||
}
|
||||
|
||||
impl Obj {
|
||||
pub fn new() -> Obj {
|
||||
Obj::default()
|
||||
}
|
||||
|
||||
/// Appends a pair. Refuses an empty key, a key or value over its cap, and a key already present.
|
||||
pub fn insert(&mut self, key: String, value: Value) -> Result<(), ParseError> {
|
||||
let _ = (key, value);
|
||||
Err(ParseError::Missing("key"))
|
||||
}
|
||||
|
||||
/// The value under `key`, ignoring ASCII case.
|
||||
pub fn get(&self, key: &str) -> Option<&Value> {
|
||||
self.entries.iter().find(|(k, _)| k.eq_ignore_ascii_case(key)).map(|(_, v)| v)
|
||||
}
|
||||
|
||||
/// The pairs in file order.
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
|
||||
self.entries.iter().map(|(k, v)| (k.as_str(), v))
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.entries.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.entries.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits `input` into tokens. Checks size, UTF-8, escapes and string lengths; not nesting.
|
||||
pub fn tokenize(input: &[u8]) -> Result<Vec<Token>, ParseError> {
|
||||
let _ = input;
|
||||
Err(ParseError::Syntax { at: 0 })
|
||||
}
|
||||
|
||||
/// Parses a whole document: a sequence of top-level pairs.
|
||||
pub fn parse(input: &[u8]) -> Result<Obj, ParseError> {
|
||||
let _ = input;
|
||||
Err(ParseError::Syntax { at: 0 })
|
||||
}
|
||||
|
||||
/// Writes a document in Valve's layout (tabs, one pair per line). [`parse`] of the result gives `doc` back.
|
||||
pub fn write(doc: &Obj) -> String {
|
||||
let _ = doc;
|
||||
String::new()
|
||||
}
|
||||
Reference in New Issue
Block a user