use super::Expr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Pat {
Ident(String),
Object(Vec<PatField>),
Array(Vec<Option<Pat>>, Option<String>),
Rest(Box<Pat>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PatField {
pub key: String,
pub pat: Pat,
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Expr>,
}
impl Pat {
pub fn ident(name: impl Into<String>) -> Self {
Pat::Ident(name.into())
}
pub fn object(fields: Vec<PatField>) -> Self {
Pat::Object(fields)
}
pub fn array(elements: Vec<Option<Pat>>, rest: Option<String>) -> Self {
Pat::Array(elements, rest)
}
}
impl PatField {
pub fn shorthand(key: impl Into<String>) -> Self {
let k = key.into();
Self {
pat: Pat::Ident(k.clone()),
key: k,
default: None,
}
}
pub fn renamed(key: impl Into<String>, name: impl Into<String>) -> Self {
Self {
key: key.into(),
pat: Pat::Ident(name.into()),
default: None,
}
}
pub fn nested(key: impl Into<String>, pat: Pat) -> Self {
Self {
key: key.into(),
pat,
default: None,
}
}
pub fn with_default(mut self, default: Expr) -> Self {
self.default = Some(default);
self
}
}