mod.rs
 1  // Copyright (c) 2025 ADnet Contributors
 2  // This file is part of the AlphaVM library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  mod key;
17  use key::*;
18  
19  mod value;
20  use value::*;
21  
22  mod bytes;
23  mod parse;
24  
25  use console::{network::prelude::*, program::Identifier};
26  
27  #[derive(Clone, PartialEq, Eq)]
28  pub struct Mapping<N: Network> {
29      /// The name of the mapping.
30      name: Identifier<N>,
31      /// The key statement.
32      key: MapKey<N>,
33      /// The value statement.
34      value: MapValue<N>,
35  }
36  
37  impl<N: Network> Mapping<N> {
38      /// Initializes a new mapping with the given name, key statement, and value statement.
39      pub fn new(name: Identifier<N>, key: MapKey<N>, value: MapValue<N>) -> Self {
40          Self { name, key, value }
41      }
42  
43      /// Returns the name of the mapping.
44      pub const fn name(&self) -> &Identifier<N> {
45          &self.name
46      }
47  
48      /// Returns the key statement.
49      pub const fn key(&self) -> &MapKey<N> {
50          &self.key
51      }
52  
53      /// Returns the value statement.
54      pub const fn value(&self) -> &MapValue<N> {
55          &self.value
56      }
57  
58      /// Returns `true` if the mapping contains a string type.
59      pub fn contains_string_type(&self) -> bool {
60          self.key.plaintext_type().contains_string_type() || self.value.plaintext_type().contains_string_type()
61      }
62  
63      /// Returns `true` if the mapping contains an array type with a size that exceeds the given maximum.
64      pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
65          self.key.plaintext_type().exceeds_max_array_size(max_array_size)
66              || self.value.plaintext_type().exceeds_max_array_size(max_array_size)
67      }
68  }
69  
70  impl<N: Network> TypeName for Mapping<N> {
71      /// Returns the type name as a string.
72      #[inline]
73      fn type_name() -> &'static str {
74          "mapping"
75      }
76  }