edgefirst_decoder/model_config/
provenance.rs1use std::fmt;
7
8edgefirst_tensor::ef_vocabulary! {
9 #[non_exhaustive]
13 pub enum ConfigSource {
14 EmbeddedEdgefirst = 0, "embedded_edgefirst", EMBEDDED_EDGEFIRST,
16 SidecarEdgefirst = 1, "sidecar_edgefirst", SIDECAR_EDGEFIRST,
18 EmbeddedHf = 2, "embedded_hf", EMBEDDED_HF,
21 SidecarHf = 3, "sidecar_hf", SIDECAR_HF,
24 UltralyticsMetadata = 4, "ultralytics_metadata", ULTRALYTICS_METADATA,
27 Heuristic = 5, "heuristic", HEURISTIC,
29 Default = 6, "default", DEFAULT,
31 Override = 7, "override", OVERRIDE,
33 }
34 #[doc(hidden)]
35 pub mod config_source_code;
36}
37
38edgefirst_tensor::ef_vocabulary! {
39 #[non_exhaustive]
44 pub enum ModelFamily {
45 EdgeFirst = 0, "edgefirst", EDGEFIRST,
47 Ultralytics = 1, "ultralytics", ULTRALYTICS,
49 HuggingFace = 2, "huggingface", HUGGINGFACE,
52 Unknown = 3, "unknown", UNKNOWN,
54 }
55 #[doc(hidden)]
56 pub mod model_family_code;
57}
58
59edgefirst_tensor::ef_vocabulary! {
60 #[non_exhaustive]
64 pub enum ModelTask {
65 Detect = 0, "detect", DETECT,
67 Segment = 1, "segment", SEGMENT,
69 Semantic = 2, "semantic", SEMANTIC,
71 }
72 #[doc(hidden)]
73 pub mod model_task_code;
74}
75
76edgefirst_tensor::ef_vocabulary! {
77 #[derive(PartialOrd, Ord)]
83 #[non_exhaustive]
84 pub enum ConfigField {
85 Normalization = 0, "normalize", NORMALIZATION,
87 Resize = 1, "resize", RESIZE,
89 Placement = 2, "placement", PLACEMENT,
91 Pad = 3, "pad", PAD,
93 Interpolation = 4, "interpolation", INTERPOLATION,
95 Task = 5, "task", TASK,
97 DecodeMode = 6, "decode", DECODE_MODE,
99 Labels = 7, "labels", LABELS,
101 Size = 8, "size", SIZE,
103 }
104 #[doc(hidden)]
105 pub mod config_field_code;
106}
107
108#[derive(Debug, Clone, PartialEq, Eq)]
122#[non_exhaustive]
123pub struct Provenance {
124 pub source: ConfigSource,
126 pub detail: Option<String>,
128}
129
130impl Provenance {
131 pub fn new(source: ConfigSource, detail: Option<&str>) -> Self {
133 Self {
134 source,
135 detail: detail.map(str::to_owned),
136 }
137 }
138}
139
140impl fmt::Display for Provenance {
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 let detail = self.detail.as_deref();
143 match (self.source, detail) {
144 (ConfigSource::Heuristic, Some(d)) => write!(f, "guess: {d}"),
145 (ConfigSource::Heuristic, None) => f.write_str("guess"),
146 (ConfigSource::Default, Some(d)) => write!(f, "default: {d}"),
147 (ConfigSource::Default, None) => f.write_str("default"),
148 (ConfigSource::Override, _) => f.write_str("override"),
149 (ConfigSource::UltralyticsMetadata, _) => f.write_str("ultralytics metadata"),
150 (_, Some(d)) => f.write_str(d),
151 (ConfigSource::EmbeddedEdgefirst, None) => f.write_str("edgefirst.json"),
152 (ConfigSource::SidecarEdgefirst, None) => f.write_str("sidecar edgefirst.json"),
153 (ConfigSource::EmbeddedHf, None) => f.write_str("embedded hugging face config"),
154 (ConfigSource::SidecarHf, None) => f.write_str("sidecar hugging face config"),
155 }
156 }
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 #[test]
164 fn vocabularies_round_trip_codes_and_strings() {
165 for &s in ConfigSource::all() {
166 assert_eq!(ConfigSource::from_code(s.code()), Some(s));
167 assert_eq!(ConfigSource::from_str_code(s.as_str()), Some(s));
168 }
169 for &m in ModelFamily::all() {
170 assert_eq!(ModelFamily::from_str_code(m.as_str()), Some(m));
171 }
172 for &t in ModelTask::all() {
173 assert_eq!(ModelTask::from_str_code(t.as_str()), Some(t));
174 }
175 for (i, &c) in ConfigField::all().iter().enumerate() {
176 assert_eq!(c.code() as usize, i, "fields describe in code order");
177 assert_eq!(ConfigField::from_str_code(c.as_str()), Some(c));
178 }
179 }
180
181 #[test]
182 fn provenance_displays_bracket_text() {
183 let p = |s, d| Provenance::new(s, d).to_string();
184 assert_eq!(
185 p(ConfigSource::SidecarHf, Some("preprocessor_config.json")),
186 "preprocessor_config.json"
187 );
188 assert_eq!(
189 p(ConfigSource::Heuristic, Some("SegFormer default")),
190 "guess: SegFormer default"
191 );
192 assert_eq!(p(ConfigSource::Override, None), "override");
193 assert_eq!(p(ConfigSource::Default, None), "default");
194 assert_eq!(p(ConfigSource::EmbeddedEdgefirst, None), "edgefirst.json");
195 assert_eq!(
196 p(ConfigSource::UltralyticsMetadata, None),
197 "ultralytics metadata"
198 );
199 }
200}