Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise exit status section #30

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn main() {
Section::new("usage note")
.paragraph("This program will overwrite any file currently stored at the output path")
)
.exit_status(ExitStatus::default())
.render();

println!("{}", page);
Expand Down
1 change: 1 addition & 0 deletions examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn main() {
.paragraph("text for the custom section")
.paragraph("Additional text for the custom section"),
)
.exit_status(ExitStatus::default())
.author(Author::new("Alice Person").email("[email protected]"))
.author(Author::new("Bob Human").email("[email protected]"))
.render();
Expand Down
30 changes: 30 additions & 0 deletions src/exit_status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Add a exit status section
#[derive(Debug, Clone, Default)]
pub struct ExitStatus {
pub(crate) code: Option<i32>,
pub(crate) description: Option<&'static str>,
pub(crate) use_default_instead: bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. If you have a bunch of these and any one of them has this boolean flag set to true, you'll ignore all information and instead print a list of default statuses? I'd do this with an enum like enum ExitStatuses { DefaultStatuses, List(Vec<ExitStatus>) } (with ExitStatus containing fields for code and description)

}

impl ExitStatus {
pub fn new(code: i32) -> Self {
Self {
code: Some(code),
description: None,
use_default_instead: false,
}
}

pub fn default() -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are already deriving Default which generates an equivalent method

Self {
code: None,
description: None,
use_default_instead: true,
}
}

pub fn description(mut self, description: &'static str) -> Self {
self.description = Some(description);
self
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod arg;
mod author;
mod environment;
mod example;
mod exit_status;
mod flag;
mod man;
mod option;
Expand All @@ -20,6 +21,7 @@ pub use arg::Arg;
pub use author::Author;
pub use environment::Env;
pub use example::Example;
pub use exit_status::ExitStatus;
pub use flag::Flag;
pub use man::Manual;
pub use option::Opt;
Expand Down
44 changes: 34 additions & 10 deletions src/man.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Manual {
environment: Vec<Env>,
arguments: Vec<Arg>,
custom_sections: Vec<Section>,
exit_statuses: Vec<ExitStatus>,
examples: Vec<Example>,
}

Expand All @@ -29,6 +30,7 @@ impl Manual {
arguments: vec![],
environment: vec![],
custom_sections: vec![],
exit_statuses: vec![],
examples: vec![],
}
}
Expand Down Expand Up @@ -89,6 +91,11 @@ impl Manual {
self
}

pub fn exit_status(mut self, exit_status: ExitStatus) -> Self {
self.exit_statuses.push(exit_status);
self
}

/// Render to a string.
pub fn render(self) -> String {
let man_num = 1;
Expand All @@ -108,7 +115,7 @@ impl Manual {
for section in self.custom_sections.into_iter() {
page = custom(page, section);
}
page = exit_status(page);
page = exit_status(page, &self.exit_statuses);
page = examples(page, &self.examples);
page = authors(page, &self.authors);
page.render()
Expand Down Expand Up @@ -336,9 +343,9 @@ fn env(page: Roff, environment: &[Env]) -> Roff {

/// Create a `EXIT STATUS` section.
///
/// ## Implementation Note
/// This currently only returns the status code `0`, and takes no arguments. We
/// should let it take arguments.
/// If initialized with the default method, this will have status codes of 0
/// (success), 1 (failure), and 101 (panic). Alternatively, it can be initialized
/// with custom codes and descriptions.
///
/// ## Formatting
/// ```txt
Expand All @@ -349,15 +356,32 @@ fn env(page: Roff, environment: &[Env]) -> Roff {
///
/// 2 Optional error
/// ```
fn exit_status(page: Roff) -> Roff {
page.section(
"EXIT STATUS",
&[
fn exit_status(page: Roff, exit_statuses: &[ExitStatus]) -> Roff {
if exit_statuses.is_empty() {
return page;
}
let arr = if exit_statuses
.iter()
.any(|status| status.use_default_instead)
{
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
vec![
list(&[bold("0")], &["Successful program execution.\n\n"]),
list(&[bold("1")], &["Unsuccessful program execution.\n\n"]),
list(&[bold("101")], &["The program panicked."]),
],
)
]
} else {
let mut arr = vec![];
for exit_status in exit_statuses {
let exit_code =
format!("{}", exit_status.code.expect("initialized with value"));
let mut exit_description =
String::from(exit_status.description.unwrap_or(""));
exit_description.push_str("\n\n");
arr.push(list(&[bold(&exit_code)], &[exit_description]));
}
arr
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
};
page.section("EXIT STATUS", &arr)
}

/// Create a custom section.
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use arg::Arg;
pub use author::Author;
pub use environment::Env;
pub use example::Example;
pub use exit_status::ExitStatus;
pub use flag::Flag;
pub use man::Manual;
pub use option::Opt;
Expand Down