use crate::{Expression, Identifier, Node, NodeID, Statement, Type};
use leo_span::Span;
use itertools::Itertools as _;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct DefinitionStatement {
pub place: DefinitionPlace,
pub type_: Option<Type>,
pub value: Expression,
pub span: Span,
pub id: NodeID,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub enum DefinitionPlace {
Single(Identifier),
Multiple(Vec<Identifier>),
}
impl fmt::Display for DefinitionPlace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DefinitionPlace::Single(id) => id.fmt(f),
DefinitionPlace::Multiple(ids) => write!(f, "({})", ids.iter().format(", ")),
}
}
}
impl fmt::Display for DefinitionStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.type_ {
Some(Type::Err) | None => write!(f, "let {} = {}", self.place, self.value),
Some(ty) => write!(f, "let {}: {} = {}", self.place, ty, self.value),
}
}
}
impl From<DefinitionStatement> for Statement {
fn from(value: DefinitionStatement) -> Self {
Statement::Definition(value)
}
}
crate::simple_node_impl!(DefinitionStatement);