qubit_config/from/config_parse_context.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10
11use crate::ConfigResult;
12use crate::options::ConfigReadOptions;
13
14/// Context passed to [`crate::from::FromConfig`] implementations.
15///
16pub struct ConfigParseContext<'a> {
17 /// The root-relative configuration key.
18 key: &'a str,
19 /// The read options used for this parse operation.
20 options: &'a ConfigReadOptions,
21 /// The substitution function used for this parse operation.
22 substitute: &'a dyn Fn(&str) -> ConfigResult<String>,
23}
24
25impl<'a> ConfigParseContext<'a> {
26 /// Creates a parsing context.
27 ///
28 /// # Parameters
29 ///
30 /// * `key` - The root-relative configuration key.
31 /// * `options` - The read options used for this parse operation.
32 /// * `substitute` - The substitution function used for this parse operation.
33 ///
34 /// # Returns
35 ///
36 /// A new parsing context.
37 pub(crate) fn new(
38 key: &'a str,
39 options: &'a ConfigReadOptions,
40 substitute: &'a dyn Fn(&str) -> ConfigResult<String>,
41 ) -> Self {
42 Self {
43 key,
44 options,
45 substitute,
46 }
47 }
48
49 /// Gets the key being parsed.
50 ///
51 /// # Returns
52 ///
53 /// The root-relative configuration key.
54 #[inline]
55 pub fn key(&self) -> &str {
56 self.key
57 }
58
59 /// Gets the read options used for this parse operation.
60 ///
61 /// # Returns
62 ///
63 /// Read options selected by the field or reader.
64 #[inline]
65 pub fn options(&self) -> &ConfigReadOptions {
66 self.options
67 }
68
69 /// Applies variable substitution to a string value.
70 ///
71 /// # Parameters
72 ///
73 /// * `value` - The string value to substitute.
74 ///
75 /// # Returns
76 ///
77 /// The substituted string value.
78 pub(crate) fn substitute_string(&self, value: &str) -> ConfigResult<String> {
79 (self.substitute)(value)
80 }
81}