Skip to main content

qubit_codec/value/
value_codec_registration_source.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! Value-codec registration source locations.
10
11/// Source location for one linked value-codec registration.
12///
13/// The four parts are captured at compile time by the registration macros from
14/// `CARGO_PKG_NAME`, `module_path!`, `file!`, and `line!`, so they point at the
15/// declaration site rather than at the call site that later looks the
16/// registration up. Ordering compares the source fields in derived order:
17/// crate name, module path, file, then line. Duplicate reports therefore have
18/// stable source-field ordering independent of registration iteration order.
19///
20/// # Examples
21///
22/// ```
23/// use qubit_codec::ValueCodecRegistrationSource;
24///
25/// let source = ValueCodecRegistrationSource::new(
26///     "qubit-codec",
27///     "my_crate::codecs",
28///     "src/codec.rs",
29///     42,
30/// );
31/// assert_eq!(source.crate_name(), "qubit-codec");
32/// assert_eq!(source.line(), 42);
33/// ```
34#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
35pub struct ValueCodecRegistrationSource {
36    /// Package name captured from `CARGO_PKG_NAME` at the declaration site.
37    crate_name: &'static str,
38    /// Module path captured from `module_path!` at the declaration site.
39    module_path: &'static str,
40    /// Source file captured from `file!` at the declaration site.
41    file: &'static str,
42    /// One-based line captured from `line!` at the declaration site.
43    line: u32,
44}
45
46impl ValueCodecRegistrationSource {
47    /// Creates a source location captured by a registration macro.
48    ///
49    /// # Parameters
50    ///
51    /// - `crate_name`: Package name captured from `CARGO_PKG_NAME`.
52    /// - `module_path`: Module path captured from `module_path!`.
53    /// - `file`: Source file captured from `file!`.
54    /// - `line`: One-based line captured from `line!`.
55    ///
56    /// # Returns
57    ///
58    /// Returns a location that borrows all four captured parts statically.
59    ///
60    /// Prefer the registration macros over calling this directly.
61    #[doc(hidden)]
62    #[inline]
63    #[must_use]
64    pub const fn new(crate_name: &'static str, module_path: &'static str, file: &'static str, line: u32) -> Self {
65        Self {
66            crate_name,
67            module_path,
68            file,
69            line,
70        }
71    }
72
73    /// Returns the declaring crate name.
74    ///
75    /// # Returns
76    ///
77    /// Returns the `CARGO_PKG_NAME` of the crate that declared the codec.
78    #[must_use]
79    #[inline]
80    pub const fn crate_name(&self) -> &'static str {
81        self.crate_name
82    }
83
84    /// Returns the declaring module path.
85    ///
86    /// # Returns
87    ///
88    /// Returns the `module_path!` value of the declaring module.
89    #[must_use]
90    #[inline]
91    pub const fn module_path(&self) -> &'static str {
92        self.module_path
93    }
94
95    /// Returns the source file path.
96    ///
97    /// # Returns
98    ///
99    /// Returns the `file!` value of the declaring file.
100    #[must_use]
101    #[inline]
102    pub const fn file(&self) -> &'static str {
103        self.file
104    }
105
106    /// Returns the one-based source line.
107    ///
108    /// # Returns
109    ///
110    /// Returns the `line!` value of the declaring macro invocation.
111    #[must_use]
112    #[inline]
113    pub const fn line(&self) -> u32 {
114        self.line
115    }
116}