Skip to content

synek317/finja

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Finja

Warning: this is just an experiment in a very early stage of development.

Jinja templating meets Rust code

Basic Example

use finja::finja;

#[finja(
    welcomer => "println!",
    greeting => "Hello, {}",
    name => "guest2"
)]
fn main() {
    let guest1 = "Universe";
    let guest2 = "World";

    {{ welcomer }}("{{ greeting }}", {{ name }});
}

Which renders to:

fn main()
{
    let guest1 = "Universe";
    let guest2 = "World";
    println!("Hello, {}", guest2);
}

Other Example

Move error-related code outside of the function body to make it less noisy.

use finja::finja;

#[finja(
    OpenError => "|e| MyError::OpenError(path.to_path_buf(), e)",
    ReadError => "|e| MyError::ReadError(path.to_path_buf(), e)",
    FooError => "Err(MyError::ContentError(path.to_path_buf(), 42.to_string()))",
)]
fn process(path: impl AsRef<Path>) -> Result<(), MyError> {
    let path = path.as_ref();
    let mut file = File::open(path).map_err({{ OpenError }})?;
    let mut contents = Vec::new();
    file.read_to_end(&mut contents).map_err({{ ReadError }})?;

    if !contents.windows(2).any(|w| w == b"42") {
        return {{ FooError }};
    }

    Ok(())
}

Usage:

Add this to your Cargo.toml

[dependencies]
finja = { git = "https://github.com/synek317/finja" }

And you are ready to use it. Currently the examples above are the only documentation available.

About

Jinja templates for Rust functions

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages