Add initial parsing

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-05 08:22:10 +01:00
parent 31e102a4ee
commit f4e8137e17
6 changed files with 1098 additions and 6 deletions

View file

@ -1,14 +1,99 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
use std::collections::BTreeMap;
use std::collections::HashMap;
use displaydoc::Display;
use serde::Serialize;
use thiserror::Error;
pub mod parser;
#[derive(Debug, Error, Display)]
pub enum TempleError {
/// Could not parse the given template
ParseError {},
}
pub struct Temple {
templates: HashMap<String, Template>,
}
impl Default for Temple {
fn default() -> Self {
Self::new()
}
}
impl Temple {
pub fn new() -> Temple {
Temple {
templates: HashMap::new(),
}
}
pub fn add_template(
&mut self,
name: impl Into<String>,
value: impl AsRef<str>,
) -> Result<(), TempleError> {
Ok(())
}
fn render(&self, arg: &str, ctx: &Context) -> Result<String, TempleError> {
Ok(String::new())
}
}
struct Template {}
pub struct Context {
values: BTreeMap<String, serde_json::Value>,
}
impl Default for Context {
fn default() -> Self {
Context::new()
}
}
impl Context {
pub fn new() -> Context {
Context {
values: BTreeMap::new(),
}
}
pub fn try_insert(
&mut self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<(), serde_json::Error> {
self.values.insert(key.into(), serde_json::to_value(value)?);
Ok(())
}
pub fn insert(&mut self, key: impl Into<String>, value: impl Serialize) {
self.try_insert(key, value)
.expect("inserted value should serialize without error");
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Context;
use crate::Temple;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
fn check_simple_template() {
let mut temp = Temple::new();
temp.add_template("base", "Hello {{= name }}").unwrap();
let mut ctx = Context::new();
ctx.insert("name", "World");
let rendered = temp.render("base", &ctx).unwrap();
insta::assert_snapshot!(rendered, @"")
}
}