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