feat: relative style paths & component crate example

This commit is contained in:
Yiffy Dev 2024-11-16 22:32:38 +05:00 • committed by spushk
commit d24f68e6ee
Signed by: spushk
GPG key ID: 7797B761E105E8AA

View file

@ -1,4 +1,7 @@
use std::{fs, io, path::Path};
use std::{
fs, io,
path::{Path, PathBuf},
};
use proc_macro2::TokenStream;
use sabry_procmacro_impl::impls::styly;
@ -6,7 +9,7 @@ use syn::{spanned::Spanned, visit::Visit};
#[derive(Debug, thiserror::Error)]
pub enum FileVisitError {
#[error("Could not read file")]
#[error("tt read file")]
Read(#[from] io::Error),
#[error("styly! macro found, yet invalid")]
Styly(#[from] syn::Error),
@ -19,6 +22,7 @@ pub fn visit_file(path: &Path) -> Result<StylyVisitor, FileVisitError> {
let mut styly_visitor = StylyVisitor {
found_stylys: vec![],
path: path.to_owned(),
};
styly_visitor.visit_file(&code_file);
@ -34,17 +38,26 @@ pub fn visit_file(path: &Path) -> Result<StylyVisitor, FileVisitError> {
#[derive(Debug)]
pub struct StylyVisitor {
pub found_stylys: Vec<styly::MacroSyntax>,
pub path: PathBuf,
}
impl<'ast> Visit<'ast> for StylyVisitor {
fn visit_item_macro(&mut self, node: &'ast syn::ItemMacro) {
if node.mac.path.get_ident().is_some_and(|i| i == "styly") {
let body = node.mac.parse_body::<TokenStream>().unwrap_or_else(|_| {
panic!("could not parse `styly!` macro at {:?}", node.mac.span())
});
let macro_data = styly::parse_macro_syntax(body).unwrap_or_else(|_| {
panic!("could not parse `styly!` macro at {:?}", node.mac.span())
let body = node.mac.parse_body::<TokenStream>().unwrap_or_else(|e| {
panic!(
"could not parse `styly!` macro at {:?}: {e:?}",
node.mac.span()
)
});
let macro_data =
styly::parse_macro_syntax(body, self.path.parent().map(|p| p.to_owned()))
.unwrap_or_else(|e| {
panic!(
"could not parse `styly!` macro at {:?}: {e:?}",
node.mac.span()
)
});
self.found_stylys.push(macro_data);
}
}