Initial commit
This commit is contained in:
commit
453d954063
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "newt"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "newt"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
97
flake.lock
Normal file
97
flake.lock
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"fenix": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
],
|
||||||
|
"rust-analyzer-src": "rust-analyzer-src"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1715927173,
|
||||||
|
"narHash": "sha256-2S8hVck6nlyiBifzymDvePl5HWgqvVgxkBZCRax1qD8=",
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "fenix",
|
||||||
|
"rev": "a2d19ef9305841f26c8ab908b1c09a84ca307e18",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "fenix",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1710146030,
|
||||||
|
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1715668745,
|
||||||
|
"narHash": "sha256-xp62OkRkbUDNUc6VSqH02jB0FbOS+MsfMb7wL1RJOfA=",
|
||||||
|
"path": "/nix/store/7q4y8idjk2di380j1fxn4k7wx9y935cl-source",
|
||||||
|
"rev": "9ddcaffecdf098822d944d4147dd8da30b4e6843",
|
||||||
|
"type": "path"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"id": "nixpkgs",
|
||||||
|
"type": "indirect"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"fenix": "fenix",
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-analyzer-src": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1715839492,
|
||||||
|
"narHash": "sha256-EyjtjocGLtB7tqyqwBfadP4y5BBtT5EkoG3kq/zym5U=",
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"rev": "83ba42043166948db91fcfcfe30e0b7eac10b3d5",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "rust-lang",
|
||||||
|
"ref": "nightly",
|
||||||
|
"repo": "rust-analyzer",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
26
flake.nix
Normal file
26
flake.nix
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
inputs.nixpkgs.url = "nixpkgs";
|
||||||
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
inputs.fenix = {
|
||||||
|
url = "github:nix-community/fenix";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs, flake-utils, fenix }:
|
||||||
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
|
let
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
fenix' = fenix.packages.${system};
|
||||||
|
nightly = fenix'.default;
|
||||||
|
stable = fenix'.stable;
|
||||||
|
in {
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
packages = [ (fenix'.combine [
|
||||||
|
(stable.withComponents [
|
||||||
|
"cargo" "rustc" "rust-src" "rust-analyzer" "clippy"
|
||||||
|
])
|
||||||
|
nightly.rustfmt
|
||||||
|
]) pkgs.cargo-watch ];
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
5
rustfmt.toml
Normal file
5
rustfmt.toml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
edition = "2021"
|
||||||
|
hard_tabs = true
|
||||||
|
match_block_trailing_comma = true
|
||||||
|
max_width = 80
|
||||||
|
empty_item_single_line = false
|
||||||
5
src/internal/mod.rs
Normal file
5
src/internal/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
//! NOTE: Anything marked pub within this module or its children should not be
|
||||||
|
//! considered stable when it comes to semver. However, they are exposed for
|
||||||
|
//! advanced use cases.
|
||||||
|
|
||||||
|
pub mod parse;
|
||||||
58
src/internal/parse.rs
Normal file
58
src/internal/parse.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
use crate::Result;
|
||||||
|
|
||||||
|
pub struct ValuePath {}
|
||||||
|
pub enum Expr {
|
||||||
|
ValuePath(ValuePath),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct If {
|
||||||
|
expr: Box<Expr>,
|
||||||
|
then: Ast,
|
||||||
|
else_: Option<Ast>,
|
||||||
|
}
|
||||||
|
pub struct For {
|
||||||
|
expr: Box<Expr>,
|
||||||
|
body: Ast,
|
||||||
|
}
|
||||||
|
pub struct Has {
|
||||||
|
expr: Box<Expr>,
|
||||||
|
body: Ast,
|
||||||
|
}
|
||||||
|
pub struct Print {
|
||||||
|
expr: Box<Expr>,
|
||||||
|
}
|
||||||
|
pub enum AstKind {
|
||||||
|
Text(String),
|
||||||
|
Print(Print),
|
||||||
|
Has(Has),
|
||||||
|
If(If),
|
||||||
|
For(For),
|
||||||
|
}
|
||||||
|
pub type Ast = Vec<AstKind>;
|
||||||
|
|
||||||
|
pub fn parse(template: &str) -> Result<Ast> {
|
||||||
|
let mut out = Ast::new();
|
||||||
|
let mut remaining = template;
|
||||||
|
while !remaining.is_empty() {
|
||||||
|
let (ast, r) = parse_one(remaining)?;
|
||||||
|
remaining = r;
|
||||||
|
out.push(ast);
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_one(template: &str) -> Result<(AstKind, &str)> {
|
||||||
|
if template.starts_with("{{") {
|
||||||
|
parse_text(template)
|
||||||
|
} else if let Some(r) = template.strip_prefix('{') {
|
||||||
|
parse_block(r)
|
||||||
|
} else {
|
||||||
|
parse_text(template)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_text(template: &str) -> Result<(AstKind, &str)> {
|
||||||
|
assert!(template.starts_with("{{") || !template.starts_with('{'));
|
||||||
|
|
||||||
|
let template.trim_start_matches(|c| c != '{')
|
||||||
|
}
|
||||||
67
src/lib.rs
Normal file
67
src/lib.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
pub mod internal;
|
||||||
|
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
pub enum Error {}
|
||||||
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
|
pub trait Value {
|
||||||
|
fn print(&self, w: &mut dyn Write) -> core::fmt::Result;
|
||||||
|
fn has(&self) -> Option<&dyn Value>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
Some(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn if_(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn for_(&self, index: usize) -> Option<&dyn Value>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
if index == 0 {
|
||||||
|
Some(self)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> Value for &'a T
|
||||||
|
where
|
||||||
|
T: Value,
|
||||||
|
{
|
||||||
|
fn print(&self, w: &mut dyn Write) -> core::fmt::Result {
|
||||||
|
(&**self).print(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod build {}
|
||||||
|
|
||||||
|
pub struct Newt {}
|
||||||
|
impl Newt {
|
||||||
|
pub fn build(template: &str) -> Result<Self> {
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_and_execute(
|
||||||
|
template: &str,
|
||||||
|
value: &dyn Value,
|
||||||
|
) -> Result<String> {
|
||||||
|
let newt = Newt::build(template)?;
|
||||||
|
|
||||||
|
newt.execute(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
#[test]
|
||||||
|
fn wishlist() {
|
||||||
|
let tmpl = r#"Hello, {.}"#;
|
||||||
|
let out = Newt::build_and_execute(tl, "World!").unwrap();
|
||||||
|
assert_eq!(out, "Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue