chore: pre0.1
This commit is contained in:
parent
bfe28820e3
commit
5d18f9201e
3 changed files with 114 additions and 238 deletions
|
|
@ -1,10 +1,10 @@
|
|||
use std::{fmt::Debug, fs, path::PathBuf, str::FromStr};
|
||||
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use quote::{quote, ToTokens, TokenStreamExt};
|
||||
use quote::{ToTokens, TokenStreamExt, quote};
|
||||
use regex::Regex;
|
||||
use sabry_intrnl::syntax::ostrta::OneSyntaxToRuleThemAll;
|
||||
use syn::{braced, parse::Parse, Ident, LitStr, Token};
|
||||
use syn::{Ident, LitStr, Token, braced, parse::Parse};
|
||||
|
||||
pub mod scssy;
|
||||
pub mod styly;
|
||||
|
|
@ -31,20 +31,15 @@ impl ArbitraryStyleBlock {
|
|||
&self.code
|
||||
}
|
||||
|
||||
pub fn parse_syn(
|
||||
input: syn::parse::ParseStream,
|
||||
use_code_path_prefix: Option<PathBuf>,
|
||||
) -> syn::Result<Self> {
|
||||
pub fn parse_syn(input: syn::parse::ParseStream, path_prefix: PathBuf) -> syn::Result<Self> {
|
||||
let (code, span) = if let Ok(path_tok) = input.parse::<LitStr>() {
|
||||
let path = if let Some(prefix) = use_code_path_prefix {
|
||||
let path = {
|
||||
let p = path_tok.value();
|
||||
if let Some(pp) = p.strip_prefix("./") {
|
||||
prefix.join(pp)
|
||||
path_prefix.join(pp)
|
||||
} else {
|
||||
PathBuf::from_str(&p).expect("Unable to convert path into PathBuf")
|
||||
PathBuf::from_str(&p).unwrap() // infallible
|
||||
}
|
||||
} else {
|
||||
PathBuf::from_str(&path_tok.value()).expect("Unable to convert path into PathBuf")
|
||||
};
|
||||
|
||||
let fullpath = match path.canonicalize() {
|
||||
|
|
@ -52,8 +47,11 @@ impl ArbitraryStyleBlock {
|
|||
Err(e) => {
|
||||
return Err(syn::Error::new(
|
||||
path_tok.span(),
|
||||
format!("Could not use path {path:?}: {:?}. If the path is relative and 'nightly' feature flag is set - this is likely false-positive", e.kind()),
|
||||
))
|
||||
format!(
|
||||
"Could not use path {path:?}: {:?}. If the path is relative and 'nightly' feature flag is set - this is likely false-positive",
|
||||
e.kind()
|
||||
),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -63,7 +61,7 @@ impl ArbitraryStyleBlock {
|
|||
return Err(syn::Error::new(
|
||||
path_tok.span(),
|
||||
format!("Could not read file at {fullpath:?}"),
|
||||
))
|
||||
));
|
||||
}
|
||||
};
|
||||
let code = String::from_utf8_lossy(&iofile).to_string();
|
||||
|
|
@ -79,7 +77,10 @@ impl ArbitraryStyleBlock {
|
|||
let c = ident_regex.replace_all(&c, "\n").to_string();
|
||||
(c, stream.span())
|
||||
} else {
|
||||
return Err(syn::Error::new(s.span(), "Use \"\" within the braces to specify your SASS/SCSS. Unquoted style syntax is reserved for the future. Unquoted SCSS/SASS doesnt make sense though, as you won't benefit from it in rust file.\n\ntip: use `{\"style\"}` instead of `{style}`"));
|
||||
return Err(syn::Error::new(
|
||||
s.span(),
|
||||
"Use \"\" within the braces to specify your SASS/SCSS. Unquoted style syntax is reserved for the future. Unquoted SCSS/SASS doesnt make sense though, as you won't benefit from it in rust file.\n\ntip: use `{\"style\"}` instead of `{style}`",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -87,12 +88,6 @@ impl ArbitraryStyleBlock {
|
|||
}
|
||||
}
|
||||
|
||||
impl Parse for ArbitraryStyleBlock {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Self::parse_syn(input, None)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::to_string_trait_impl)]
|
||||
impl ToString for ArbitraryStyleBlock {
|
||||
fn to_string(&self) -> String {
|
||||
|
|
@ -148,56 +143,3 @@ impl Parse for ArbitraryStyleSyntax {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
fn arbitrary_style_block_sass() {
|
||||
use super::ArbitraryStyleBlock;
|
||||
|
||||
let code = "
|
||||
#a
|
||||
co: red
|
||||
.sel
|
||||
co: white
|
||||
&-dark
|
||||
& > div
|
||||
co: blue";
|
||||
let expect_code = "
|
||||
#a
|
||||
co: red
|
||||
.sel
|
||||
co: white
|
||||
&-dark
|
||||
& > div
|
||||
co: blue";
|
||||
let input = format!("{{\"{code}\"}}");
|
||||
let block = syn::parse_str::<ArbitraryStyleBlock>(&input).unwrap();
|
||||
|
||||
assert_eq!(expect_code, block.code);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arbitrary_style_block_scss() {
|
||||
use super::ArbitraryStyleBlock;
|
||||
|
||||
let code = "
|
||||
#a {
|
||||
c: r;
|
||||
}
|
||||
.b.c {
|
||||
c: 'into';
|
||||
}";
|
||||
let expect_code = "
|
||||
#a {
|
||||
c: r;
|
||||
}
|
||||
.b.c {
|
||||
c: 'into';
|
||||
}";
|
||||
let input = format!("{{\"{code}\"}}");
|
||||
let block = syn::parse_str::<ArbitraryStyleBlock>(&input).unwrap();
|
||||
|
||||
assert_eq!(expect_code, block.code)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@ use std::path::PathBuf;
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use sabry_intrnl::scoper::ArbitraryScope;
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
Ident,
|
||||
};
|
||||
use syn::{Ident, parse::ParseStream};
|
||||
|
||||
use super::{ArbitraryStyleBlock, ArbitraryStyleSyntax};
|
||||
|
||||
|
|
@ -16,7 +13,7 @@ use super::{ArbitraryStyleBlock, ArbitraryStyleSyntax};
|
|||
/// `$syntax`: sass/scss
|
||||
///
|
||||
/// `$code`: intended to be valid based on $syntax
|
||||
pub fn scssy_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> TokenStream {
|
||||
pub fn scssy_macro_impl(input: TokenStream, source_path: PathBuf) -> TokenStream {
|
||||
let MacroSyntax {
|
||||
ident,
|
||||
syntax,
|
||||
|
|
@ -36,7 +33,9 @@ pub fn scssy_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> Tok
|
|||
}
|
||||
|
||||
let sourcesass = code.to_string();
|
||||
let macro_doc = format!("Arbitrary {syntax:?} code declared with `scssy!`. Pretty usable in tandem with `usey!` and `buildy` at build time");
|
||||
let macro_doc = format!(
|
||||
"Arbitrary {syntax:?} code declared with `scssy!`. Pretty usable in tandem with `usey!` and `buildy` at build time"
|
||||
);
|
||||
|
||||
quote! {
|
||||
#[doc = #macro_doc]
|
||||
|
|
@ -55,17 +54,11 @@ pub struct MacroSyntax {
|
|||
}
|
||||
|
||||
impl MacroSyntax {
|
||||
pub fn parse_syn(
|
||||
input: syn::parse::ParseStream,
|
||||
source_path: Option<PathBuf>,
|
||||
) -> syn::Result<Self> {
|
||||
pub fn parse_syn(input: syn::parse::ParseStream, source_path: PathBuf) -> syn::Result<Self> {
|
||||
let ident = input.parse::<Ident>()?;
|
||||
let syntax = input.parse::<ArbitraryStyleSyntax>()?;
|
||||
|
||||
let code = match source_path {
|
||||
Some(sp) => ArbitraryStyleBlock::parse_syn(input, Some(sp))?,
|
||||
None => input.parse::<ArbitraryStyleBlock>()?,
|
||||
};
|
||||
let code = ArbitraryStyleBlock::parse_syn(input, source_path)?;
|
||||
|
||||
Ok(Self {
|
||||
ident,
|
||||
|
|
@ -75,11 +68,11 @@ impl MacroSyntax {
|
|||
}
|
||||
}
|
||||
|
||||
impl Parse for MacroSyntax {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Self::parse_syn(input, None)
|
||||
}
|
||||
}
|
||||
// impl Parse for MacroSyntax {
|
||||
// fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
// Self::parse_syn(input, None)
|
||||
// }
|
||||
// }
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ use proc_macro2::{Span, TokenStream};
|
|||
use quote::quote;
|
||||
use sabry_intrnl::{
|
||||
config::SabryConfig,
|
||||
scoper::{apply_basic_rusty_member_gen_rules, ArbitraryScope, ScopedSelector},
|
||||
scoper::{ArbitraryScope, ScopedSelector, sanitize_css_member},
|
||||
};
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
Ident, Token,
|
||||
parse::{Parse, ParseStream},
|
||||
};
|
||||
|
||||
use super::{ArbitraryStyleBlock, ArbitraryStyleSyntax};
|
||||
|
|
@ -32,7 +32,7 @@ use super::{ArbitraryStyleBlock, ArbitraryStyleSyntax};
|
|||
///
|
||||
/// The machine-readable output may also be forced by use `machine_readable: true` arg on `parse_macro_syntax` function
|
||||
/// without modifying tokenstream
|
||||
pub fn styly_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> TokenStream {
|
||||
pub fn styly_macro_impl(input: TokenStream, source_path: PathBuf) -> TokenStream {
|
||||
let config = match SabryConfig::require() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
|
|
@ -40,7 +40,7 @@ pub fn styly_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> Tok
|
|||
Span::call_site(),
|
||||
format!("Could not read sabry configuration required by this macro: {e:?}",),
|
||||
)
|
||||
.to_compile_error()
|
||||
.to_compile_error();
|
||||
}
|
||||
};
|
||||
let ms = match parse_macro_syntax(input, source_path) {
|
||||
|
|
@ -62,7 +62,7 @@ pub fn styly_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> Tok
|
|||
Span::call_site(),
|
||||
format!("Could not generate scope: {e:?}"),
|
||||
)
|
||||
.to_compile_error()
|
||||
.to_compile_error();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ pub fn styly_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> Tok
|
|||
})
|
||||
.unique()
|
||||
.map(|s| {
|
||||
let ident = apply_basic_rusty_member_gen_rules(&s);
|
||||
let ident = sanitize_css_member(&s);
|
||||
let fnident = syn::parse_str::<Ident>(format!("_{ident}").as_str())
|
||||
.expect("BUG: invalid ident for nesting selector formed");
|
||||
let formatstr = format!("{{c}}{}", &s);
|
||||
|
|
@ -223,7 +223,7 @@ pub fn styly_macro_impl(input: TokenStream, source_path: Option<PathBuf>) -> Tok
|
|||
// reusable function, that does not return tokenstream for machine-processing at build time
|
||||
pub fn parse_macro_syntax(
|
||||
input: TokenStream,
|
||||
source_path: Option<PathBuf>,
|
||||
source_path: PathBuf,
|
||||
) -> Result<MacroSyntax, syn::Error> {
|
||||
syn::parse::Parser::parse2(
|
||||
|input: ParseStream<'_>| MacroSyntax::parse_syn(input, source_path),
|
||||
|
|
@ -263,18 +263,11 @@ pub struct MacroSyntax {
|
|||
}
|
||||
|
||||
impl MacroSyntax {
|
||||
pub fn parse_syn(
|
||||
input: syn::parse::ParseStream,
|
||||
source_path: Option<PathBuf>,
|
||||
) -> syn::Result<Self> {
|
||||
pub fn parse_syn(input: syn::parse::ParseStream, source_path: PathBuf) -> syn::Result<Self> {
|
||||
let mut generator = input.parse::<ScopeGenerator>()?;
|
||||
let scope = input.parse::<Ident>()?;
|
||||
let syntax = input.parse::<ArbitraryStyleSyntax>()?;
|
||||
let code = if source_path.is_some() {
|
||||
ArbitraryStyleBlock::parse_syn(input, source_path)
|
||||
} else {
|
||||
input.parse::<ArbitraryStyleBlock>()
|
||||
}?;
|
||||
let code = ArbitraryStyleBlock::parse_syn(input, source_path)?;
|
||||
|
||||
if input.parse::<Token![#]>().is_ok() {
|
||||
generator = ScopeGenerator::MachineReadable
|
||||
|
|
@ -288,9 +281,3 @@ impl MacroSyntax {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for MacroSyntax {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Self::parse_syn(input, None)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue