Skip to main content

qubit_validator/registry/
registration_source.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//! Linked registration source locations.
10
11/// Source location for one linked registration.
12///
13/// # Examples
14///
15/// ```
16/// use qubit_validator::RegistrationSource;
17///
18/// let source = RegistrationSource::new("rules", "rules::text", "src/text.rs", 12);
19/// assert_eq!(source.crate_name(), "rules");
20/// assert_eq!(source.line(), 12);
21/// ```
22#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
23pub struct RegistrationSource {
24    /// Cargo package which declared the registration.
25    crate_name: &'static str,
26    /// Rust module path which declared the registration.
27    module_path: &'static str,
28    /// Source file recorded at compile time.
29    file: &'static str,
30    /// One-based source line recorded at compile time.
31    line: u32,
32}
33
34impl RegistrationSource {
35    /// Creates the source location of a validator registration.
36    ///
37    /// `crate_name`, `module_path`, and `file` identify the declaring crate,
38    /// module, and source file, while `line` is the one-based source line.
39    /// All text fields must live for the duration of the program. This
40    /// constructor only stores the supplied metadata and does not access the
41    /// file system. It can be used for local registrations as well as the
42    /// registrations generated by `register_validator!`.
43    ///
44    /// # Parameters
45    ///
46    /// * `crate_name` - Cargo package which declared the registration.
47    /// * `module_path` - Rust module path which declared the registration.
48    /// * `file` - Source file recorded at compile time.
49    /// * `line` - One-based source line recorded at compile time.
50    ///
51    /// # Returns
52    ///
53    /// Returns the source location containing the supplied metadata.
54    #[must_use]
55    #[inline]
56    pub const fn new(crate_name: &'static str, module_path: &'static str, file: &'static str, line: u32) -> Self {
57        Self {
58            crate_name,
59            module_path,
60            file,
61            line,
62        }
63    }
64
65    /// Returns the declaring crate name.
66    ///
67    /// # Returns
68    ///
69    /// Returns the Cargo package name stored in this source location.
70    #[must_use]
71    #[inline]
72    pub const fn crate_name(&self) -> &'static str {
73        self.crate_name
74    }
75
76    /// Returns the declaring module path.
77    ///
78    /// # Returns
79    ///
80    /// Returns the Rust module path stored in this source location.
81    #[must_use]
82    #[inline]
83    pub const fn module_path(&self) -> &'static str {
84        self.module_path
85    }
86
87    /// Returns the source file path.
88    ///
89    /// # Returns
90    ///
91    /// Returns the compile-time source file path stored in this location.
92    #[must_use]
93    #[inline]
94    pub const fn file(&self) -> &'static str {
95        self.file
96    }
97
98    /// Returns the one-based source line.
99    ///
100    /// # Returns
101    ///
102    /// Returns the compile-time line number stored in this location.
103    #[must_use]
104    #[inline]
105    pub const fn line(&self) -> u32 {
106        self.line
107    }
108}