leo_ast/statement/definition/
mod.rs1use crate::{Expression, Identifier, Node, NodeID, Statement, Type};
18
19use leo_span::Span;
20
21use itertools::Itertools as _;
22use serde::{Deserialize, Serialize};
23use std::fmt;
24
25#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
27pub struct DefinitionStatement {
28 pub place: DefinitionPlace,
30 pub type_: Option<Type>,
32 pub value: Expression,
34 pub span: Span,
36 pub id: NodeID,
38}
39
40#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
41pub enum DefinitionPlace {
42 Single(Identifier),
43 Multiple(Vec<Identifier>),
44}
45
46impl fmt::Display for DefinitionPlace {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 match self {
49 DefinitionPlace::Single(id) => id.fmt(f),
50 DefinitionPlace::Multiple(ids) => write!(f, "({})", ids.iter().format(", ")),
51 }
52 }
53}
54
55impl fmt::Display for DefinitionStatement {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 match &self.type_ {
58 Some(Type::Err) | None => write!(f, "let {} = {}", self.place, self.value),
60 Some(ty) => write!(f, "let {}: {} = {}", self.place, ty, self.value),
61 }
62 }
63}
64
65impl From<DefinitionStatement> for Statement {
66 fn from(value: DefinitionStatement) -> Self {
67 Statement::Definition(value)
68 }
69}
70
71crate::simple_node_impl!(DefinitionStatement);