qubit_argument/argument/argument_result_ext.rs
1// =============================================================================
2// Copyright (c) 2025 - 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Nested path propagation for argument validation results.
9
10use crate::argument::{
11 ArgumentResult,
12 sealed::Sealed,
13};
14
15/// Adds nested argument-path context to validation results.
16///
17/// Successful results are returned unchanged and do not allocate a path.
18/// Failed results consume their [`crate::ArgumentError`] and prepend the
19/// supplied parent path while preserving the structured failure kind.
20///
21/// The trait is sealed and implemented only for [`ArgumentResult`].
22pub trait ArgumentResultExt<T>: Sealed + Sized {
23 /// Prepends a parent path to a validation failure.
24 ///
25 /// # Parameters
26 ///
27 /// - `prefix`: The parent path to add before an error's current path.
28 ///
29 /// # Returns
30 ///
31 /// The unchanged successful value, or the original validation error with
32 /// its path prefixed.
33 fn with_path_prefix(self, prefix: &str) -> ArgumentResult<T>;
34}
35
36impl<T> ArgumentResultExt<T> for ArgumentResult<T> {
37 /// Prefixes only the error branch and leaves success values untouched.
38 #[inline]
39 fn with_path_prefix(self, prefix: &str) -> ArgumentResult<T> {
40 self.map_err(|error| error.with_path_prefix(prefix))
41 }
42}