Skip to content

Commit

Permalink
Merge pull request #163 from Wicpar/main
Browse files Browse the repository at this point in the history
Compile time scss macro
  • Loading branch information
kaj authored Jan 7, 2023
2 parents 293d064 + 542ca4d commit 571f54d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rsass-macro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Rust template
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

13 changes: 13 additions & 0 deletions rsass-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rsass-macro"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
syn = { version = "1.0.106", features = ["full", "extra-traits"] }
quote = "1.0.22"
proc-macro2 = "1.0.32"
rsass = { path = ".." }
29 changes: 29 additions & 0 deletions rsass-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::ToTokens;
use syn::{Error, LitStr, parse_macro_input};
use rsass::output::{Format, Style};

#[proc_macro]
pub fn scss(tokens: TokenStream) -> TokenStream {
let input = parse_macro_input!(tokens as LitStr);
let format = Format {
style: Style::Compressed,
precision: 10,
};
match rsass::compile_scss(input.value().as_bytes(), format) {
Ok(output) => {
let output = core::str::from_utf8(&output).unwrap();
LitStr::new(output, Span::call_site())
.to_token_stream()
.into()
}
Err(err) => {
let msg = format!("{err:?}");
Error::new(Span::call_site(), msg)
.into_compile_error()
.into()
}
}
}
13 changes: 13 additions & 0 deletions rsass-macro/tests/test_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#[test]
fn test_macro() {
const CSS: &'static str = rsass_macro::scss!(r#"
.class {
background: red;
&:hover {
background: blue;
}
}
"#);
assert_eq!(".class{background:red}.class:hover{background:blue}\n", CSS)
}

0 comments on commit 571f54d

Please sign in to comment.