Compare commits

...

2 Commits
3890 ... 2182

Author SHA1 Message Date
Greg Johnston
f1632efa09 add docs 2024-08-19 22:10:23 -04:00
Greg Johnston
d475b213d7 feat: allow including a view from an external file (closes #2182) 2024-08-19 09:36:15 -04:00

View File

@@ -13,6 +13,7 @@ use component::DummyModel;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenTree};
use quote::{quote, ToTokens};
use std::str::FromStr;
use syn::{parse_macro_input, spanned::Spanned, token::Pub, Visibility};
mod params;
@@ -331,6 +332,31 @@ fn normalized_call_site(site: proc_macro::Span) -> Option<String> {
}
}
/// This behaves like the [`view`] macro, but loads the view from an external file instead of
/// parsing it inline.
///
/// This is designed to allow editing views in a separate file, if this improves a user's workflow.
///
/// The file is loaded and parsed during proc-macro execution, and its path is resolved relative to
/// the crate root rather than relative to the file from which it is called.
#[proc_macro_error::proc_macro_error]
#[proc_macro]
pub fn include_view(tokens: TokenStream) -> TokenStream {
let file_name = syn::parse::<syn::LitStr>(tokens).unwrap_or_else(|_| {
abort!(
Span::call_site(),
"the only supported argument is a string literal"
);
});
let file =
std::fs::read_to_string(file_name.value()).unwrap_or_else(|_| {
abort!(Span::call_site(), "could not open file");
});
let tokens = proc_macro2::TokenStream::from_str(&file)
.unwrap_or_else(|e| abort!(Span::call_site(), e));
view(tokens.into())
}
/// Annotates a function so that it can be used with your template as a Leptos `<Component/>`.
///
/// The `#[component]` macro allows you to annotate plain Rust functions as components