snarkvm_console_program/data/value/
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 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, 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}
39
40impl<N: Network> From<Literal<N>> for Value<N> {
41    /// Initializes the value from a literal.
42    fn from(literal: Literal<N>) -> Self {
43        Self::Plaintext(Plaintext::from(literal))
44    }
45}
46
47impl<N: Network> From<&Literal<N>> for Value<N> {
48    /// Initializes the value from a literal.
49    fn from(literal: &Literal<N>) -> Self {
50        Self::from(Plaintext::from(literal))
51    }
52}
53
54impl<N: Network> From<Plaintext<N>> for Value<N> {
55    /// Initializes the value from a plaintext.
56    fn from(plaintext: Plaintext<N>) -> Self {
57        Self::Plaintext(plaintext)
58    }
59}
60
61impl<N: Network> From<&Plaintext<N>> for Value<N> {
62    /// Initializes the value from a plaintext.
63    fn from(plaintext: &Plaintext<N>) -> Self {
64        Self::from(plaintext.clone())
65    }
66}
67
68impl<N: Network> From<Record<N, Plaintext<N>>> for Value<N> {
69    /// Initializes the value from a record.
70    fn from(record: Record<N, Plaintext<N>>) -> Self {
71        Self::Record(record)
72    }
73}
74
75impl<N: Network> From<&Record<N, Plaintext<N>>> for Value<N> {
76    /// Initializes the value from a record.
77    fn from(record: &Record<N, Plaintext<N>>) -> Self {
78        Self::from(record.clone())
79    }
80}
81
82impl<N: Network> From<Future<N>> for Value<N> {
83    /// Initializes the value from a future.
84    fn from(future: Future<N>) -> Self {
85        Self::Future(future)
86    }
87}
88
89impl<N: Network> From<&Future<N>> for Value<N> {
90    /// Initializes the value from a future.
91    fn from(future: &Future<N>) -> Self {
92        Self::from(future.clone())
93    }
94}
95
96impl<N: Network> From<Argument<N>> for Value<N> {
97    /// Initializes the value from an argument.
98    fn from(argument: Argument<N>) -> Self {
99        match argument {
100            Argument::Plaintext(plaintext) => Self::Plaintext(plaintext),
101            Argument::Future(future) => Self::Future(future),
102        }
103    }
104}
105
106impl<N: Network> From<&Argument<N>> for Value<N> {
107    /// Initializes the value from an argument.
108    fn from(argument: &Argument<N>) -> Self {
109        Self::from(argument.clone())
110    }
111}
112
113impl<N: Network> From<&Value<N>> for Value<N> {
114    /// Returns a clone of the value.
115    fn from(value: &Value<N>) -> Self {
116        value.clone()
117    }
118}
119
120impl<N: Network> TryFrom<Result<Value<N>>> for Value<N> {
121    type Error = Error;
122
123    /// Initializes a value from a result.
124    fn try_from(value: Result<Value<N>>) -> Result<Self> {
125        value
126    }
127}
128
129impl<N: Network> TryFrom<String> for Value<N> {
130    type Error = Error;
131
132    /// Initializes a value from a string.
133    fn try_from(value: String) -> Result<Self> {
134        Self::from_str(&value)
135    }
136}
137
138impl<N: Network> TryFrom<&String> for Value<N> {
139    type Error = Error;
140
141    /// Initializes a value from a string.
142    fn try_from(value: &String) -> Result<Self> {
143        Self::from_str(value)
144    }
145}
146
147impl<N: Network> TryFrom<&str> for Value<N> {
148    type Error = Error;
149
150    /// Initializes a value from a string.
151    fn try_from(value: &str) -> Result<Self> {
152        Self::from_str(value)
153    }
154}