Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ panic = "warn"
todo = "warn"
unimplemented = "warn"
dbg_macro = "warn"
# `let _ =` hides a `#[must_use]` result, such as a dropped error.
let_underscore_must_use = "warn"
# Every exception names its lint with `#[expect]` and says why.
allow_attributes = "warn"
allow_attributes_without_reason = "warn"
Expand Down
5 changes: 3 additions & 2 deletions apps/auth-service/src/cli/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Output {
/// it is ignored, since the command fails anyway.
pub(super) fn error(&self, error: &(dyn Error + 'static)) {
let causes = std::iter::successors(error.source(), |&cause| cause.source());
let _ = if self.0.json {
(if self.0.json {
self.event(&Event::Error {
message: error.to_string(),
causes: causes.map(ToString::to_string).collect(),
Expand All @@ -276,7 +276,8 @@ impl Output {
Ok(())
})
.map_err(CliError::Output)
};
})
.ok();
}

/// Asks `question` on standard error with a `[y/N]` suffix, and reads the
Expand Down
43 changes: 22 additions & 21 deletions apps/auth-service/src/cli/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::Cli;
use clap::{Arg, ArgAction, Command, CommandFactory};
use sourceorigin_common::config::GeneratedFile;
use std::fmt::Write as _;
use std::fmt::{self, Write as _};

/// The page's title and introduction, after the generated-file comment.
const INTRO: &str = "\
Expand All @@ -28,7 +28,7 @@ struct Reference {

impl Reference {
/// Renders the page from the whole command tree.
fn render() -> String {
fn render() -> Result<String, fmt::Error> {
let mut command = Cli::command().bin_name("auth-service");
// Building fills in each subcommand's full name and its usage.
command.build();
Expand All @@ -38,39 +38,39 @@ impl Reference {
`apps/auth-service/src/cli`. Do not edit it by hand. -->\n\n{INTRO}"
),
};
reference.command(&command, "auth-service");
reference.page
reference.command(&command, "auth-service")?;
Ok(reference.page)
}

/// Adds `command`'s section: what it does, its usage, its arguments and
/// options, and links to its subcommands, whose sections follow.
/// `invocation` is how to call it: its path with the arguments its parents
/// take before it, such as `auth-service account <USER> name`, which clap's
/// usage leaves out.
fn command(&mut self, command: &Command, invocation: &str) {
fn command(&mut self, command: &Command, invocation: &str) -> fmt::Result {
let path = command.get_bin_name().unwrap_or(command.get_name());
let _ = writeln!(self.page, "\n## `{path}`\n");
writeln!(self.page, "\n## `{path}`\n")?;
if let Some(about) = command.get_long_about().or(command.get_about()) {
let _ = writeln!(self.page, "{}\n", Self::sentence(&about.to_string()));
writeln!(self.page, "{}\n", Self::sentence(&about.to_string()))?;
}
let usage = command.clone().render_usage().to_string();
let _ = writeln!(
writeln!(
self.page,
"```text\n{}\n```",
usage
.trim_start_matches("Usage: ")
.replacen(path, invocation, 1)
);
)?;

let (positionals, options): (Vec<&Arg>, Vec<&Arg>) = command
.get_arguments()
.filter(|argument| Self::documented(argument))
.partition(|argument| argument.is_positional());
for (title, arguments) in [("Arguments", positionals), ("Options", options)] {
if !arguments.is_empty() {
let _ = writeln!(self.page, "\n{title}:\n");
writeln!(self.page, "\n{title}:\n")?;
for argument in arguments {
self.argument(argument);
self.argument(argument)?;
}
}
}
Expand All @@ -81,32 +81,33 @@ impl Reference {
.filter(|subcommand| !subcommand.is_hide_set() && subcommand.get_name() != "help")
.collect();
if !subcommands.is_empty() {
let _ = writeln!(self.page, "\nCommands:\n");
writeln!(self.page, "\nCommands:\n")?;
for subcommand in &subcommands {
let name = format!("{path} {}", subcommand.get_name());
let about = subcommand
.get_about()
.map_or_default(|about| Self::sentence(&about.to_string()));
let _ = writeln!(
writeln!(
self.page,
"- [`{name}`](#{}): {about}",
name.replace(' ', "-")
);
)?;
}
}
let mut arguments = String::new();
for argument in command.get_positionals() {
let _ = write!(arguments, " {argument}");
write!(arguments, " {argument}")?;
}
for subcommand in subcommands {
let invocation = format!("{invocation}{arguments} {}", subcommand.get_name());
self.command(subcommand, &invocation);
self.command(subcommand, &invocation)?;
}
Ok(())
}

/// Adds one argument or option: how to write it, what it does, and its
/// default and accepted values.
fn argument(&mut self, argument: &Arg) {
fn argument(&mut self, argument: &Arg) -> fmt::Result {
let syntax = match (argument.get_short(), argument.get_long()) {
(Some(short), Some(_)) => format!("-{short}, {argument}"),
_ => argument.to_string(),
Expand All @@ -127,7 +128,7 @@ impl Reference {
.filter(|value| value != "``")
.collect();
if takes_values && !defaults.is_empty() {
let _ = write!(text, " Default: {}.", defaults.join(", "));
write!(text, " Default: {}.", defaults.join(", "))?;
}
let values: Vec<String> = argument
.get_possible_values()
Expand All @@ -139,9 +140,9 @@ impl Reference {
})
.collect();
if takes_values && !values.is_empty() {
let _ = write!(text, " Values: {}.", values.join(", "));
write!(text, " Values: {}.", values.join(", "))?;
}
let _ = writeln!(self.page, "- {}", text.trim_end());
writeln!(self.page, "- {}", text.trim_end())
}

/// Whether `argument` belongs on the page: not hidden, and not clap's own
Expand Down Expand Up @@ -175,7 +176,7 @@ fn cli_reference_is_current() {
"/../../docs/dev/operations/cli-commands.md"
)
.into(),
text: Reference::render(),
text: Reference::render().expect("rendered"),
}
.write_or_compare()
.expect("current reference");
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/cli/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Command {
};
let jobs = async {
// Fails only once `stop` is dropped, which also means stopping.
let _ = stop.subscribe().wait_for(|stopped| *stopped).await;
stop.subscribe().wait_for(|stopped| *stopped).await.ok();
jobs.shutdown().await
};
let signal = async {
Expand Down
3 changes: 2 additions & 1 deletion apps/auth-service/src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ fn configuration_reference_is_current() {
&sections,
3,
"the variables in `apps/auth-service/src/config`",
),
)
.expect("rendered"),
}
.write_or_compare()
.expect("current reference");
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/deletion/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ impl AccountExport {
};
tracing::error!(error = %ErrorChain(&error), "account export failed");
// The browser may already be gone.
let _ = sender.send(Err(error)).await;
sender.send(Err(error)).await.ok();
}
.in_current_span(),
);
Expand Down
2 changes: 1 addition & 1 deletion apps/auth-service/src/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Shutdown {
/// Resolves once shutdown has been requested.
pub(crate) async fn requested(&self) {
// An error means the runner is gone, which is a shutdown too.
let _ = self.0.clone().wait_for(|&requested| requested).await;
self.0.clone().wait_for(|&requested| requested).await.ok();
}

/// Runs `work` unless shutdown is requested first: its output, or `None`
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ fn configuration_reference_is_current() {
}],
3,
"the variables in `apps/web/src/main.rs`",
),
)
.expect("rendered"),
}
.write_or_compare()
.expect("current reference");
Expand Down
27 changes: 17 additions & 10 deletions crates/common/src/config/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
//! [`GeneratedFile`].

use super::Variable;
use std::{fmt::Write as _, fs, io, path::PathBuf};
use std::{
fmt::{self, Write as _},
fs, io,
path::PathBuf,
};
use thiserror::Error;

/// Set to rewrite generated files instead of comparing them; `just docs reference`
Expand Down Expand Up @@ -66,7 +70,7 @@ pub struct Documented {
impl Documented {
/// Renders the variable as a list item: its name, its description, and its
/// default. A blank doc comment line starts a new paragraph.
fn render(&self, out: &mut String) {
fn render(&self, out: &mut String) -> fmt::Result {
let mut paragraphs: Vec<String> = vec![String::new()];
for line in self.lines {
let line = line.strip_prefix(' ').unwrap_or(line).trim_end();
Expand All @@ -90,14 +94,15 @@ impl Documented {
None => paragraphs.push(sentence),
}
}
let _ = write!(out, "- `{}`:", self.variable.name());
write!(out, "- `{}`:", self.variable.name())?;
for (index, paragraph) in paragraphs.iter().enumerate() {
if index == 0 {
let _ = writeln!(out, " {paragraph}");
writeln!(out, " {paragraph}")?;
} else {
let _ = writeln!(out, "\n {paragraph}");
writeln!(out, "\n {paragraph}")?;
}
}
Ok(())
}
}

Expand All @@ -113,18 +118,20 @@ pub struct Section {
impl Section {
/// Renders `sections` as a Markdown fragment, each under a heading of `level`,
/// with the comment saying where it comes from and how to regenerate it.
#[must_use]
pub fn render(sections: &[Self], level: usize, source: &str) -> String {
///
/// # Errors
/// Returns the formatter's error, which writing to a `String` never raises.
pub fn render(sections: &[Self], level: usize, source: &str) -> Result<String, fmt::Error> {
let mut out = format!(
"<!-- Generated by `just docs reference` from {source}. Do not edit it by hand. -->\n"
);
for section in sections {
let _ = writeln!(out, "\n{} {}\n", "#".repeat(level), section.title);
writeln!(out, "\n{} {}\n", "#".repeat(level), section.title)?;
for variable in section.variables {
variable.render(&mut out);
variable.render(&mut out)?;
}
}
out
Ok(out)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Telemetry {
let tracer = self.tracer.shutdown();
let meter = self.meter.map_or(Ok(()), |meter| meter.shutdown());
let logger = self.logger.map_or(Ok(()), |logger| logger.shutdown());
let _ = flushed.send(tracer.and(meter).and(logger));
flushed.send(tracer.and(meter).and(logger)).ok();
});
let flushed = tokio::time::timeout(within, result)
.await
Expand Down
Loading