mirror of
https://github.com/xazukx/ts-rs.git
synced 2026-11-19 22:40:25 +00:00
Parse WherePredicate to improve error locations
This commit is contained in:
+11
-5
@@ -1,12 +1,14 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use syn::{Attribute, Ident, Result, Type};
|
||||
use syn::{Attribute, Ident, Result, Type, WherePredicate};
|
||||
|
||||
use crate::{
|
||||
attr::{parse_assign_inflection, parse_assign_str, parse_concrete, Inflection},
|
||||
utils::{parse_attrs, parse_docs},
|
||||
};
|
||||
|
||||
use super::parse_bound;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EnumAttr {
|
||||
pub rename_all: Option<Inflection>,
|
||||
@@ -16,7 +18,7 @@ pub struct EnumAttr {
|
||||
pub export: bool,
|
||||
pub docs: String,
|
||||
pub concrete: HashMap<Ident, Type>,
|
||||
pub bound: Option<String>,
|
||||
pub bound: Option<Vec<WherePredicate>>,
|
||||
tag: Option<String>,
|
||||
untagged: bool,
|
||||
content: Option<String>,
|
||||
@@ -85,7 +87,10 @@ impl EnumAttr {
|
||||
self.export_to = self.export_to.take().or(export_to);
|
||||
self.docs = docs;
|
||||
self.concrete.extend(concrete);
|
||||
self.bound = self.bound.take().or(bound);
|
||||
self.bound = self.bound
|
||||
.take()
|
||||
.map(|b| b.into_iter().chain(bound.clone().unwrap_or_default()).collect())
|
||||
.or(bound);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +105,7 @@ impl_parse! {
|
||||
"content" => out.content = Some(parse_assign_str(input)?),
|
||||
"untagged" => out.untagged = true,
|
||||
"concrete" => out.concrete = parse_concrete(input)?,
|
||||
"bound" => out.bound = Some(parse_assign_str(input)?),
|
||||
"bound" => out.bound = Some(parse_bound(input)?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +117,7 @@ impl_parse! {
|
||||
"rename_all_fields" => out.0.rename_all_fields = Some(parse_assign_inflection(input)?),
|
||||
"tag" => out.0.tag = Some(parse_assign_str(input)?),
|
||||
"content" => out.0.content = Some(parse_assign_str(input)?),
|
||||
"untagged" => out.0.untagged = true
|
||||
"untagged" => out.0.untagged = true,
|
||||
"bound" => out.0.bound = Some(parse_bound(input)?),
|
||||
}
|
||||
}
|
||||
|
||||
+20
-3
@@ -4,8 +4,8 @@ pub use field::*;
|
||||
pub use r#enum::*;
|
||||
pub use r#struct::*;
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
Error, Lit, Result, Token,
|
||||
parse::{Parse, ParseStream, Parser},
|
||||
Error, Lit, Result, Token, WherePredicate, punctuated::Punctuated,
|
||||
};
|
||||
pub use variant::*;
|
||||
|
||||
@@ -107,7 +107,7 @@ fn parse_concrete(input: ParseStream) -> Result<HashMap<syn::Ident, syn::Type>>
|
||||
syn::parenthesized!(content in input);
|
||||
|
||||
Ok(
|
||||
syn::punctuated::Punctuated::<Concrete, Token![,]>::parse_terminated(&content)?
|
||||
Punctuated::<Concrete, Token![,]>::parse_terminated(&content)?
|
||||
.into_iter()
|
||||
.map(|concrete| (concrete.ident, concrete.ty))
|
||||
.collect(),
|
||||
@@ -128,3 +128,20 @@ where
|
||||
other => Err(Error::new(other.span(), "expected string")),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_bound(input: ParseStream) -> Result<Vec<WherePredicate>> {
|
||||
input.parse::<Token![=]>()?;
|
||||
match Lit::parse(input)? {
|
||||
Lit::Str(string) => {
|
||||
let parser = Punctuated::<WherePredicate, Token![,]>::parse_terminated;
|
||||
|
||||
Ok(
|
||||
parser.parse_str(&string.value())
|
||||
.map_err(|e| Error::new(string.span(), e.to_string()))?
|
||||
.into_iter()
|
||||
.collect()
|
||||
)
|
||||
},
|
||||
other => Err(Error::new(other.span(), "expected string")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use std::{collections::HashMap, convert::TryFrom};
|
||||
|
||||
use syn::{Attribute, Ident, Result, Type};
|
||||
use syn::{Attribute, Ident, Result, Type, WherePredicate};
|
||||
|
||||
use crate::{
|
||||
attr::{parse_assign_str, parse_concrete, Inflection, VariantAttr},
|
||||
utils::{parse_attrs, parse_docs},
|
||||
};
|
||||
|
||||
use super::parse_bound;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct StructAttr {
|
||||
pub rename_all: Option<Inflection>,
|
||||
@@ -16,7 +18,7 @@ pub struct StructAttr {
|
||||
pub tag: Option<String>,
|
||||
pub docs: String,
|
||||
pub concrete: HashMap<Ident, Type>,
|
||||
pub bound: Option<String>,
|
||||
pub bound: Option<Vec<WherePredicate>>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-compat")]
|
||||
@@ -56,7 +58,10 @@ impl StructAttr {
|
||||
self.tag = self.tag.take().or(tag);
|
||||
self.docs = docs;
|
||||
self.concrete.extend(concrete);
|
||||
self.bound = self.bound.take().or(bound);
|
||||
self.bound = self.bound
|
||||
.take()
|
||||
.map(|b| b.into_iter().chain(bound.clone().unwrap_or_default()).collect())
|
||||
.or(bound);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +88,7 @@ impl_parse! {
|
||||
"export" => out.export = true,
|
||||
"export_to" => out.export_to = Some(parse_assign_str(input)?),
|
||||
"concrete" => out.concrete = parse_concrete(input)?,
|
||||
"bound" => out.bound = Some(parse_assign_str(input)?),
|
||||
"bound" => out.bound = Some(parse_bound(input)?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +98,7 @@ impl_parse! {
|
||||
"rename" => out.0.rename = Some(parse_assign_str(input)?),
|
||||
"rename_all" => out.0.rename_all = Some(parse_assign_str(input).and_then(Inflection::try_from)?),
|
||||
"tag" => out.0.tag = Some(parse_assign_str(input)?),
|
||||
"bound" => out.0.bound = Some(parse_bound(input)?),
|
||||
// parse #[serde(default)] to not emit a warning
|
||||
"deny_unknown_fields" | "default" => {
|
||||
use syn::Token;
|
||||
|
||||
+8
-5
@@ -8,7 +8,7 @@ use quote::{format_ident, quote};
|
||||
use syn::{
|
||||
parse_quote, spanned::Spanned, ConstParam, GenericParam, Generics, Item, LifetimeParam, Result,
|
||||
Type, TypeArray, TypeParam, TypeParen, TypePath, TypeReference, TypeSlice, TypeTuple,
|
||||
WhereClause,
|
||||
WhereClause, WherePredicate,
|
||||
};
|
||||
|
||||
use crate::{deps::Dependencies, utils::format_generics};
|
||||
@@ -26,7 +26,7 @@ struct DerivedTS {
|
||||
inline_flattened: Option<TokenStream>,
|
||||
dependencies: Dependencies,
|
||||
concrete: HashMap<Ident, Type>,
|
||||
bound: Option<String>,
|
||||
bound: Option<Vec<WherePredicate>>,
|
||||
|
||||
export: bool,
|
||||
export_to: Option<String>,
|
||||
@@ -301,7 +301,7 @@ fn generate_assoc_type(
|
||||
fn generate_impl_block_header(
|
||||
ty: &Ident,
|
||||
generics: &Generics,
|
||||
bounds: Option<&str>,
|
||||
bounds: Option<&[WherePredicate]>,
|
||||
dependencies: &Dependencies,
|
||||
) -> TokenStream {
|
||||
use GenericParam as G;
|
||||
@@ -338,8 +338,11 @@ fn generate_impl_block_header(
|
||||
quote! { #bounds }
|
||||
},
|
||||
|bounds| {
|
||||
let bounds = syn::parse_str::<TokenStream>(bounds).expect("malformed `bounds`");
|
||||
quote! { where #bounds }
|
||||
if !bounds.is_empty() {
|
||||
quote! { where #(#bounds),* }
|
||||
} else {
|
||||
quote!()
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user