Skip to content

Commit 3f03acc

Browse files
authored
[meta] more minor cleanups (#81)
1 parent cdbc088 commit 3f03acc

5 files changed

Lines changed: 113 additions & 188 deletions

File tree

crates/dropshot-api-manager-types/src/validation.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ impl LockstepApiSpecFileName {
134134

135135
impl fmt::Display for LockstepApiSpecFileName {
136136
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137-
f.write_str(self.path().as_str())
137+
// For lockstep files, path == basename (no directory prefix).
138+
f.write_str(&self.basename())
138139
}
139140
}
140141

@@ -203,7 +204,12 @@ impl VersionedApiSpecFileName {
203204

204205
/// Returns the base name of this file path.
205206
pub fn basename(&self) -> String {
206-
match self.kind {
207+
self.basename_for_kind(self.kind)
208+
}
209+
210+
/// Returns the base name for a specific storage kind.
211+
fn basename_for_kind(&self, kind: VersionedApiSpecKind) -> String {
212+
match kind {
207213
VersionedApiSpecKind::Json => {
208214
format!("{}-{}-{}.json", self.ident, self.version, self.hash)
209215
}
@@ -245,21 +251,22 @@ impl VersionedApiSpecFileName {
245251
/// - If already a Git stub, returns `basename()` directly.
246252
/// - If JSON, returns `basename() + ".gitstub"`.
247253
pub fn git_stub_basename(&self) -> String {
248-
self.to_git_stub().basename()
254+
self.basename_for_kind(VersionedApiSpecKind::GitStub)
249255
}
250256

251257
/// Returns the basename as a JSON filename.
252258
///
253259
/// - If already JSON, returns `basename()` directly.
254260
/// - If Git stub, returns the basename without `.gitstub`.
255261
pub fn json_basename(&self) -> String {
256-
self.to_json().basename()
262+
self.basename_for_kind(VersionedApiSpecKind::Json)
257263
}
258264
}
259265

260266
impl fmt::Display for VersionedApiSpecFileName {
261267
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
262-
f.write_str(self.path().as_str())
268+
// path = "{ident}/{basename}".
269+
write!(f, "{}/{}", self.ident, self.basename())
263270
}
264271
}
265272

@@ -288,7 +295,10 @@ pub enum ApiSpecFileName {
288295

289296
impl fmt::Display for ApiSpecFileName {
290297
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291-
f.write_str(self.path().as_str())
298+
match self {
299+
ApiSpecFileName::Lockstep(l) => fmt::Display::fmt(l, f),
300+
ApiSpecFileName::Versioned(v) => fmt::Display::fmt(v, f),
301+
}
292302
}
293303
}
294304

crates/dropshot-api-manager-types/src/versions.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -294,28 +294,10 @@ macro_rules! api_versions {
294294
)*
295295
$(,)?
296296
] ) => {
297-
dropshot_api_manager_types::paste! {
298-
pub const [<VERSION_ $latest_name>]: $crate::semver::Version =
299-
$crate::semver::Version::new($latest_major, 0, 0);
300-
301-
$(
302-
pub const [<VERSION_ $name>]: $crate::semver::Version =
303-
$crate::semver::Version::new($major, 0, 0);
304-
)*
305-
306-
pub fn supported_versions() -> $crate::SupportedVersions {
307-
let mut literal_versions = vec![
308-
$crate::SupportedVersion::new([<VERSION_ $latest_name>], stringify!($latest_name)),
309-
$( $crate::SupportedVersion::new([<VERSION_ $name>], stringify!($name)) ),*
310-
];
311-
literal_versions.reverse();
312-
$crate::SupportedVersions::new(literal_versions)
313-
}
314-
315-
pub const fn latest_version() -> $crate::semver::Version {
316-
[<VERSION_ $latest_name>]
317-
}
318-
}
297+
$crate::api_versions_picky!([
298+
($latest_major, 0, 0, $latest_name)
299+
$(, ($major, 0, 0, $name))*
300+
]);
319301
};
320302
}
321303

crates/dropshot-api-manager/src/resolved.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,17 +1712,6 @@ fn resolve_api_version_blessed<'a>(
17121712
git_stub: None,
17131713
});
17141714
}
1715-
1716-
// Report non-matching local files as extra.
1717-
problems.extend(non_matching.into_iter().map(|s| {
1718-
Problem::BlessedVersionExtraLocalSpec {
1719-
spec_file_name: s
1720-
.spec_file_name()
1721-
.as_versioned()
1722-
.expect("blessed extra spec is versioned")
1723-
.clone(),
1724-
}
1725-
}));
17261715
} else if !use_git_stub_storage || is_latest {
17271716
// Fast path: Git stub storage disabled or this is the latest version.
17281717
// We know we always want JSON in this case, so we can avoid computing
@@ -1756,16 +1745,6 @@ fn resolve_api_version_blessed<'a>(
17561745
.push(Problem::GitStubShouldBeJson { local_file, blessed });
17571746
}
17581747
}
1759-
1760-
problems.extend(non_matching.into_iter().map(|s| {
1761-
Problem::BlessedVersionExtraLocalSpec {
1762-
spec_file_name: s
1763-
.spec_file_name()
1764-
.as_versioned()
1765-
.expect("blessed extra spec is versioned")
1766-
.clone(),
1767-
}
1768-
}));
17691748
} else {
17701749
// Slow path: Git stub storage enabled and not latest. Compute what
17711750
// storage format this version should use.
@@ -1876,18 +1855,19 @@ fn resolve_api_version_blessed<'a>(
18761855
}
18771856
}
18781857
}
1879-
1880-
problems.extend(non_matching.into_iter().map(|s| {
1881-
Problem::BlessedVersionExtraLocalSpec {
1882-
spec_file_name: s
1883-
.spec_file_name()
1884-
.as_versioned()
1885-
.expect("blessed extra spec is versioned")
1886-
.clone(),
1887-
}
1888-
}));
18891858
}
18901859

