/ console / types / string / src / lib.rs
lib.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  #![cfg_attr(test, allow(clippy::assertions_on_result_states))]
20  #![warn(clippy::cast_possible_truncation)]
21  
22  mod bitwise;
23  mod bytes;
24  mod parse;
25  mod random;
26  mod serialize;
27  
28  pub use alphavm_console_network_environment::prelude::*;
29  pub use alphavm_console_types_boolean::Boolean;
30  pub use alphavm_console_types_field::Field;
31  pub use alphavm_console_types_integers::Integer;
32  
33  use core::marker::PhantomData;
34  
35  #[derive(Clone, PartialEq, Eq, Hash)]
36  pub struct StringType<E: Environment> {
37      /// The underlying string.
38      string: String,
39      /// PhantomData
40      _phantom: PhantomData<E>,
41  }
42  
43  impl<E: Environment> StringTrait for StringType<E> {}
44  
45  impl<E: Environment> StringType<E> {
46      /// Initializes a new string.
47      pub fn new(string: &str) -> Self {
48          // Ensure the string is within the allowed capacity.
49          let num_bytes = string.len();
50          match num_bytes <= E::MAX_STRING_BYTES as usize {
51              true => Self { string: string.to_string(), _phantom: PhantomData },
52              false => E::halt(format!("Attempted to allocate a string of size {num_bytes}")),
53          }
54      }
55  }
56  
57  impl<E: Environment> TypeName for StringType<E> {
58      /// Returns the type name as a string.
59      #[inline]
60      fn type_name() -> &'static str {
61          "string"
62      }
63  }
64  
65  impl<E: Environment> Deref for StringType<E> {
66      type Target = str;
67  
68      #[inline]
69      fn deref(&self) -> &Self::Target {
70          self.string.as_str()
71      }
72  }