Merge remote-tracking branch 'origin/master' into pch/wiggle_error_transforms

This commit is contained in:
Pat Hickey
2020-05-30 13:20:30 -07:00
71 changed files with 1297 additions and 409 deletions

View File

@@ -1,12 +1,16 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use proc_macro2::Span;
use syn::{
braced, bracketed,
parse::{Parse, ParseStream},
punctuated::Punctuated,
Error, Ident, LitStr, Result, Token,
use {
proc_macro2::Span,
std::{
collections::HashMap,
iter::FromIterator,
path::{Path, PathBuf},
},
syn::{
braced, bracketed,
parse::{Parse, ParseStream},
punctuated::Punctuated,
Error, Ident, LitStr, Result, Token,
},
};
#[derive(Debug, Clone)]
@@ -26,7 +30,8 @@ pub enum ConfigField {
impl ConfigField {
pub fn parse_pair(ident: &str, value: ParseStream, err_loc: Span) -> Result<Self> {
match ident {
"witx" => Ok(ConfigField::Witx(value.parse()?)),
"witx" => Ok(ConfigField::Witx(WitxConf::Paths(value.parse()?))),
"witx_literal" => Ok(ConfigField::Witx(WitxConf::Literal(value.parse()?))),
"ctx" => Ok(ConfigField::Ctx(value.parse()?)),
"errors" => Ok(ConfigField::Error(value.parse()?)),
_ => Err(Error::new(err_loc, "expected `witx`, `ctx`, or `errors`")),
@@ -79,6 +84,15 @@ impl Config {
errors: errors.take().unwrap_or_default(),
})
}
/// Load the `witx` document for the configuration.
///
/// # Panics
///
/// This method will panic if the paths given in the `witx` field were not valid documents.
pub fn load_document(&self) -> witx::Document {
self.witx.load_document()
}
}
impl Parse for Config {
@@ -91,31 +105,110 @@ impl Parse for Config {
}
}
/// The witx document(s) that will be loaded from a [`Config`](struct.Config.html).
///
/// A witx interface definition can be provided either as a collection of relative paths to
/// documents, or as a single inlined string literal. Note that `(use ...)` directives are not
/// permitted when providing a string literal.
#[derive(Debug, Clone)]
pub struct WitxConf {
pub paths: Vec<PathBuf>,
pub enum WitxConf {
/// A collection of paths pointing to witx files.
Paths(Paths),
/// A single witx document, provided as a string literal.
Literal(Literal),
}
impl WitxConf {
/// Load the `witx` document.
///
/// # Panics
///
/// This method will panic if the paths given in the `witx` field were not valid documents, or
/// if any of the given documents were not syntactically valid.
pub fn load_document(&self) -> witx::Document {
match self {
Self::Paths(paths) => witx::load(paths.as_ref()).expect("loading witx"),
Self::Literal(doc) => witx::parse(doc.as_ref()).expect("parsing witx"),
}
}
/// If using the [`Paths`][paths] syntax, make all paths relative to a root directory.
///
/// [paths]: enum.WitxConf.html#variant.Paths
pub fn make_paths_relative_to<P: AsRef<Path>>(&mut self, root: P) {
self.paths.iter_mut().for_each(|p| {
if !p.is_absolute() {
*p = PathBuf::from(root.as_ref()).join(p.clone());
}
});
if let Self::Paths(paths) = self {
paths.as_mut().iter_mut().for_each(|p| {
if !p.is_absolute() {
*p = PathBuf::from(root.as_ref()).join(p.clone());
}
});
}
}
}
impl Parse for WitxConf {
/// A collection of paths, pointing to witx documents.
#[derive(Debug, Clone)]
pub struct Paths(Vec<PathBuf>);
impl Paths {
/// Create a new, empty collection of paths.
pub fn new() -> Self {
Default::default()
}
}
impl Default for Paths {
fn default() -> Self {
Self(Default::default())
}
}
impl AsRef<[PathBuf]> for Paths {
fn as_ref(&self) -> &[PathBuf] {
self.0.as_ref()
}
}
impl AsMut<[PathBuf]> for Paths {
fn as_mut(&mut self) -> &mut [PathBuf] {
self.0.as_mut()
}
}
impl FromIterator<PathBuf> for Paths {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = PathBuf>,
{
Self(iter.into_iter().collect())
}
}
impl Parse for Paths {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let _ = bracketed!(content in input);
let path_lits: Punctuated<LitStr, Token![,]> = content.parse_terminated(Parse::parse)?;
let paths = path_lits
Ok(path_lits
.iter()
.map(|lit| PathBuf::from(lit.value()))
.collect();
Ok(WitxConf { paths })
.collect())
}
}
/// A single witx document, provided as a string literal.
#[derive(Debug, Clone)]
pub struct Literal(String);
impl AsRef<str> for Literal {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl Parse for Literal {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self(input.parse::<syn::LitStr>()?.value()))
}
}