we now parse witx paths and the ctx type name in the macro invocation
This commit is contained in:
@@ -1,64 +1,98 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use proc_macro2::Span;
|
||||
use syn::{
|
||||
bracketed,
|
||||
braced, bracketed,
|
||||
parse::{Parse, ParseStream},
|
||||
punctuated::Punctuated,
|
||||
token, LitStr, Result, Token,
|
||||
Error, Ident, LitStr, Result, Token,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
_bracket_token: token::Bracket,
|
||||
path_lits: Punctuated<LitStr, Token![,]>,
|
||||
pub witx: WitxConf,
|
||||
pub ctx: CtxConf,
|
||||
}
|
||||
|
||||
enum ConfigField {
|
||||
Witx(WitxConf),
|
||||
Ctx(CtxConf),
|
||||
}
|
||||
|
||||
impl Parse for ConfigField {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let id: Ident = input.parse()?;
|
||||
let _colon: Token![:] = input.parse()?;
|
||||
match id.to_string().as_ref() {
|
||||
"witx" => Ok(ConfigField::Witx(input.parse()?)),
|
||||
"ctx" => Ok(ConfigField::Ctx(input.parse()?)),
|
||||
_ => Err(Error::new(id.span(), "expected `witx` or `ctx`")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn witx_paths(&self) -> Vec<PathBuf> {
|
||||
self.path_lits
|
||||
.iter()
|
||||
.map(|lit| PathBuf::from(lit.value()))
|
||||
.collect()
|
||||
fn build(fields: impl Iterator<Item = ConfigField>, err_loc: Span) -> Result<Self> {
|
||||
let mut witx = None;
|
||||
let mut ctx = None;
|
||||
for f in fields {
|
||||
match f {
|
||||
ConfigField::Witx(c) => {
|
||||
witx = Some(c);
|
||||
}
|
||||
ConfigField::Ctx(c) => {
|
||||
ctx = Some(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Config {
|
||||
witx: witx
|
||||
.take()
|
||||
.ok_or_else(|| Error::new(err_loc, "`witx` field required"))?,
|
||||
ctx: ctx
|
||||
.take()
|
||||
.ok_or_else(|| Error::new(err_loc, "`ctx` field required"))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for Config {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let contents;
|
||||
let _lbrace = braced!(contents in input);
|
||||
let fields: Punctuated<ConfigField, Token![,]> =
|
||||
contents.parse_terminated(ConfigField::parse)?;
|
||||
Ok(Config::build(fields.into_iter(), input.span())?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WitxConf {
|
||||
pub paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl Parse for WitxConf {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let content;
|
||||
Ok(Config {
|
||||
_bracket_token: bracketed!(content in input),
|
||||
path_lits: content.parse_terminated(Parse::parse)?,
|
||||
let _ = bracketed!(content in input);
|
||||
let path_lits: Punctuated<LitStr, Token![,]> = content.parse_terminated(Parse::parse)?;
|
||||
let paths: Vec<PathBuf> = path_lits
|
||||
.iter()
|
||||
.map(|lit| PathBuf::from(lit.value()))
|
||||
.collect();
|
||||
Ok(WitxConf { paths })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CtxConf {
|
||||
pub name: Ident,
|
||||
}
|
||||
|
||||
impl Parse for CtxConf {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
Ok(CtxConf {
|
||||
name: input.parse()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
let arg_strings = args
|
||||
.into_iter()
|
||||
.map(|arg| match arg {
|
||||
TokenTree::Literal(lit) => string_literal(lit),
|
||||
_ => bail!("expected string literal, got: {:?}", arg),
|
||||
})
|
||||
.collect::<Result<Vec<String>>>()?;
|
||||
|
||||
if arg_strings.is_empty() {
|
||||
bail!("expected at least one argument");
|
||||
}
|
||||
Ok(arg_strings)
|
||||
}
|
||||
|
||||
fn string_literal(literal: Literal) -> Result<String> {
|
||||
let s = literal.to_string();
|
||||
if !s.starts_with('"') || !s.ends_with('"') {
|
||||
bail!("string literal must be enclosed in double quotes");
|
||||
}
|
||||
|
||||
let trimmed = s[1..s.len() - 1].to_owned();
|
||||
if trimmed.contains('"') {
|
||||
bail!("string literal must not contain quotes");
|
||||
}
|
||||
if trimmed.contains('\\') {
|
||||
bail!("string literal must not contain backslashes");
|
||||
}
|
||||
Ok(trimmed)
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user