diff --git a/dev-scripts/Cargo.toml b/dev-scripts/Cargo.toml index 28a890d..5b85e41 100644 --- a/dev-scripts/Cargo.toml +++ b/dev-scripts/Cargo.toml @@ -14,6 +14,7 @@ networking = { path = "../internal/networking" } serde = "1.0" serde_json = "1.0" bincode = "1.3" -zip = "0.6" +untwine = "0.4" rayon = "1.10" regex = "1.10" +zip = "0.6" diff --git a/dev-scripts/src/main.rs b/dev-scripts/src/main.rs index dbd02ac..13819c1 100644 --- a/dev-scripts/src/main.rs +++ b/dev-scripts/src/main.rs @@ -12,6 +12,7 @@ mod update_docs; mod update_hash; mod default_layout; mod layout; +mod parser; #[derive(Clone, Parser)] diff --git a/dev-scripts/src/parser.rs b/dev-scripts/src/parser.rs new file mode 100644 index 0000000..4b15b3e --- /dev/null +++ b/dev-scripts/src/parser.rs @@ -0,0 +1,56 @@ +use std::{ + collections::HashMap, + num::{ParseFloatError, ParseIntError}, +}; + +use untwine::{parser, parser_repl}; + +#[derive(Debug)] +pub enum JSONValue { + String(String), + Null, + Int(i128), + Float(f64), + Bool(bool), + List(Vec), + Map(HashMap), +} + +impl JSONValue { + pub fn string(self) -> Option { + match self { + JSONValue::String(s) => Some(s), + _ => None, + } + } +} + +#[derive(Debug, thiserror::Error)] +enum ParseJSONError { + #[error("Syntax error: {0}")] + Untwine(#[from] untwine::ParserError), + #[error("Failed to parse number: {0}")] + ParseInt(#[from] ParseIntError), + #[error("Failed to parse number: {0}")] + ParseFloat(#[from] ParseFloatError), +} + +parser! { + [error = ParseJSONError] + sep = #{char::is_ascii_whitespace}*; + comma = sep "," sep; + int: num=<"-"? '0'-'9'+> -> JSONValue { JSONValue::Int(num.parse()?) } + float: num=<"-"? '0'-'9'+ "." '0'-'9'+> -> JSONValue { JSONValue::Float(num.parse()?) } + str_char = ("\\" . | [^"\""]) -> char; + str: "\"" chars=str_char* "\"" -> JSONValue { JSONValue::String(chars.into_iter().collect()) } + null: "null" -> JSONValue { JSONValue::Null } + bool: bool=<"true" | "false"> -> JSONValue { JSONValue::Bool(bool == "true") } + list: "[" sep values=json$comma* sep "]" -> JSONValue { JSONValue::List(values) } + map_entry: key=str sep ":" sep value=json -> (String, JSONValue) { (key.string().unwrap(), value) } + map: "{" sep values=map_entry$comma* sep "}" -> JSONValue { JSONValue::Map(values.into_iter().collect()) } + pub json = (bool | null | str | float | int | list | map) -> JSONValue; +} + +fn main() { + parser_repl(json); +} \ No newline at end of file diff --git a/internal/backend/src/datasource.rs b/internal/backend/src/datasource.rs new file mode 100644 index 0000000..907ef95 --- /dev/null +++ b/internal/backend/src/datasource.rs @@ -0,0 +1,4 @@ +// TODO: use +trait Datasource { + fn get(&self, coordinates: &Coordinates, settings: Settings) -> crate::Result; +}