1use std::fmt::Debug;
4
5use anyhow::bail;
6use trustfall::Schema;
7
8macro_rules! add_version_method {
9 () => {
10 pub fn version(&self) -> u32 {
11 match self {
12 #[cfg(feature = "v37")]
13 Self::V37(..) => 37,
14
15 #[cfg(feature = "v39")]
16 Self::V39(..) => 39,
17
18 #[cfg(feature = "v43")]
19 Self::V43(..) => 43,
20
21 #[cfg(feature = "v45")]
22 Self::V45(..) => 45,
23
24 #[cfg(feature = "v53")]
25 Self::V53(..) => 53,
26 }
27 }
28 };
29}
30
31#[non_exhaustive]
32#[derive(Debug)]
33pub enum VersionedStorage {
34 #[cfg(feature = "v37")]
35 V37(trustfall_rustdoc_adapter_v37::PackageStorage),
36
37 #[cfg(feature = "v39")]
38 V39(trustfall_rustdoc_adapter_v39::PackageStorage),
39
40 #[cfg(feature = "v43")]
41 V43(trustfall_rustdoc_adapter_v43::PackageStorage),
42
43 #[cfg(feature = "v45")]
44 V45(trustfall_rustdoc_adapter_v45::PackageStorage),
45
46 #[cfg(feature = "v53")]
47 V53(trustfall_rustdoc_adapter_v53::PackageStorage),
48}
49
50#[non_exhaustive]
51#[derive(Debug)]
52pub enum VersionedIndex<'a> {
53 #[cfg(feature = "v37")]
54 V37(trustfall_rustdoc_adapter_v37::PackageIndex<'a>),
55
56 #[cfg(feature = "v39")]
57 V39(trustfall_rustdoc_adapter_v39::PackageIndex<'a>),
58
59 #[cfg(feature = "v43")]
60 V43(trustfall_rustdoc_adapter_v43::PackageIndex<'a>),
61
62 #[cfg(feature = "v45")]
63 V45(trustfall_rustdoc_adapter_v45::PackageIndex<'a>),
64
65 #[cfg(feature = "v53")]
66 V53(trustfall_rustdoc_adapter_v53::PackageIndex<'a>),
67}
68
69#[non_exhaustive]
70pub enum VersionedRustdocAdapter<'a> {
71 #[cfg(feature = "v37")]
72 V37(Schema, trustfall_rustdoc_adapter_v37::RustdocAdapter<'a>),
73
74 #[cfg(feature = "v39")]
75 V39(Schema, trustfall_rustdoc_adapter_v39::RustdocAdapter<'a>),
76
77 #[cfg(feature = "v43")]
78 V43(Schema, trustfall_rustdoc_adapter_v43::RustdocAdapter<'a>),
79
80 #[cfg(feature = "v45")]
81 V45(Schema, trustfall_rustdoc_adapter_v45::RustdocAdapter<'a>),
82
83 #[cfg(feature = "v53")]
84 V53(Schema, trustfall_rustdoc_adapter_v53::RustdocAdapter<'a>),
85}
86
87impl VersionedStorage {
88 pub fn crate_version(&self) -> Option<&str> {
92 match self {
93 #[cfg(feature = "v37")]
94 VersionedStorage::V37(s) => s.crate_version(),
95
96 #[cfg(feature = "v39")]
97 VersionedStorage::V39(s) => s.crate_version(),
98
99 #[cfg(feature = "v43")]
100 VersionedStorage::V43(s) => s.crate_version(),
101
102 #[cfg(feature = "v45")]
103 VersionedStorage::V45(s) => s.crate_version(),
104
105 #[cfg(feature = "v53")]
106 VersionedStorage::V53(s) => s.crate_version(),
107 }
108 }
109
110 add_version_method!();
111}
112
113impl<'a> VersionedIndex<'a> {
114 pub fn from_storage(storage: &'a VersionedStorage, target_triple: &str) -> Self {
115 match storage {
116 #[cfg(feature = "v37")]
117 VersionedStorage::V37(s) => Self::V37(
118 trustfall_rustdoc_adapter_v37::PackageIndex::from_storage(s, target_triple),
119 ),
120
121 #[cfg(feature = "v39")]
122 VersionedStorage::V39(s) => Self::V39(
123 trustfall_rustdoc_adapter_v39::PackageIndex::from_storage(s, target_triple),
124 ),
125
126 #[cfg(feature = "v43")]
127 VersionedStorage::V43(s) => Self::V43(
128 trustfall_rustdoc_adapter_v43::PackageIndex::from_storage(s, target_triple),
129 ),
130
131 #[cfg(feature = "v45")]
132 VersionedStorage::V45(s) => {
133 Self::V45(trustfall_rustdoc_adapter_v45::PackageIndex::from_storage(s))
134 }
135
136 #[cfg(feature = "v53")]
137 VersionedStorage::V53(s) => {
138 Self::V53(trustfall_rustdoc_adapter_v53::PackageIndex::from_storage(s))
139 }
140 }
141 }
142
143 add_version_method!();
144}
145
146impl<'a> VersionedRustdocAdapter<'a> {
147 pub fn new(
148 current: &'a VersionedIndex<'a>,
149 baseline: Option<&'a VersionedIndex<'a>>,
150 ) -> anyhow::Result<Self> {
151 match (current, baseline) {
152 #[cfg(feature = "v37")]
153 (VersionedIndex::V37(c), Some(VersionedIndex::V37(b))) => {
154 let adapter = trustfall_rustdoc_adapter_v37::RustdocAdapter::new(c, Some(b));
155 Ok(VersionedRustdocAdapter::V37(
156 trustfall_rustdoc_adapter_v37::RustdocAdapter::schema(),
157 adapter,
158 ))
159 }
160
161 #[cfg(feature = "v37")]
162 (VersionedIndex::V37(c), None) => {
163 let adapter = trustfall_rustdoc_adapter_v37::RustdocAdapter::new(c, None);
164 Ok(VersionedRustdocAdapter::V37(
165 trustfall_rustdoc_adapter_v37::RustdocAdapter::schema(),
166 adapter,
167 ))
168 }
169
170 #[cfg(feature = "v39")]
171 (VersionedIndex::V39(c), Some(VersionedIndex::V39(b))) => {
172 let adapter = trustfall_rustdoc_adapter_v39::RustdocAdapter::new(c, Some(b));
173 Ok(VersionedRustdocAdapter::V39(
174 trustfall_rustdoc_adapter_v39::RustdocAdapter::schema(),
175 adapter,
176 ))
177 }
178
179 #[cfg(feature = "v39")]
180 (VersionedIndex::V39(c), None) => {
181 let adapter = trustfall_rustdoc_adapter_v39::RustdocAdapter::new(c, None);
182 Ok(VersionedRustdocAdapter::V39(
183 trustfall_rustdoc_adapter_v39::RustdocAdapter::schema(),
184 adapter,
185 ))
186 }
187
188 #[cfg(feature = "v43")]
189 (VersionedIndex::V43(c), Some(VersionedIndex::V43(b))) => {
190 let adapter = trustfall_rustdoc_adapter_v43::RustdocAdapter::new(c, Some(b));
191 Ok(VersionedRustdocAdapter::V43(
192 trustfall_rustdoc_adapter_v43::RustdocAdapter::schema(),
193 adapter,
194 ))
195 }
196
197 #[cfg(feature = "v43")]
198 (VersionedIndex::V43(c), None) => {
199 let adapter = trustfall_rustdoc_adapter_v43::RustdocAdapter::new(c, None);
200 Ok(VersionedRustdocAdapter::V43(
201 trustfall_rustdoc_adapter_v43::RustdocAdapter::schema(),
202 adapter,
203 ))
204 }
205
206 #[cfg(feature = "v45")]
207 (VersionedIndex::V45(c), Some(VersionedIndex::V45(b))) => {
208 let adapter = trustfall_rustdoc_adapter_v45::RustdocAdapter::new(c, Some(b));
209 Ok(VersionedRustdocAdapter::V45(
210 trustfall_rustdoc_adapter_v45::RustdocAdapter::schema(),
211 adapter,
212 ))
213 }
214
215 #[cfg(feature = "v45")]
216 (VersionedIndex::V45(c), None) => {
217 let adapter = trustfall_rustdoc_adapter_v45::RustdocAdapter::new(c, None);
218 Ok(VersionedRustdocAdapter::V45(
219 trustfall_rustdoc_adapter_v45::RustdocAdapter::schema(),
220 adapter,
221 ))
222 }
223
224 #[cfg(feature = "v53")]
225 (VersionedIndex::V53(c), Some(VersionedIndex::V53(b))) => {
226 let adapter = trustfall_rustdoc_adapter_v53::RustdocAdapter::new(c, Some(b));
227 Ok(VersionedRustdocAdapter::V53(
228 trustfall_rustdoc_adapter_v53::RustdocAdapter::schema(),
229 adapter,
230 ))
231 }
232
233 #[cfg(feature = "v53")]
234 (VersionedIndex::V53(c), None) => {
235 let adapter = trustfall_rustdoc_adapter_v53::RustdocAdapter::new(c, None);
236 Ok(VersionedRustdocAdapter::V53(
237 trustfall_rustdoc_adapter_v53::RustdocAdapter::schema(),
238 adapter,
239 ))
240 }
241
242 #[allow(unreachable_patterns)]
243 (c, Some(b)) => {
244 bail!(
245 "version mismatch between current (v{}) and baseline (v{}) format versions",
246 c.version(),
247 b.version()
248 )
249 }
250 }
251 }
252
253 pub fn schema(&self) -> &Schema {
254 match self {
255 #[cfg(feature = "v37")]
256 VersionedRustdocAdapter::V37(schema, ..) => schema,
257
258 #[cfg(feature = "v39")]
259 VersionedRustdocAdapter::V39(schema, ..) => schema,
260
261 #[cfg(feature = "v43")]
262 VersionedRustdocAdapter::V43(schema, ..) => schema,
263
264 #[cfg(feature = "v45")]
265 VersionedRustdocAdapter::V45(schema, ..) => schema,
266
267 #[cfg(feature = "v53")]
268 VersionedRustdocAdapter::V53(schema, ..) => schema,
269 }
270 }
271
272 add_version_method!();
273}
274
275pub(crate) fn supported_versions() -> &'static [u32] {
276 &[
277 #[cfg(feature = "v37")]
278 37,
279 #[cfg(feature = "v39")]
280 39,
281 #[cfg(feature = "v43")]
282 43,
283 #[cfg(feature = "v45")]
284 45,
285 #[cfg(feature = "v53")]
286 53,
287 ]
288}