feat: Renamed all attributes to typed
This commit is contained in:
@@ -287,9 +287,12 @@ fn parse_fields(
|
||||
let mut as_name = LitStr::new(&name.to_string(), name.span());
|
||||
|
||||
for attr in &field.attrs {
|
||||
if attr.path().is_ident("serde")
|
||||
if attr.path().is_ident("typed")
|
||||
&& let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("default") {
|
||||
if meta.path.is_ident("as") {
|
||||
let value = meta.value()?;
|
||||
as_name = value.parse()?;
|
||||
} else if meta.path.is_ident("default") {
|
||||
default = true;
|
||||
if meta.input.peek(Token![=]) {
|
||||
meta.input.parse::<Token![=]>()?;
|
||||
@@ -298,11 +301,7 @@ fn parse_fields(
|
||||
} else if meta.path.is_ident("flatten") {
|
||||
flatten = true;
|
||||
} else {
|
||||
// Parse away any additional token that we don't care about
|
||||
if meta.input.peek(Token![=]) {
|
||||
meta.input.parse::<Token![=]>()?;
|
||||
meta.input.parse::<LitStr>()?;
|
||||
}
|
||||
return Err(syn::Error::new(meta.path.span(), "Unknown attribute"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -310,21 +309,6 @@ fn parse_fields(
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
if attr.path().is_ident("typed")
|
||||
&& let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("as") {
|
||||
let value = meta.value()?;
|
||||
as_name = value.parse()?;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(syn::Error::new(meta.path.span(), "Unknown attribute"))
|
||||
}
|
||||
})
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
fields.push(StructField {
|
||||
@@ -339,7 +323,7 @@ fn parse_fields(
|
||||
Ok(fields)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Typed, attributes(serde, typed))]
|
||||
#[proc_macro_derive(Typed, attributes(typed))]
|
||||
pub fn typed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
@@ -358,9 +342,12 @@ fn typed_inner(ast: DeriveInput) -> syn::Result<TokenStream2> {
|
||||
let mut case = None;
|
||||
let mut tag = None;
|
||||
for attr in &ast.attrs {
|
||||
if attr.path().is_ident("serde")
|
||||
if attr.path().is_ident("typed")
|
||||
&& let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("rename_all") {
|
||||
if meta.path.is_ident("as") {
|
||||
let value = meta.value()?;
|
||||
as_name = value.parse()?;
|
||||
} else if meta.path.is_ident("rename_all") {
|
||||
let value = meta.value()?;
|
||||
let case_name: LitStr = value.parse()?;
|
||||
|
||||
@@ -378,11 +365,11 @@ fn typed_inner(ast: DeriveInput) -> syn::Result<TokenStream2> {
|
||||
"Typed does not support this type of rename",
|
||||
)),
|
||||
}?);
|
||||
}
|
||||
|
||||
if meta.path.is_ident("tag") {
|
||||
} else if meta.path.is_ident("tag") {
|
||||
meta.input.parse::<Token![=]>()?;
|
||||
tag = Some(meta.input.parse::<LitStr>()?);
|
||||
} else {
|
||||
return Err(syn::Error::new(meta.path.span(), "Unknown attribute"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -390,21 +377,6 @@ fn typed_inner(ast: DeriveInput) -> syn::Result<TokenStream2> {
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
if attr.path().is_ident("typed")
|
||||
&& let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("as") {
|
||||
let value = meta.value()?;
|
||||
as_name = value.parse()?;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(syn::Error::new(meta.path.span(), "Unknown attribute"))
|
||||
}
|
||||
})
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
if as_name.value().is_empty() {
|
||||
|
||||
@@ -8,7 +8,7 @@ pub struct B {
|
||||
|
||||
#[derive(Typed)]
|
||||
pub struct A {
|
||||
#[serde(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub b: B,
|
||||
pub cool: u32,
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
use lua_typed::Typed;
|
||||
|
||||
#[derive(Typed)]
|
||||
#[serde(rename_all = "snake_case", tag = "action")]
|
||||
#[typed(rename_all = "snake_case", tag = "action")]
|
||||
pub enum ActionType {
|
||||
Broadcast { extras: HashMap<String, String> },
|
||||
// View,
|
||||
@@ -23,7 +23,7 @@ fn action_type() {
|
||||
|
||||
#[derive(Typed)]
|
||||
#[repr(u8)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[typed(rename_all = "snake_case")]
|
||||
pub enum Priority {
|
||||
Min = 1,
|
||||
Low,
|
||||
@@ -46,7 +46,7 @@ fn priority() {
|
||||
|
||||
#[derive(Typed)]
|
||||
pub struct Action {
|
||||
#[serde(flatten)]
|
||||
#[typed(flatten)]
|
||||
pub action: ActionType,
|
||||
pub label: String,
|
||||
pub clear: Option<bool>,
|
||||
@@ -69,10 +69,10 @@ fn action() {
|
||||
pub struct Notification {
|
||||
pub title: String,
|
||||
pub message: Option<String>,
|
||||
#[serde(default)]
|
||||
#[typed(default)]
|
||||
pub tags: Vec<String>,
|
||||
pub priority: Option<Priority>,
|
||||
#[serde(default)]
|
||||
#[typed(default)]
|
||||
pub actions: Vec<Action>,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use lua_typed::Typed;
|
||||
|
||||
#[derive(Typed)]
|
||||
#[serde(rename_all = "invalid/case")]
|
||||
#[typed(rename_all = "invalid/case")]
|
||||
pub enum Test {
|
||||
HelloWorld,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
error: Typed does not support this type of rename
|
||||
--> tests/ui/invalid_rename.rs:4:22
|
||||
|
|
||||
4 | #[serde(rename_all = "invalid/case")]
|
||||
4 | #[typed(rename_all = "invalid/case")]
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use lua_typed::Typed;
|
||||
|
||||
#[derive(Typed)]
|
||||
#[serde(tag = "tag")]
|
||||
#[typed(tag = "tag")]
|
||||
pub enum Test {
|
||||
A,
|
||||
B(String),
|
||||
|
||||
Reference in New Issue
Block a user