Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add optional caching #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions markup-proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ proc-macro = true
proc-macro2 = "1.0.3"
quote = "1.0.2"
syn = { version = "1.0.5", features = ["extra-traits", "full"] }
sha2 = { version = "0.8", optional = true }
dirs = { version = "2.0.2", optional = true }
version = { version = "3.0.0", optional = true }

[features]
caching = ["sha2", "dirs", "version"]
35 changes: 35 additions & 0 deletions markup-proc-macro/src/caching.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use proc_macro::TokenStream;

#[cfg(not(feature = "caching"))]
pub fn cached(tokens: TokenStream, f: impl FnOnce(TokenStream) -> TokenStream) -> TokenStream {
f(tokens)
}

#[cfg(feature = "caching")]
pub fn cached(tokens: TokenStream, f: impl FnOnce(TokenStream) -> TokenStream) -> TokenStream {
use sha2::{Digest, Sha256};
use std::fs;

let dir = dirs::cache_dir().unwrap().join("markup-rs");
let _ = fs::create_dir(&dir);
let dir = dir.join(version::version!().to_string());
let _ = fs::create_dir(&dir);

let input = tokens.to_string();
let mut hasher = Sha256::new();
hasher.input(input);
let hash = hasher.result();
let path = dir.join(format!("{:x}", hash));

if let Ok(cached) = fs::read_to_string(&path) {
if let Ok(cached) = cached.parse() {
return cached;
}
}

let output = f(tokens);
if fs::write(&path, output.to_string()).is_err() {
eprintln!("warning: cannot write to cache dir");
}
output
}
11 changes: 8 additions & 3 deletions markup-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
extern crate proc_macro;

mod ast;
mod caching;
mod generate;
mod parse;

use proc_macro::TokenStream;

#[proc_macro]
pub fn define(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
let structs = syn::parse_macro_input!(tokens as parse::Many<ast::Struct>).0;
quote::quote!( #(#structs)* ).into()
pub fn define(tokens: TokenStream) -> TokenStream {
caching::cached(tokens, |tokens| {
let structs = syn::parse_macro_input!(tokens as parse::Many<ast::Struct>).0;
quote::quote!( #(#structs)* ).into()
})
}
3 changes: 3 additions & 0 deletions markup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ categories = ["template-engine"]

[dependencies]
markup-proc-macro = { path = "../markup-proc-macro", version = "0.4.0" }

[features]
caching = ["markup-proc-macro/caching"]