mod.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the deltavm 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  
 16  mod bytes;
 17  mod parse;
 18  
 19  use console::{
 20      network::prelude::*,
 21      program::{FinalizeType, Register},
 22  };
 23  
 24  /// An input statement defines an input argument to finalize, and is of the form
 25  /// `input {register} as {finalize_type}`.
 26  #[derive(Clone, PartialEq, Eq, Hash)]
 27  pub struct Input<N: Network> {
 28      /// The input register.
 29      register: Register<N>,
 30      /// The input finalize type.
 31      finalize_type: FinalizeType<N>,
 32  }
 33  
 34  impl<N: Network> Input<N> {
 35      /// Returns the input register.
 36      #[inline]
 37      pub const fn register(&self) -> &Register<N> {
 38          &self.register
 39      }
 40  
 41      /// Returns the input finalize type.
 42      #[inline]
 43      pub const fn finalize_type(&self) -> &FinalizeType<N> {
 44          &self.finalize_type
 45      }
 46  }
 47  
 48  impl<N: Network> TypeName for Input<N> {
 49      /// Returns the type name as a string.
 50      #[inline]
 51      fn type_name() -> &'static str {
 52          "input"
 53      }
 54  }
 55  
 56  impl<N: Network> Ord for Input<N> {
 57      /// Ordering is determined by the register (the finalize type is ignored).
 58      fn cmp(&self, other: &Self) -> Ordering {
 59          self.register().cmp(other.register())
 60      }
 61  }
 62  
 63  impl<N: Network> PartialOrd for Input<N> {
 64      /// Ordering is determined by the register (the finalize type is ignored).
 65      fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
 66          Some(self.cmp(other))
 67      }
 68  }
 69  
 70  #[cfg(test)]
 71  mod tests {
 72      use super::*;
 73      use console::network::MainnetV0;
 74  
 75      type CurrentNetwork = MainnetV0;
 76  
 77      #[test]
 78      fn test_input_type_name() -> Result<()> {
 79          assert_eq!(Input::<CurrentNetwork>::type_name(), "input");
 80          Ok(())
 81      }
 82  
 83      #[test]
 84      fn test_input_partial_ord() -> Result<()> {
 85          let input1 = Input::<CurrentNetwork>::from_str("input r0 as field.public;")?;
 86          let input2 = Input::<CurrentNetwork>::from_str("input r1 as field.public;")?;
 87  
 88          let input3 = Input::<CurrentNetwork>::from_str("input r0 as signature.public;")?;
 89          let input4 = Input::<CurrentNetwork>::from_str("input r1 as signature.public;")?;
 90  
 91          assert_eq!(input1.partial_cmp(&input1), Some(Ordering::Equal));
 92          assert_eq!(input1.partial_cmp(&input2), Some(Ordering::Less));
 93          assert_eq!(input1.partial_cmp(&input3), Some(Ordering::Equal));
 94          assert_eq!(input1.partial_cmp(&input4), Some(Ordering::Less));
 95  
 96          assert_eq!(input2.partial_cmp(&input1), Some(Ordering::Greater));
 97          assert_eq!(input2.partial_cmp(&input2), Some(Ordering::Equal));
 98          assert_eq!(input2.partial_cmp(&input3), Some(Ordering::Greater));
 99          assert_eq!(input2.partial_cmp(&input4), Some(Ordering::Equal));
100  
101          assert_eq!(input3.partial_cmp(&input1), Some(Ordering::Equal));
102          assert_eq!(input3.partial_cmp(&input2), Some(Ordering::Less));
103          assert_eq!(input3.partial_cmp(&input3), Some(Ordering::Equal));
104          assert_eq!(input3.partial_cmp(&input4), Some(Ordering::Less));
105  
106          assert_eq!(input4.partial_cmp(&input1), Some(Ordering::Greater));
107          assert_eq!(input4.partial_cmp(&input2), Some(Ordering::Equal));
108          assert_eq!(input4.partial_cmp(&input3), Some(Ordering::Greater));
109          assert_eq!(input4.partial_cmp(&input4), Some(Ordering::Equal));
110  
111          Ok(())
112      }
113  }