Rename library to nomo
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
d691fb9198
commit
d2e0405033
28 changed files with 92 additions and 92 deletions
28
Cargo.lock
generated
28
Cargo.lock
generated
|
|
@ -228,6 +228,20 @@ version = "2.8.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "nomo"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"annotate-snippets",
|
||||
"displaydoc",
|
||||
"insta",
|
||||
"nomo",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
|
|
@ -386,20 +400,6 @@ dependencies = [
|
|||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "temple"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"annotate-snippets",
|
||||
"displaydoc",
|
||||
"insta",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"temple",
|
||||
"thiserror",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.18"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "temple"
|
||||
name = "nomo"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
|
|
@ -14,7 +14,7 @@ winnow = { version = "0.7.14", features = ["unstable-recover"] }
|
|||
[dev-dependencies]
|
||||
annotate-snippets = { version = "0.12.13", features = ["testing-colors"] }
|
||||
insta = { version = "1.46.3", features = ["glob"] }
|
||||
temple = { path = ".", features = ["serialize"] }
|
||||
nomo = { path = ".", features = ["serialize"] }
|
||||
|
||||
[profile.dev.package]
|
||||
insta.opt-level = 3
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ast::TemplateAstExpr;
|
||||
use crate::input::TempleInput;
|
||||
use crate::input::NomoInput;
|
||||
|
||||
pub struct EmitMachine {
|
||||
current_index: usize,
|
||||
|
|
@ -25,10 +25,10 @@ pub struct VariableSlot {
|
|||
#[derive(Debug)]
|
||||
pub enum Instruction {
|
||||
AppendContent {
|
||||
content: TempleInput,
|
||||
content: NomoInput,
|
||||
},
|
||||
LoadFromContextToSlot {
|
||||
name: TempleInput,
|
||||
name: NomoInput,
|
||||
slot: VariableSlot,
|
||||
},
|
||||
EmitFromSlot {
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@ use thiserror::Error;
|
|||
|
||||
use crate::Context;
|
||||
use crate::emit::Instruction;
|
||||
use crate::input::TempleInput;
|
||||
use crate::input::NomoInput;
|
||||
|
||||
#[derive(Debug, Error, Display)]
|
||||
pub enum EvaluationError {
|
||||
/// An unknown variable was encountered: .0
|
||||
UnknownVariable(TempleInput),
|
||||
UnknownVariable(NomoInput),
|
||||
/// An explicit abort was requested
|
||||
ExplicitAbort,
|
||||
}
|
||||
|
|
|
|||
50
src/input.rs
50
src/input.rs
|
|
@ -9,14 +9,14 @@ use winnow::stream::Stream;
|
|||
use winnow::stream::StreamIsPartial;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct TempleInput {
|
||||
pub struct NomoInput {
|
||||
backing: Arc<str>,
|
||||
range: Range<usize>,
|
||||
}
|
||||
|
||||
impl TempleInput {
|
||||
pub fn from_parts(backing: Arc<str>, range: Range<usize>) -> TempleInput {
|
||||
TempleInput { backing, range }
|
||||
impl NomoInput {
|
||||
pub fn from_parts(backing: Arc<str>, range: Range<usize>) -> NomoInput {
|
||||
NomoInput { backing, range }
|
||||
}
|
||||
|
||||
pub fn into_parts(self) -> (Arc<str>, Range<usize>) {
|
||||
|
|
@ -24,43 +24,43 @@ impl TempleInput {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for TempleInput {
|
||||
impl std::fmt::Debug for NomoInput {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "\"{}\"", self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for TempleInput {
|
||||
impl From<String> for NomoInput {
|
||||
fn from(value: String) -> Self {
|
||||
let range = 0..value.len();
|
||||
let backing = Arc::from(value);
|
||||
|
||||
TempleInput { backing, range }
|
||||
NomoInput { backing, range }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for TempleInput {
|
||||
impl From<&str> for NomoInput {
|
||||
fn from(value: &str) -> Self {
|
||||
let backing = Arc::from(value.to_string());
|
||||
let range = 0..value.len();
|
||||
|
||||
TempleInput { backing, range }
|
||||
NomoInput { backing, range }
|
||||
}
|
||||
}
|
||||
|
||||
impl FindSlice<&str> for TempleInput {
|
||||
impl FindSlice<&str> for NomoInput {
|
||||
fn find_slice(&self, substr: &str) -> Option<core::ops::Range<usize>> {
|
||||
self.as_str().find_slice(substr)
|
||||
}
|
||||
}
|
||||
|
||||
impl Compare<&str> for TempleInput {
|
||||
impl Compare<&str> for NomoInput {
|
||||
fn compare(&self, t: &str) -> winnow::stream::CompareResult {
|
||||
self.as_str().compare(t)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for TempleInput {
|
||||
impl Deref for NomoInput {
|
||||
type Target = str;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
|
|
@ -68,24 +68,24 @@ impl Deref for TempleInput {
|
|||
}
|
||||
}
|
||||
|
||||
impl TempleInput {
|
||||
impl NomoInput {
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Offset for TempleInput {
|
||||
impl Offset for NomoInput {
|
||||
fn offset_from(&self, start: &Self) -> usize {
|
||||
self.as_str().offset_from(&start.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TempleInputIter {
|
||||
pub struct NomoInputIter {
|
||||
idx: usize,
|
||||
input: TempleInput,
|
||||
input: NomoInput,
|
||||
}
|
||||
|
||||
impl Iterator for TempleInputIter {
|
||||
impl Iterator for NomoInputIter {
|
||||
type Item = (usize, char);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
|
@ -95,7 +95,7 @@ impl Iterator for TempleInputIter {
|
|||
}
|
||||
}
|
||||
|
||||
impl StreamIsPartial for TempleInput {
|
||||
impl StreamIsPartial for NomoInput {
|
||||
type PartialState = ();
|
||||
|
||||
fn complete(&mut self) -> Self::PartialState {
|
||||
|
|
@ -109,17 +109,17 @@ impl StreamIsPartial for TempleInput {
|
|||
}
|
||||
}
|
||||
|
||||
impl Stream for TempleInput {
|
||||
impl Stream for NomoInput {
|
||||
type Token = char;
|
||||
|
||||
type Slice = TempleInput;
|
||||
type Slice = NomoInput;
|
||||
|
||||
type IterOffsets = TempleInputIter;
|
||||
type IterOffsets = NomoInputIter;
|
||||
|
||||
type Checkpoint = TempleInput;
|
||||
type Checkpoint = NomoInput;
|
||||
|
||||
fn iter_offsets(&self) -> Self::IterOffsets {
|
||||
TempleInputIter {
|
||||
NomoInputIter {
|
||||
idx: 0,
|
||||
input: self.clone(),
|
||||
}
|
||||
|
|
@ -187,11 +187,11 @@ impl Stream for TempleInput {
|
|||
mod tests {
|
||||
use winnow::stream::Stream;
|
||||
|
||||
use crate::input::TempleInput;
|
||||
use crate::input::NomoInput;
|
||||
|
||||
#[test]
|
||||
fn check_stream_impl() {
|
||||
let mut stream = TempleInput::from("checking");
|
||||
let mut stream = NomoInput::from("checking");
|
||||
|
||||
let checkpoint = stream.checkpoint();
|
||||
|
||||
|
|
|
|||
28
src/lib.rs
28
src/lib.rs
|
|
@ -6,7 +6,7 @@ use serde::Serialize;
|
|||
use thiserror::Error;
|
||||
|
||||
use crate::emit::Instruction;
|
||||
use crate::input::TempleInput;
|
||||
use crate::input::NomoInput;
|
||||
|
||||
pub mod ast;
|
||||
pub mod emit;
|
||||
|
|
@ -15,7 +15,7 @@ pub mod input;
|
|||
pub mod parser;
|
||||
|
||||
#[derive(Debug, Error, Display)]
|
||||
pub enum TempleError {
|
||||
pub enum NomoError {
|
||||
/// Could not parse the given template
|
||||
ParseError {
|
||||
#[from]
|
||||
|
|
@ -37,19 +37,19 @@ pub enum TempleError {
|
|||
UnknownTemplate(String),
|
||||
}
|
||||
|
||||
pub struct Temple {
|
||||
pub struct Nomo {
|
||||
templates: HashMap<String, Template>,
|
||||
}
|
||||
|
||||
impl Default for Temple {
|
||||
impl Default for Nomo {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Temple {
|
||||
pub fn new() -> Temple {
|
||||
Temple {
|
||||
impl Nomo {
|
||||
pub fn new() -> Nomo {
|
||||
Nomo {
|
||||
templates: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -57,8 +57,8 @@ impl Temple {
|
|||
pub fn add_template(
|
||||
&mut self,
|
||||
name: impl Into<String>,
|
||||
value: impl Into<TempleInput>,
|
||||
) -> Result<(), TempleError> {
|
||||
value: impl Into<NomoInput>,
|
||||
) -> Result<(), NomoError> {
|
||||
let source = value.into();
|
||||
let parse = parser::parse(source.clone())?;
|
||||
let ast = ast::parse(parse.tokens())?;
|
||||
|
|
@ -76,11 +76,11 @@ impl Temple {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn render(&self, name: &str, ctx: &Context) -> Result<String, TempleError> {
|
||||
pub fn render(&self, name: &str, ctx: &Context) -> Result<String, NomoError> {
|
||||
let template = self
|
||||
.templates
|
||||
.get(name)
|
||||
.ok_or_else(|| TempleError::UnknownTemplate(name.to_string()))?;
|
||||
.ok_or_else(|| NomoError::UnknownTemplate(name.to_string()))?;
|
||||
|
||||
let res = eval::execute(&template.instructions, ctx)?;
|
||||
|
||||
|
|
@ -89,7 +89,7 @@ impl Temple {
|
|||
}
|
||||
|
||||
struct Template {
|
||||
source: TempleInput,
|
||||
source: NomoInput,
|
||||
instructions: Vec<Instruction>,
|
||||
}
|
||||
|
||||
|
|
@ -186,11 +186,11 @@ where
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Context;
|
||||
use crate::Temple;
|
||||
use crate::Nomo;
|
||||
|
||||
#[test]
|
||||
fn check_simple_template() {
|
||||
let mut temp = Temple::new();
|
||||
let mut temp = Nomo::new();
|
||||
|
||||
temp.add_template("base", "Hello {{= name }}").unwrap();
|
||||
|
||||
|
|
|
|||
|
|
@ -33,10 +33,10 @@ use winnow::token::take_until;
|
|||
use winnow::token::take_while;
|
||||
|
||||
use crate::SourceSpan;
|
||||
use crate::input::TempleInput;
|
||||
use crate::input::NomoInput;
|
||||
use crate::resume_after_cut;
|
||||
|
||||
type Input<'input> = Recoverable<LocatingSlice<TempleInput>, ParseError>;
|
||||
type Input<'input> = Recoverable<LocatingSlice<NomoInput>, ParseError>;
|
||||
type PResult<'input, T> = Result<T, ParseError>;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
|
@ -52,7 +52,7 @@ impl std::fmt::Display for ParseFailure {
|
|||
}
|
||||
|
||||
impl ParseFailure {
|
||||
fn from_errors(errors: Vec<ParseError>, input: TempleInput) -> ParseFailure {
|
||||
fn from_errors(errors: Vec<ParseError>, input: NomoInput) -> ParseFailure {
|
||||
ParseFailure {
|
||||
input: Arc::from(input.to_string()),
|
||||
errors,
|
||||
|
|
@ -248,13 +248,13 @@ impl<const LEN: usize> winnow::stream::ContainsToken<&'_ TemplateToken> for [Tok
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TemplateToken {
|
||||
kind: TokenKind,
|
||||
source: TempleInput,
|
||||
source: NomoInput,
|
||||
}
|
||||
|
||||
macro_rules! impl_token_kind_builders {
|
||||
($($name:ident => $kind:expr),+ $(,)?) => {
|
||||
$(
|
||||
fn $name(source: TempleInput) -> Self {
|
||||
fn $name(source: NomoInput) -> Self {
|
||||
TemplateToken {
|
||||
kind: $kind,
|
||||
source,
|
||||
|
|
@ -277,7 +277,7 @@ impl TemplateToken {
|
|||
end => TokenKind::End,
|
||||
}
|
||||
|
||||
pub fn literal(literal: TokenLiteral, source: TempleInput) -> Self {
|
||||
pub fn literal(literal: TokenLiteral, source: NomoInput) -> Self {
|
||||
TemplateToken {
|
||||
kind: TokenKind::Literal(literal),
|
||||
source,
|
||||
|
|
@ -288,12 +288,12 @@ impl TemplateToken {
|
|||
self.kind
|
||||
}
|
||||
|
||||
pub fn source(&self) -> TempleInput {
|
||||
pub fn source(&self) -> NomoInput {
|
||||
self.source.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(input: TempleInput) -> Result<ParsedTemplate, ParseFailure> {
|
||||
pub fn parse(input: NomoInput) -> Result<ParsedTemplate, ParseFailure> {
|
||||
let (_remaining, val, errors) =
|
||||
parse_tokens.recoverable_parse(LocatingSlice::new(input.clone()));
|
||||
|
||||
|
|
@ -335,7 +335,7 @@ fn parse_interpolate<'input>(input: &mut Input<'input>) -> PResult<'input, Vec<T
|
|||
.resume_after(recover)
|
||||
.with_taken()
|
||||
.map(|(val, taken)| {
|
||||
val.unwrap_or_else(|| (vec![TemplateToken::invalid(taken)], TempleInput::from("")))
|
||||
val.unwrap_or_else(|| (vec![TemplateToken::invalid(taken)], NomoInput::from("")))
|
||||
})
|
||||
.parse_next(input)?;
|
||||
|
||||
|
|
@ -419,7 +419,7 @@ fn parse_ident<'input>(input: &mut Input<'input>) -> PResult<'input, TemplateTok
|
|||
.parse_next(input)
|
||||
}
|
||||
|
||||
fn ident<'input>(input: &mut Input<'input>) -> PResult<'input, TempleInput> {
|
||||
fn ident<'input>(input: &mut Input<'input>) -> PResult<'input, NomoInput> {
|
||||
peek(not(parse_literal))
|
||||
.context(ParseError::ctx().msg("Expected an ident, but found a literal instead"))
|
||||
.parse_next(input)?;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: parsed
|
||||
input_file: tests/cases/identifiers.temple
|
||||
input_file: tests/cases/identifiers.nomo
|
||||
---
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: parsed
|
||||
input_file: tests/cases/interpolation.temple
|
||||
input_file: tests/cases/interpolation.nomo
|
||||
---
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: parsed
|
||||
input_file: tests/cases/multiple.temple
|
||||
input_file: tests/cases/multiple.nomo
|
||||
---
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: parsed
|
||||
input_file: tests/cases/simple.temple
|
||||
input_file: tests/cases/simple.nomo
|
||||
---
|
||||
ParsedTemplate {
|
||||
tokens: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: ast
|
||||
input_file: tests/cases/identifiers.temple
|
||||
input_file: tests/cases/identifiers.nomo
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: ast
|
||||
input_file: tests/cases/interpolation.temple
|
||||
input_file: tests/cases/interpolation.nomo
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: ast
|
||||
input_file: tests/cases/multiple.temple
|
||||
input_file: tests/cases/multiple.nomo
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: ast
|
||||
input_file: tests/cases/simple.temple
|
||||
input_file: tests/cases/simple.nomo
|
||||
---
|
||||
TemplateAst {
|
||||
root: [
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: emit
|
||||
input_file: tests/cases/identifiers.temple
|
||||
input_file: tests/cases/identifiers.nomo
|
||||
---
|
||||
[
|
||||
LoadFromContextToSlot {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: emit
|
||||
input_file: tests/cases/interpolation.temple
|
||||
input_file: tests/cases/interpolation.nomo
|
||||
---
|
||||
[
|
||||
AppendContent {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: emit
|
||||
input_file: tests/cases/multiple.temple
|
||||
input_file: tests/cases/multiple.nomo
|
||||
---
|
||||
[
|
||||
AppendContent {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: emit
|
||||
input_file: tests/cases/simple.temple
|
||||
input_file: tests/cases/simple.nomo
|
||||
---
|
||||
[
|
||||
AppendContent {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: output
|
||||
input_file: tests/cases/identifiers.temple
|
||||
input_file: tests/cases/identifiers.nomo
|
||||
---
|
||||
"Foo\nFoo\nFoo\nFoo\nFoo\nFoo"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: output
|
||||
input_file: tests/cases/interpolation.temple
|
||||
input_file: tests/cases/interpolation.nomo
|
||||
---
|
||||
"Hello! I'm Hemera"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: output
|
||||
input_file: tests/cases/multiple.temple
|
||||
input_file: tests/cases/multiple.nomo
|
||||
---
|
||||
"Hi there! My name is Hemera Green"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
source: tests/file_tests.rs
|
||||
expression: output
|
||||
input_file: tests/cases/simple.temple
|
||||
input_file: tests/cases/simple.nomo
|
||||
---
|
||||
"Hello World!"
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use temple::Context;
|
||||
use nomo::Context;
|
||||
|
||||
#[test]
|
||||
fn check_cases() {
|
||||
insta::glob!("cases/*.temple", |path| {
|
||||
insta::glob!("cases/*.nomo", |path| {
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.set_snapshot_path("cases");
|
||||
settings.set_snapshot_suffix(path.file_stem().unwrap().display().to_string());
|
||||
|
|
@ -27,19 +27,19 @@ fn check_cases() {
|
|||
context.insert(k, v);
|
||||
}
|
||||
|
||||
let parsed = temple::parser::parse(input.into()).unwrap();
|
||||
let parsed = nomo::parser::parse(input.into()).unwrap();
|
||||
|
||||
insta::assert_debug_snapshot!("1-parsed", parsed);
|
||||
|
||||
let ast = temple::ast::parse(parsed.tokens()).unwrap();
|
||||
let ast = nomo::ast::parse(parsed.tokens()).unwrap();
|
||||
|
||||
insta::assert_debug_snapshot!("2-ast", ast);
|
||||
|
||||
let emit = temple::emit::emit_machine(ast);
|
||||
let emit = nomo::emit::emit_machine(ast);
|
||||
|
||||
insta::assert_debug_snapshot!("3-instructions", emit);
|
||||
|
||||
let output = temple::eval::execute(&emit, &context).unwrap();
|
||||
let output = nomo::eval::execute(&emit, &context).unwrap();
|
||||
|
||||
insta::assert_debug_snapshot!("4-output", output);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue