Skip to main content

snarkvm_console_program/data/value/
mod.rs

1// Copyright (c) 2019-2026 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 bytes;
17mod equal;
18mod find;
19mod parse;
20mod serialize;
21mod to_bits;
22mod to_bits_raw;
23mod to_fields;
24mod to_fields_raw;
25
26use crate::{Access, Argument, DynamicFuture, DynamicRecord, Entry, Future, Literal, Plaintext, Record};
27use snarkvm_console_network::Network;
28use snarkvm_console_types::prelude::*;
29
30#[derive(Clone)]
31pub enum Value<N: Network> {
32    /// A plaintext value.
33    Plaintext(Plaintext<N>),
34    /// A record value.
35    Record(Record<N, Plaintext<N>>),
36    /// A future.
37    Future(Future<N>),
38    /// A dynamic record.
39    DynamicRecord(DynamicRecord<N>),
40    /// A dynamic future.
41    DynamicFuture(DynamicFuture<N>),
42}
43
44impl<N: Network> From<Literal<N>> for Value<N> {
45    /// Initializes the value from a literal.
46    fn from(literal: Literal<N>) -> Self {
47        Self::Plaintext(Plaintext::from(literal))
48    }
49}
50
51impl<N: Network> From<&Literal<N>> for Value<N> {
52    /// Initializes the value from a literal.
53    fn from(literal: &Literal<N>) -> Self {
54        Self::from(Plaintext::from(literal))
55    }
56}
57
58impl<N: Network> From<Plaintext<N>> for Value<N> {
59    /// Initializes the value from a plaintext.
60    fn from(plaintext: Plaintext<N>) -> Self {
61        Self::Plaintext(plaintext)
62    }
63}
64
65impl<N: Network> From<&Plaintext<N>> for Value<N> {
66    /// Initializes the value from a plaintext.
67    fn from(plaintext: &Plaintext<N>) -> Self {
68        Self::from(plaintext.clone())
69    }
70}
71
72impl<N: Network> From<Record<N, Plaintext<N>>> for Value<N> {
73    /// Initializes the value from a record.
74    fn from(record: Record<N, Plaintext<N>>) -> Self {
75        Self::Record(record)
76    }
77}
78
79impl<N: Network> From<&Record<N, Plaintext<N>>> for Value<N> {
80    /// Initializes the value from a record.
81    fn from(record: &Record<N, Plaintext<N>>) -> Self {
82        Self::from(record.clone())
83    }
84}
85
86impl<N: Network> From<Future<N>> for Value<N> {
87    /// Initializes the value from a future.
88    fn from(future: Future<N>) -> Self {
89        Self::Future(future)
90    }
91}
92
93impl<N: Network> From<&Future<N>> for Value<N> {
94    /// Initializes the value from a future.
95    fn from(future: &Future<N>) -> Self {
96        Self::from(future.clone())
97    }
98}
99
100impl<N: Network> From<DynamicRecord<N>> for Value<N> {
101    /// Initializes the value from a dynamic record.
102    fn from(dynamic_record: DynamicRecord<N>) -> Self {
103        Self::DynamicRecord(dynamic_record)
104    }
105}
106
107impl<N: Network> From<&DynamicRecord<N>> for Value<N> {
108    /// Initializes the value from a dynamic record.
109    fn from(dynamic_record: &DynamicRecord<N>) -> Self {
110        Self::from(dynamic_record.clone())
111    }
112}
113
114impl<N: Network> From<DynamicFuture<N>> for Value<N> {
115    /// Initializes the value from a dynamic future.
116    fn from(dynamic_future: DynamicFuture<N>) -> Self {
117        Self::DynamicFuture(dynamic_future)
118    }
119}
120
121impl<N: Network> From<&DynamicFuture<N>> for Value<N> {
122    /// Initializes the value from a dynamic future.
123    fn from(dynamic_future: &DynamicFuture<N>) -> Self {
124        Self::from(dynamic_future.clone())
125    }
126}
127
128impl<N: Network> From<Argument<N>> for Value<N> {
129    /// Initializes the value from an argument.
130    fn from(argument: Argument<N>) -> Self {
131        match argument {
132            Argument::Plaintext(plaintext) => Self::Plaintext(plaintext),
133            Argument::Future(future) => Self::Future(future),
134            Argument::DynamicFuture(future) => Self::DynamicFuture(future),
135        }
136    }
137}
138
139impl<N: Network> From<&Argument<N>> for Value<N> {
140    /// Initializes the value from an argument.
141    fn from(argument: &Argument<N>) -> Self {
142        Self::from(argument.clone())
143    }
144}
145
146impl<N: Network> From<&Value<N>> for Value<N> {
147    /// Returns a clone of the value.
148    fn from(value: &Value<N>) -> Self {
149        value.clone()
150    }
151}
152
153impl<N: Network> TryFrom<Result<Value<N>>> for Value<N> {
154    type Error = Error;
155
156    /// Initializes a value from a result.
157    fn try_from(value: Result<Value<N>>) -> Result<Self> {
158        value
159    }
160}
161
162impl<N: Network> TryFrom<String> for Value<N> {
163    type Error = Error;
164
165    /// Initializes a value from a string.
166    fn try_from(value: String) -> Result<Self> {
167        Self::from_str(&value)
168    }
169}
170
171impl<N: Network> TryFrom<&String> for Value<N> {
172    type Error = Error;
173
174    /// Initializes a value from a string.
175    fn try_from(value: &String) -> Result<Self> {
176        Self::from_str(value)
177    }
178}
179
180impl<N: Network> TryFrom<&str> for Value<N> {
181    type Error = Error;
182
183    /// Initializes a value from a string.
184    fn try_from(value: &str) -> Result<Self> {
185        Self::from_str(value)
186    }
187}