[rust] deny unused fields on non-node structs

Rejects unknown fields during deserialization of helper structs (which don't 
have a `type` field). This would be super useful for AST `Node` types, too, but 
that requires implementing a custom deserializer so i'm punting on that for now.
This commit is contained in:
Joe Savona
2023-07-25 16:22:57 -07:00
parent f21270a97d
commit 8bc6184728
2 changed files with 6 additions and 0 deletions
@@ -5,17 +5,20 @@ use serde::ser::{Serializer, SerializeMap};
use serde::{Serialize, Deserialize};
use crate::{JsValue, Binding, SourceRange};
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct SourceLocation {
pub source: Option<String>,
pub start: Position,
pub end: Position,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Position {
pub line: NonZeroU32,
pub column: u32,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Function {
pub id: Option<Identifier>,
pub params: Vec<Pattern>,
@@ -32,11 +35,13 @@ pub struct Function {
pub range: Option<SourceRange>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct RegExpValue {
pub pattern: String,
pub flags: String,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct TemplateElementValue {
pub cooked: Option<String>,
pub raw: String,
@@ -90,6 +90,7 @@ impl Object {
quote! {
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct #name {
#(#fields),*
}