use alloc::vec::Vec;
use vize_l0::{Span, String};
use crate::dump::owned::expr::{Contract, Expr, own_expr};
use crate::dump::owned::{Attribute, Name, own_attribute, own_name};
use crate::op::BindingOp;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Binding {
Bind(Bind),
On(On),
Model(Model),
SlotContent(SlotContent),
SlotsObject(SlotsObject),
VueDirective(VueDirective),
VueCssBind(VueCssBind),
VueSync(VueSync),
VueSlotScope(VueSlotScope),
VueOnce(VueOnce),
VueMemo(VueMemo),
VueShow(VueShow),
VueHtml(VueHtml),
VueText(VueText),
VueCloak(VueCloak),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Bind {
pub name: Option<Name>,
pub modifiers: Vec<String>,
pub value: Option<Expr>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct On {
pub name: Option<Name>,
pub modifiers: Vec<String>,
pub handler: Option<Expr>,
pub native_handler: Option<u32>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Model {
pub contract: Contract,
pub argument: Option<Name>,
pub attributes: Vec<Attribute>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SlotContent {
pub name: Option<Name>,
pub modifiers: Vec<String>,
pub params: Option<Expr>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SlotsObject {
pub value: Expr,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueSync {
pub name: String,
pub modifiers: Vec<String>,
pub value: Expr,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueSlotScope {
pub name: Option<String>,
pub params: Option<Expr>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueDirective {
pub name: String,
pub argument: Option<Name>,
pub modifiers: Vec<String>,
pub value: Option<Expr>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueCssBind {
pub value: Expr,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueOnce {
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueMemo {
pub value: Expr,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueShow {
pub value: Expr,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueHtml {
pub value: Option<Expr>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueText {
pub value: Option<Expr>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VueCloak {
pub span: Span,
}
pub(super) fn own_binding(binding: &BindingOp<'_>) -> Binding {
match binding {
BindingOp::Bind(bind) => Binding::Bind(Bind {
name: bind.name.as_ref().map(own_name),
modifiers: own_modifiers(&bind.modifiers),
value: bind.value.as_ref().map(own_expr),
span: bind.span,
}),
BindingOp::On(on) => Binding::On(On {
name: on.name.as_ref().map(own_name),
modifiers: own_modifiers(&on.modifiers),
handler: on.expression().as_ref().map(own_expr),
native_handler: on
.handler
.and_then(|handler| handler.body())
.map(|id| id.node().index()),
span: on.span,
}),
BindingOp::Model(model) => Binding::Model(Model {
contract: Contract {
read: own_expr(&model.contract.read),
write: own_expr(&model.contract.write),
},
argument: model.argument.as_ref().map(own_name),
attributes: model.attributes.iter().map(own_attribute).collect(),
span: model.span,
}),
BindingOp::SlotContent(content) => Binding::SlotContent(SlotContent {
name: content.name.as_ref().map(own_name),
modifiers: own_modifiers(&content.modifiers),
params: content.params.as_ref().map(own_expr),
span: content.span,
}),
BindingOp::SlotsObject(object) => Binding::SlotsObject(SlotsObject {
value: own_expr(&object.value),
span: object.span,
}),
BindingOp::VueDirective(directive) => Binding::VueDirective(VueDirective {
name: String::from(directive.name),
argument: directive.argument.as_ref().map(own_name),
modifiers: own_modifiers(&directive.modifiers),
value: directive.value.as_ref().map(own_expr),
span: directive.span,
}),
BindingOp::VueCssBind(bind) => Binding::VueCssBind(VueCssBind {
value: own_expr(&bind.value),
span: bind.span,
}),
BindingOp::VueSync(sync) => Binding::VueSync(VueSync {
name: String::from(sync.name),
modifiers: own_modifiers(&sync.modifiers),
value: own_expr(&sync.value),
span: sync.span,
}),
BindingOp::VueSlotScope(scope) => Binding::VueSlotScope(VueSlotScope {
name: scope.name.map(String::from),
params: scope.params.as_ref().map(own_expr),
span: scope.span,
}),
BindingOp::VueOnce(once) => Binding::VueOnce(VueOnce { span: once.span }),
BindingOp::VueMemo(memo) => Binding::VueMemo(VueMemo {
value: own_expr(&memo.value),
span: memo.span,
}),
BindingOp::VueShow(show) => Binding::VueShow(VueShow {
value: own_expr(&show.value),
span: show.span,
}),
BindingOp::VueHtml(html) => Binding::VueHtml(VueHtml {
value: html.value.as_ref().map(own_expr),
span: html.span,
}),
BindingOp::VueText(text) => Binding::VueText(VueText {
value: text.value.as_ref().map(own_expr),
span: text.span,
}),
BindingOp::VueCloak(cloak) => Binding::VueCloak(VueCloak { span: cloak.span }),
}
}
fn own_modifiers(modifiers: &[&str]) -> Vec<String> {
modifiers.iter().map(|text| String::from(*text)).collect()
}