Skip to content

Commit 4eb6172

Browse files
committed
use macro string
1 parent b77d6c7 commit 4eb6172

3 files changed

Lines changed: 12 additions & 66 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dioxus-code-macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ proc-macro = true
1616
[dependencies]
1717
arborium = { version = "2.16.0", default-features = false, features = ["all-languages"] }
1818
arborium-theme = "2.16.0"
19+
macro-string = "0.1.4"
1920
proc-macro-crate = "3.5.0"
2021
proc-macro2 = "1.0.103"
2122
quote = "1.0.42"

dioxus-code-macro/src/lib.rs

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use std::env;
55
use std::fs;
66
use std::path::{Path, PathBuf};
77

8+
use macro_string::MacroString;
89
use proc_macro::TokenStream;
910
use proc_macro_crate::{FoundCrate, crate_name};
1011
use proc_macro2::{Span, TokenStream as TokenStream2};
1112
use quote::{format_ident, quote};
1213
use syn::parse::{Parse, ParseStream};
13-
use syn::{Expr, Ident, LitStr, Token, parse_macro_input};
14+
use syn::{Ident, LitStr, Token, parse_macro_input};
1415

1516
/// Compile-time syntax highlighting.
1617
///
@@ -30,13 +31,13 @@ pub fn code(input: TokenStream) -> TokenStream {
3031
}
3132

3233
struct CodeInput {
33-
path: Expr,
34+
path: String,
3435
language: Option<LitStr>,
3536
}
3637

3738
impl Parse for CodeInput {
3839
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
39-
let path = input.parse()?;
40+
let MacroString(path) = input.parse()?;
4041
let mut language = None;
4142

4243
if input.peek(Token![,]) {
@@ -62,7 +63,7 @@ fn expand_code(input: CodeInput) -> syn::Result<TokenStream2> {
6263
let manifest_dir = env::var("CARGO_MANIFEST_DIR")
6364
.map_err(|error| syn::Error::new(Span::call_site(), error.to_string()))?;
6465
let manifest_dir = PathBuf::from(manifest_dir);
65-
let macro_path = eval_path_expr(&input.path)?;
66+
let macro_path = input.path;
6667
let absolute_path = resolve_manifest_path(&manifest_dir, &macro_path);
6768

6869
let source = fs::read_to_string(&absolute_path).map_err(|error| {
@@ -193,71 +194,14 @@ fn dioxus_code_crate_path() -> syn::Result<TokenStream2> {
193194
}
194195

195196
fn resolve_manifest_path(manifest_dir: &Path, path: &str) -> PathBuf {
197+
let path_buf = PathBuf::from(path);
198+
if path_buf.is_absolute() && (path_buf.exists() || path_buf.starts_with(manifest_dir)) {
199+
return path_buf;
200+
}
201+
196202
if let Some(stripped) = path.strip_prefix('/') {
197203
manifest_dir.join(stripped)
198204
} else {
199205
manifest_dir.join(path)
200206
}
201207
}
202-
203-
fn eval_path_expr(expr: &Expr) -> syn::Result<String> {
204-
match expr {
205-
Expr::Lit(expr_lit) => {
206-
if let syn::Lit::Str(lit) = &expr_lit.lit {
207-
Ok(lit.value())
208-
} else {
209-
Err(syn::Error::new_spanned(
210-
expr,
211-
"path must be a string literal",
212-
))
213-
}
214-
}
215-
Expr::Macro(expr_macro) => {
216-
let Some(ident) = expr_macro.mac.path.get_ident() else {
217-
return Err(syn::Error::new_spanned(
218-
expr,
219-
"only string literals, concat!, and env! are supported",
220-
));
221-
};
222-
223-
match ident.to_string().as_str() {
224-
"concat" => eval_concat(expr_macro.mac.tokens.clone()),
225-
"env" => eval_env(expr_macro.mac.tokens.clone()),
226-
_ => Err(syn::Error::new_spanned(
227-
expr,
228-
"only string literals, concat!, and env! are supported",
229-
)),
230-
}
231-
}
232-
_ => Err(syn::Error::new_spanned(
233-
expr,
234-
"only string literals, concat!, and env! are supported",
235-
)),
236-
}
237-
}
238-
239-
fn eval_concat(tokens: TokenStream2) -> syn::Result<String> {
240-
struct Args {
241-
exprs: syn::punctuated::Punctuated<Expr, Token![,]>,
242-
}
243-
244-
impl Parse for Args {
245-
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
246-
Ok(Self {
247-
exprs: syn::punctuated::Punctuated::parse_terminated(input)?,
248-
})
249-
}
250-
}
251-
252-
let args = syn::parse2::<Args>(tokens)?;
253-
let mut value = String::new();
254-
for expr in args.exprs {
255-
value.push_str(&eval_path_expr(&expr)?);
256-
}
257-
Ok(value)
258-
}
259-
260-
fn eval_env(tokens: TokenStream2) -> syn::Result<String> {
261-
let lit = syn::parse2::<LitStr>(tokens)?;
262-
env::var(lit.value()).map_err(|error| syn::Error::new(lit.span(), error.to_string()))
263-
}

0 commit comments

Comments
 (0)