Skip to content

Commit cdbc088

Browse files
authored
[meta] minor cleanups (#80)
One small functional change where we now reject Git stubs that aren't valid UTF-8. (These probably wouldn't have parsed earlier anyway, and in any case accepting them is incorrect.)
1 parent 0a74333 commit cdbc088

12 files changed

Lines changed: 75 additions & 85 deletions

File tree

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl VersionedApiSpecFileName {
198198
/// Returns the path of this file relative to the root of the OpenAPI
199199
/// documents.
200200
pub fn path(&self) -> Utf8PathBuf {
201-
Utf8PathBuf::from_iter([self.ident.deref().clone(), self.basename()])
201+
Utf8PathBuf::from_iter([self.ident.as_str(), &self.basename()])
202202
}
203203

204204
/// Returns the base name of this file path.
@@ -216,54 +216,44 @@ impl VersionedApiSpecFileName {
216216
}
217217
}
218218

219-
/// Converts this filename to its JSON equivalent.
220-
///
221-
/// If already JSON, returns a clone of self.
222-
pub fn to_json(&self) -> Self {
219+
/// Returns a copy of this filename with the given storage kind.
220+
fn with_kind(&self, kind: VersionedApiSpecKind) -> Self {
223221
Self {
224222
ident: self.ident.clone(),
225223
version: self.version.clone(),
226224
hash: self.hash.clone(),
227-
kind: VersionedApiSpecKind::Json,
225+
kind,
228226
}
229227
}
230228

229+
/// Converts this filename to its JSON equivalent.
230+
///
231+
/// If already JSON, returns a clone of self.
232+
pub fn to_json(&self) -> Self {
233+
self.with_kind(VersionedApiSpecKind::Json)
234+
}
235+
231236
/// Converts this filename to its Git stub equivalent.
232237
///
233238
/// If already a Git stub, returns a clone of self.
234239
pub fn to_git_stub(&self) -> Self {
235-
Self {
236-
ident: self.ident.clone(),
237-
version: self.version.clone(),
238-
hash: self.hash.clone(),
239-
kind: VersionedApiSpecKind::GitStub,
240-
}
240+
self.with_kind(VersionedApiSpecKind::GitStub)
241241
}
242242

243243
/// Returns the basename as a Git stubname.
244244
///
245245
/// - If already a Git stub, returns `basename()` directly.
246246
/// - If JSON, returns `basename() + ".gitstub"`.
247247
pub fn git_stub_basename(&self) -> String {
248-
match self.kind {
249-
VersionedApiSpecKind::GitStub => self.basename(),
250-
VersionedApiSpecKind::Json => {
251-
format!("{}.gitstub", self.basename())
252-
}
253-
}
248+
self.to_git_stub().basename()
254249
}
255250

256251
/// Returns the basename as a JSON filename.
257252
///
258253
/// - If already JSON, returns `basename()` directly.
259254
/// - If Git stub, returns the basename without `.gitstub`.
260255
pub fn json_basename(&self) -> String {
261-
match self.kind {
262-
VersionedApiSpecKind::Json => self.basename(),
263-
VersionedApiSpecKind::GitStub => {
264-
format!("{}-{}-{}.json", self.ident, self.version, self.hash)
265-
}
266-
}
256+
self.to_json().basename()
267257
}
268258
}
269259

@@ -497,8 +487,10 @@ impl ApiIdent {
497487
}
498488

