1use alloc::borrow::Cow;
5use alloc::string::ToString;
6use alloc::vec::Vec;
7use core::fmt;
8
9#[non_exhaustive]
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub enum Backend {
13 Cpu,
15 #[non_exhaustive]
17 Gpu {
18 api: &'static str,
20 },
21}
22
23impl Backend {
24 pub const fn gpu(api: &'static str) -> Self {
26 Self::Gpu { api }
27 }
28}
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
33#[non_exhaustive]
34pub struct Implementation {
35 crate_name: &'static str,
37 crate_version: &'static str,
39 score_revision: u32,
41 backend: Backend,
43 build: Option<&'static str>,
46}
47
48impl Implementation {
49 pub const fn new(
51 crate_name: &'static str,
52 crate_version: &'static str,
53 score_revision: u32,
54 ) -> Self {
55 Self {
56 crate_name,
57 crate_version,
58 score_revision,
59 backend: Backend::Cpu,
60 build: None,
61 }
62 }
63
64 pub const fn crate_name(&self) -> &'static str {
66 self.crate_name
67 }
68 pub const fn crate_version(&self) -> &'static str {
70 self.crate_version
71 }
72 pub const fn score_revision(&self) -> u32 {
74 self.score_revision
75 }
76 pub const fn backend(&self) -> Backend {
78 self.backend
79 }
80 pub const fn build(&self) -> Option<&'static str> {
82 self.build
83 }
84 #[must_use]
86 pub const fn with_backend(mut self, backend: Backend) -> Self {
87 self.backend = backend;
88 self
89 }
90
91 #[must_use]
93 pub const fn with_build(mut self, build: Option<&'static str>) -> Self {
94 self.build = build;
95 self
96 }
97}
98
99#[macro_export]
109macro_rules! implementation {
110 ($rev:expr) => {
111 $crate::Implementation::new(
112 ::core::env!("CARGO_PKG_NAME"),
113 ::core::env!("CARGO_PKG_VERSION"),
114 $rev,
115 )
116 .with_build(::core::option_env!("ZENMETRICS_BUILD_ID"))
117 };
118}
119
120#[derive(Clone, Debug, PartialEq, Eq, Hash)]
123#[non_exhaustive]
124pub struct MetricId {
125 family: &'static str,
126 variant: Option<&'static str>,
127 reference: Cow<'static, str>,
128 implementation: Implementation,
129 config: Vec<(Cow<'static, str>, Cow<'static, str>)>,
130}
131
132impl MetricId {
133 pub fn new(family: &'static str, implementation: Implementation) -> Self {
135 Self {
136 family,
137 variant: None,
138 reference: Cow::Borrowed(""),
139 implementation,
140 config: Vec::new(),
141 }
142 }
143
144 #[must_use]
146 pub fn with_variant(mut self, variant: &'static str) -> Self {
147 self.variant = Some(variant);
148 self
149 }
150
151 #[must_use]
154 pub fn with_reference(mut self, reference: impl Into<Cow<'static, str>>) -> Self {
155 self.reference = reference.into();
156 self
157 }
158
159 #[must_use]
161 pub fn with_backend(mut self, backend: Backend) -> Self {
162 self.implementation.backend = backend;
163 self
164 }
165
166 #[must_use]
171 pub fn with_config(
172 mut self,
173 key: impl Into<Cow<'static, str>>,
174 value: impl fmt::Display,
175 ) -> Self {
176 let key = key.into();
177 let value: Cow<'static, str> = Cow::Owned(value.to_string());
178 match self
179 .config
180 .binary_search_by(|(k, _)| k.as_ref().cmp(key.as_ref()))
181 {
182 Ok(i) => self.config[i].1 = value,
183 Err(i) => self.config.insert(i, (key, value)),
184 }
185 self
186 }
187
188 pub fn family(&self) -> &'static str {
190 self.family
191 }
192
193 pub fn variant(&self) -> Option<&'static str> {
195 self.variant
196 }
197
198 pub fn reference(&self) -> &str {
200 &self.reference
201 }
202
203 pub fn implementation(&self) -> &Implementation {
205 &self.implementation
206 }
207
208 pub fn config(&self) -> &[(Cow<'static, str>, Cow<'static, str>)] {
210 &self.config
211 }
212
213 pub fn config_value(&self, key: &str) -> Option<&str> {
215 self.config
216 .binary_search_by(|(k, _)| k.as_ref().cmp(key))
217 .ok()
218 .map(|i| self.config[i].1.as_ref())
219 }
220}
221
222impl fmt::Display for MetricId {
223 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 f.write_str(self.family)?;
226 if let Some(v) = self.variant {
227 write!(f, "/{v}")?;
228 }
229 let imp = &self.implementation;
230 write!(
231 f,
232 " ({}@{} r{} {:?})",
233 imp.crate_name, imp.crate_version, imp.score_revision, imp.backend
234 )
235 }
236}