mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-09 04:54:17 +00:00
review: give the range-limit refusal its own translatable wording
This commit is contained in:
@@ -14,6 +14,9 @@ pub enum PrintError {
|
||||
/// A range token that is neither a page number nor a page label.
|
||||
#[error("{0:?} is not a page number or label")]
|
||||
NotAPage(String),
|
||||
/// A range that names more pages than a listing will take, with the limit.
|
||||
#[error("the range selects more than {0} pages")]
|
||||
TooManySelected(usize),
|
||||
#[error("{0}")]
|
||||
Invalid(String),
|
||||
#[error("{0}")]
|
||||
@@ -33,9 +36,14 @@ pub fn in_english(wording: &str) -> String {
|
||||
|
||||
/// What a person can get wrong about what to print, in English, with their holes still in them.
|
||||
///
|
||||
/// An interface that reports these in another language translates these three, which is the whole
|
||||
/// An interface that reports these in another language translates these four, which is the whole
|
||||
/// reason they are gathered here; `wordings_are_said` keeps them in step with the errors.
|
||||
pub const WORDINGS: &[&str] = &["there are no pages to print", "page {n} is out of range (1–{count})", "{page} is not a page number or label"];
|
||||
pub const WORDINGS: &[&str] = &[
|
||||
"there are no pages to print",
|
||||
"page {n} is out of range (1–{count})",
|
||||
"{page} is not a page number or label",
|
||||
"the range selects more than {limit} pages",
|
||||
];
|
||||
|
||||
impl PrintError {
|
||||
/// This error as one sentence, said in the language `words` translates into.
|
||||
@@ -51,6 +59,7 @@ impl PrintError {
|
||||
}
|
||||
// The token goes in quoted, the way it is typed, as the error itself prints it.
|
||||
PrintError::NotAPage(token) => say(&words("{page} is not a page number or label"), &[("{page}", format!("{token:?}"))]),
|
||||
PrintError::TooManySelected(limit) => say(&words("the range selects more than {limit} pages"), &[("{limit}", limit.to_string())]),
|
||||
fault => fault.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ pub fn select_pages(count: usize, range: Option<&str>, labels: &[String], subset
|
||||
// Checked before a span is expanded, so the list never grows past the limit.
|
||||
let mut take = |from: usize, to: usize| {
|
||||
selected = selected.saturating_add(from.abs_diff(to).saturating_add(1));
|
||||
if selected > limit { Err(PrintError::Invalid(format!("the range selects more than {limit} pages"))) } else { Ok(()) }
|
||||
if selected > limit { Err(PrintError::TooManySelected(limit)) } else { Ok(()) }
|
||||
};
|
||||
for part in r.split([',', ';']).map(str::trim).filter(|p| !p.is_empty()) {
|
||||
// A complete label (e.g. "A-1") still wins over interpreting it as a range.
|
||||
|
||||
@@ -84,7 +84,7 @@ fn page_selection() {
|
||||
/// and the numbers apart from it, and says itself in English exactly as the error prints itself.
|
||||
#[test]
|
||||
fn wordings_are_said() {
|
||||
let mistakes = [PrintError::NoPages, PrintError::OutOfRange(7, 5), PrintError::NotAPage("x".into())];
|
||||
let mistakes = [PrintError::NoPages, PrintError::OutOfRange(7, 5), PrintError::NotAPage("x".into()), PrintError::TooManySelected(1_000)];
|
||||
for e in &mistakes {
|
||||
assert_eq!(e.said(&in_english), e.to_string(), "{e:?} says itself in English the way it prints itself");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user