qubit_validator/argument/named_validation_argument.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
9//! Named validator parameter values.
10
11use std::fmt;
12
13use crate::ValidationArgument;
14
15/// One named validator parameter.
16///
17/// # Type Parameters
18///
19/// - `'a`: Lifetime of the borrowed parameter name and value.
20///
21/// # Examples
22///
23/// ```
24/// use qubit_validator::{NamedValidationArgument, ValidationArgument};
25///
26/// let argument = NamedValidationArgument::new(
27/// "minimum",
28/// ValidationArgument::Unsigned(3),
29/// );
30/// assert_eq!(argument.name(), "minimum");
31/// ```
32#[derive(Clone, Copy, Eq, PartialEq)]
33pub struct NamedValidationArgument<'a> {
34 /// Name used by a validator's parameter schema.
35 name: &'a str,
36 /// Typed value supplied for the named parameter.
37 value: ValidationArgument<'a>,
38}
39
40impl<'a> NamedValidationArgument<'a> {
41 /// Creates a named parameter.
42 ///
43 /// # Parameters
44 ///
45 /// - `name`: Non-empty name declared by the validator's parameter schema.
46 /// - `value`: Typed value supplied for that parameter.
47 ///
48 /// # Returns
49 ///
50 /// A named parameter retaining the supplied borrowed data.
51 ///
52 /// # Panics
53 ///
54 /// Panics when `name` is empty.
55 #[must_use]
56 #[inline]
57 pub const fn new(name: &'a str, value: ValidationArgument<'a>) -> Self {
58 assert!(!name.is_empty(), "validator parameter name cannot be empty");
59 Self { name, value }
60 }
61
62 /// Returns the parameter name.
63 ///
64 /// # Returns
65 ///
66 /// The name borrowed from this argument.
67 #[must_use]
68 #[inline]
69 pub const fn name(&self) -> &'a str {
70 self.name
71 }
72
73 /// Returns the parameter value.
74 ///
75 /// # Returns
76 ///
77 /// The copyable typed value, which may contain borrowed data.
78 #[must_use]
79 #[inline]
80 pub const fn value(&self) -> ValidationArgument<'a> {
81 self.value
82 }
83}
84
85impl fmt::Debug for NamedValidationArgument<'_> {
86 /// Omits the parameter name and relies on the value's redacted formatter.
87 ///
88 /// # Parameters
89 ///
90 /// - `formatter`: Destination that receives the redacted debug
91 /// representation.
92 ///
93 /// # Returns
94 ///
95 /// `Ok(())` when formatting succeeds, or the formatter's error otherwise.
96 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
97 formatter
98 .debug_struct("NamedValidationArgument")
99 .field("value", &self.value)
100 .finish()
101 }
102}