499489
/// Given an API identifier and a file name, determine if we're looking at
500-
/// this API's "latest" symlink
490+
/// this API's "latest" symlink.
501491
pub fn versioned_api_is_latest_symlink(&self, base_name: &str) -> bool {
502-
base_name == self.versioned_api_latest_symlink()
492+
base_name
493+
.strip_prefix(self.0.as_str())
494+
.is_some_and(|rest| rest == "-latest.json")
503495
}
504496
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ impl Versions {
4242

4343
/// Returns whether this API is lockstep (as opposed to versioned)
4444
pub fn is_lockstep(&self) -> bool {
45-
match self {
46-
Versions::Lockstep { .. } => true,
47-
Versions::Versioned { .. } => false,
48-
}
45+
!self.is_versioned()
4946
}
5047

5148
/// Iterate over the semver versions of an API that are supported

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl From<ManagedApiConfig> for ManagedApi {
134134
api_description,
135135
} = value;
136136
ManagedApi {
137-
ident: ApiIdent::from(ident.to_owned()),
137+
ident: ApiIdent::from(ident),
138138
versions,
139139
title,
140140
metadata,

crates/dropshot-api-manager/src/cmd/check.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
apis::ManagedApis,
55
environment::{BlessedSource, GeneratedSource, ResolvedEnv},
66
output::{
7-
CheckResult, OutputOpts, Styles, display_load_problems,
8-
display_resolution, headers::*,
7+
CheckResult, OutputOpts, display_load_problems, display_resolution,
8+
headers::*,
99
},
1010
resolved::Resolved,
1111
};
@@ -17,10 +17,7 @@ pub(crate) fn check_impl(
1717
generated_source: &GeneratedSource,
1818
output: &OutputOpts,
1919
) -> anyhow::Result<CheckResult> {
20-
let mut styles = Styles::default();
21-
if output.use_color(supports_color::Stream::Stderr) {
22-
styles.colorize();
23-
}
20+
let styles = output.styles(supports_color::Stream::Stderr);
2421

2522
eprintln!("{:>HEADER_WIDTH$}", SEPARATOR);
2623

crates/dropshot-api-manager/src/cmd/debug.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
environment::{
66
BlessedSource, ErrorAccumulator, GeneratedSource, ResolvedEnv,
77
},
8-
output::{OutputOpts, Styles},
8+
output::OutputOpts,
99
resolved::Resolved,
1010
spec_files_generic::{ApiFiles, AsRawFiles},
1111
};
@@ -19,10 +19,7 @@ pub(crate) fn debug_impl(
1919
generated_source: &GeneratedSource,
2020
output: &OutputOpts,
2121
) -> anyhow::Result<()> {
22-
let mut styles = Styles::default();
23-
if output.use_color(supports_color::Stream::Stderr) {
24-
styles.colorize();
25-
}
22+
let styles = output.styles(supports_color::Stream::Stderr);
2623

2724
// Print information about local files.
2825

crates/dropshot-api-manager/src/cmd/generate.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ pub(crate) fn generate_impl(
3838
generated_source: &GeneratedSource,
3939
output: &OutputOpts,
4040
) -> Result<GenerateResult> {
41-
let mut styles = Styles::default();
42-
if output.use_color(supports_color::Stream::Stderr) {
43-
styles.colorize();
44-
}
41+
let styles = output.styles(supports_color::Stream::Stderr);
4542

4643
let (generated, errors) =
4744
generated_source.load(apis, &styles, &env.repo_root, &env.vcs)?;

crates/dropshot-api-manager/src/cmd/list.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::{
44
apis::ManagedApis,
5-
output::{OutputOpts, Styles, display_api_spec, display_error, plural},
5+
output::{OutputOpts, display_api_spec, display_error, plural},
66
};
77
use indent_write::io::IndentWriter;
88
use openapiv3::OpenAPI;
@@ -14,10 +14,7 @@ pub(crate) fn list_impl(
1414
verbose: bool,
1515
output: &OutputOpts,
1616
) -> anyhow::Result<()> {
17-
let mut styles = Styles::default();
18-
if output.use_color(supports_color::Stream::Stdout) {
19-
styles.colorize();
20-
}
17+
let styles = output.styles(supports_color::Stream::Stdout);
2118
let mut out = std::io::stdout();
2219

2320
let total = apis.len();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ fn get_json_value(
150150
}
151151

152152
fn surround_with_map(
153-
pointer: &str,
153+
last_component: &str,
154154
value: &serde_json::Value,
155155
) -> serde_json::Value {
156156
let mut map = serde_json::Map::new();
157-
let last_component = pointer.split('/').next_back().unwrap_or("");
158157
map.insert(unescape_pointer_component(last_component), value.clone());
159158
serde_json::Value::Object(map)
160159
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ impl OutputOpts {
3737
ColorChoice::Never => false,
3838
}
3939
}
40+
41+
/// Creates a `Styles` instance, colorized if color is enabled for the
42+
/// given stream.
43+
pub(crate) fn styles(&self, stream: supports_color::Stream) -> Styles {
44+
let mut styles = Styles::default();
45+
if self.use_color(stream) {
46+
styles.colorize();
47+
}
48+
styles
49+
}
4050
}
4151

4252
#[derive(Clone, Debug, Default)]
@@ -123,14 +133,16 @@ where
123133
}
124134

125135
pub(crate) fn display_api_spec(api: &ManagedApi, styles: &Styles) -> String {
126-
let versions: Vec<_> = api.iter_versions_semver().collect();
127-
let latest_version = versions.last().expect("must be at least one version");
136+
let mut versions = api.iter_versions_semver();
137+
let count = versions.len();
138+
let latest_version =
139+
versions.next_back().expect("must be at least one version");
128140
if api.is_versioned() {
129141
format!(
130142
"{} ({}, versioned ({} supported), latest = {})",
131143
api.ident().style(styles.filename),
132144
api.title(),
133-
versions.len(),
145+
count,
134146
latest_version,
135147
)
136148
} else {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ where
3636
T: Display,
3737
{
3838
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39-
// slice::join would require the use of unstable Rust.
4039
let mut iter = self.0.iter();
4140
if let Some(item) = iter.next() {
4241
write!(f, "{item}")?;
@@ -1008,7 +1007,7 @@ impl<'a> Resolved<'a> {
10081007
};
10091008
let api_local = local.get(&ident);
10101009
(
1011-
api.ident().clone(),
1010+
ident,
10121011
resolve_api(
10131012
env,
10141013
api,
@@ -1902,7 +1901,7 @@ fn resolve_api_version_local<'a>(
19021901
) -> Resolution<'a> {
19031902
let mut problems = Vec::new();
19041903

1905-
// alidate the generated API document.
1904+
// Validate the generated API document.
19061905
validate_generated(env, api, validation, version, generated, &mut problems);
19071906

19081907
let (matching, non_matching): (Vec<_>, Vec<_>) = local

0 commit comments

Comments
 (0)