snarkvm_synthesizer_program/mapping/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM 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
16mod key;
17use key::*;
18
19mod value;
20use value::*;
21
22mod bytes;
23mod parse;
24
25use console::{network::prelude::*, program::Identifier};
26
27#[derive(Clone, PartialEq, Eq)]
28pub 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
37impl<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 an array type with a size that exceeds the given maximum.
59    pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
60        self.key.plaintext_type().exceeds_max_array_size(max_array_size)
61            || self.value.plaintext_type().exceeds_max_array_size(max_array_size)
62    }
63}
64
65impl<N: Network> TypeName for Mapping<N> {
66    /// Returns the type name as a string.
67    #[inline]
68    fn type_name() -> &'static str {
69        "mapping"
70    }
71}