1use crate::{NonNegativeNumber, Type};
18use snarkvm::console::program::ArrayType as ConsoleArrayType;
19
20use leo_span::Symbol;
21use serde::{Deserialize, Serialize};
22use snarkvm::prelude::Network;
23use std::fmt;
24
25#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
27pub struct ArrayType {
28 element_type: Box<Type>,
29 length: NonNegativeNumber,
30}
31
32impl ArrayType {
33 pub fn new(element: Type, length: NonNegativeNumber) -> Self {
35 Self { element_type: Box::new(element), length }
36 }
37
38 pub fn element_type(&self) -> &Type {
40 &self.element_type
41 }
42
43 pub fn length(&self) -> usize {
45 self.length.value()
46 }
47
48 pub fn base_element_type(&self) -> &Type {
50 match self.element_type.as_ref() {
51 Type::Array(array_type) => array_type.base_element_type(),
52 type_ => type_,
53 }
54 }
55
56 pub fn from_snarkvm<N: Network>(array_type: &ConsoleArrayType<N>, program: Option<Symbol>) -> Self {
57 Self {
58 element_type: Box::new(Type::from_snarkvm(array_type.next_element_type(), program)),
59 length: NonNegativeNumber::from(array_type.length().to_string().replace("u32", "")),
60 }
61 }
62}
63
64impl fmt::Display for ArrayType {
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 write!(f, "[{}; {}]", self.element_type, self.length)
67 }
68}