From 70ccb1befae67bac1bab1c7c88bb4732c8483ea3 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 25 Mar 2026 17:42:17 -0300 Subject: [PATCH] add constructors from ranges --- nssa/core/src/program.rs | 46 +++++++++++++++++++ .../guest/src/bin/validity_window.rs | 9 ++-- .../src/bin/validity_window_chain_caller.rs | 4 +- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/nssa/core/src/program.rs b/nssa/core/src/program.rs index 19ed443d..f4726701 100644 --- a/nssa/core/src/program.rs +++ b/nssa/core/src/program.rs @@ -323,6 +323,23 @@ impl ProgramOutput { self.validity_window.set_to(id)?; Ok(self) } + + /// Sets the validity window from an infallible range conversion (`1..`, `..5`, `..`). + #[must_use] + pub fn with_validity_window>(mut self, window: W) -> Self { + self.validity_window = window.into(); + self + } + + /// Sets the validity window from a fallible range conversion (`1..5`). + /// Returns `Err` if the range is empty. + pub fn try_with_validity_window>( + mut self, + window: W, + ) -> Result { + self.validity_window = window.try_into()?; + Ok(self) + } } /// Representation of a number as `lo + hi * 2^128`. @@ -571,6 +588,35 @@ mod tests { assert_eq!(w.to(), None); } + #[test] + fn program_output_try_with_validity_window_range() { + let output = ProgramOutput::new(vec![], vec![], vec![]) + .try_with_validity_window(10_u64..100) + .unwrap(); + assert_eq!(output.validity_window.from(), Some(10)); + assert_eq!(output.validity_window.to(), Some(100)); + } + + #[test] + fn program_output_with_validity_window_range_from() { + let output = ProgramOutput::new(vec![], vec![], vec![]).with_validity_window(10_u64..); + assert_eq!(output.validity_window.from(), Some(10)); + assert_eq!(output.validity_window.to(), None); + } + + #[test] + fn program_output_with_validity_window_range_to() { + let output = ProgramOutput::new(vec![], vec![], vec![]).with_validity_window(..100_u64); + assert_eq!(output.validity_window.from(), None); + assert_eq!(output.validity_window.to(), Some(100)); + } + + #[test] + fn program_output_try_with_validity_window_empty_range_fails() { + let result = ProgramOutput::new(vec![], vec![], vec![]).try_with_validity_window(5_u64..5); + assert!(result.is_err()); + } + #[test] fn post_state_new_with_claim_constructor() { let account = Account { diff --git a/test_program_methods/guest/src/bin/validity_window.rs b/test_program_methods/guest/src/bin/validity_window.rs index 03f31073..baf36ac9 100644 --- a/test_program_methods/guest/src/bin/validity_window.rs +++ b/test_program_methods/guest/src/bin/validity_window.rs @@ -19,15 +19,12 @@ fn main() { let post = pre.account.clone(); - let output = ProgramOutput::new( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new(post)], ) - .valid_from_id(from_id) + .try_with_validity_window((from_id, until_id)) .unwrap() - .valid_until_id(until_id) - .unwrap(); - - output.write(); + .write(); } diff --git a/test_program_methods/guest/src/bin/validity_window_chain_caller.rs b/test_program_methods/guest/src/bin/validity_window_chain_caller.rs index dfdb8dfe..cf3a3d03 100644 --- a/test_program_methods/guest/src/bin/validity_window_chain_caller.rs +++ b/test_program_methods/guest/src/bin/validity_window_chain_caller.rs @@ -44,9 +44,7 @@ fn main() { vec![pre], vec![AccountPostState::new(post)], ) - .valid_from_id(from_id) - .unwrap() - .valid_until_id(until_id) + .try_with_validity_window((from_id, until_id)) .unwrap() .with_chained_calls(vec![chained_call]) .write();