Skip to main content

leo_ast/types/
composite.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{Expression, Path, Type};
18use itertools::Itertools as _;
19use serde::{Deserialize, Serialize};
20use std::fmt;
21
22/// A composite type of a identifier and external program name.
23#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
24pub struct CompositeType {
25    /// The path to the composite definition.
26    pub path: Path,
27    /// Expressions for the const arguments passed to the composite's const parameters.
28    pub const_arguments: Vec<Expression>,
29}
30
31impl fmt::Display for CompositeType {
32    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33        write!(f, "{}", self.path)?;
34
35        if !self.const_arguments.is_empty() {
36            write!(f, "::[{}]", self.const_arguments.iter().format(", "))?;
37        }
38
39        Ok(())
40    }
41}
42
43impl From<CompositeType> for Type {
44    fn from(value: CompositeType) -> Self {
45        Type::Composite(value)
46    }
47}