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 follows declaration order, which makes duplicate
17/// reports deterministic.
18///
19/// # Examples
20///
21/// ```
22/// use qubit_codec::ValueCodecRegistrationSource;
23///
24/// let source = ValueCodecRegistrationSource::new(
25///     "qubit-codec",
26///     "my_crate::codecs",
27///     "src/codec.rs",
28///     42,
29/// );
30/// assert_eq!(source.crate_name(), "qubit-codec");
31/// assert_eq!(source.line(), 42);
32/// ```
33#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
34pub struct ValueCodecRegistrationSource {
35    /// Package name captured from `CARGO_PKG_NAME` at the declaration site.
36    crate_name: &'static str,
37    /// Module path captured from `module_path!` at the declaration site.
38    module_path: &'static str,
39    /// Source file captured from `file!` at the declaration site.
40    file: &'static str,
41    /// One-based line captured from `line!` at the declaration site.
42    line: u32,
43}
44
45impl ValueCodecRegistrationSource {
46    /// Creates a source location captured by a registration macro.
47    ///
48    /// # Parameters
49    ///
50    /// - `crate_name`: Package name captured from `CARGO_PKG_NAME`.
51    /// - `module_path`: Module path captured from `module_path!`.
52    /// - `file`: Source file captured from `file!`.
53    /// - `line`: One-based line captured from `line!`.
54    ///
55    /// # Returns
56    ///
57    /// Returns a location that borrows all four captured parts statically.
58    ///
59    /// Prefer the registration macros over calling this directly.
60    #[doc(hidden)]
61    #[inline]
62    #[must_use]
63    pub const fn new(crate_name: &'static str, module_path: &'static str, file: &'static str, line: u32) -> Self {
64        Self {
65            crate_name,
66            module_path,
67            file,
68            line,
69        }
70    }
71
72    /// Returns the declaring crate name.
73    ///
74    /// # Returns
75    ///
76    /// Returns the `CARGO_PKG_NAME` of the crate that declared the codec.
77    #[inline]
78    #[must_use]
79    pub const fn crate_name(&self) -> &'static str {
80        self.crate_name
81    }
82
83    /// Returns the declaring module path.
84    ///
85    /// # Returns
86    ///
87    /// Returns the `module_path!` value of the declaring module.
88    #[inline]
89    #[must_use]
90    pub const fn module_path(&self) -> &'static str {
91        self.module_path
92    }
93
94    /// Returns the source file path.
95    ///
96    /// # Returns
97    ///
98    /// Returns the `file!` value of the declaring file.
99    #[inline]
100    #[must_use]
101    pub const fn file(&self) -> &'static str {
102        self.file
103    }
104
105    /// Returns the one-based source line.
106    ///
107    /// # Returns
108    ///
109    /// Returns the `line!` value of the declaring macro invocation.
110    #[inline]
111    #[must_use]
112    pub const fn line(&self) -> u32 {
113        self.line
114    }
115}