1860+
// Report non-matching local files as extra.
1861+
problems.extend(non_matching.into_iter().map(|s| {
1862+
Problem::BlessedVersionExtraLocalSpec {
1863+
spec_file_name: s
1864+
.spec_file_name()
1865+
.as_versioned()
1866+
.expect("blessed extra spec is versioned")
1867+
.clone(),
1868+
}
1869+
}));
1870+
18911871
Resolution::new_blessed(problems)
18921872
}
18931873

crates/dropshot-api-manager/src/spec_files_blessed.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,27 @@ struct UnrecognizedPath;
226226
impl<'a> BlessedPathKind<'a> {
227227
/// Parse a path from git ls-tree output into its structural kind.
228228
fn parse(path: &'a Utf8Path) -> Result<Self, UnrecognizedPath> {
229-
let parts: Vec<_> = path.iter().collect();
230-
match parts.as_slice() {
231-
[_basename] => Ok(BlessedPathKind::Lockstep),
232-
[api_dir, basename] if basename.ends_with(".json.gitstub") => {
233-
Ok(BlessedPathKind::GitStubFile { api_dir, basename })
234-
}
235-
[api_dir, basename] => {
236-
Ok(BlessedPathKind::VersionedFile { api_dir, basename })
237-
}
238-
_ => Err(UnrecognizedPath),
229+
let mut iter = path.iter();
230+
let first = iter.next().ok_or(UnrecognizedPath)?;
231+
let Some(second) = iter.next() else {
232+
// Single-component path: lockstep file.
233+
return Ok(BlessedPathKind::Lockstep);
234+
};
235+
if iter.next().is_some() {
236+
// Three or more components: not recognized.
237+
return Err(UnrecognizedPath);
238+
}
239+
// Two-component path: versioned file or Git stub.
240+
if second.ends_with(".json.gitstub") {
241+
Ok(BlessedPathKind::GitStubFile {
242+
api_dir: first,
243+
basename: second,
244+
})
245+
} else {
246+
Ok(BlessedPathKind::VersionedFile {
247+
api_dir: first,
248+
basename: second,
249+
})
239250
}
240251
}
241252
}

0 commit comments

Comments
 (0)