Skip to content

Commit

Permalink
Minimalist error handling by exiting on error
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 4, 2022
1 parent 632e122 commit 726d043
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
3 changes: 0 additions & 3 deletions generate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ version = "0.0.0"
authors = ["David Tolnay <[email protected]>"]
edition = "2018"
publish = false

[dependencies]
anyhow = "1"
21 changes: 7 additions & 14 deletions generate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![allow(
clippy::cast_lossless,
clippy::cast_possible_truncation, // https://github.com/rust-lang/rust-clippy/issues/9613
clippy::let_underscore_drop,
clippy::match_wild_err_arm,
clippy::module_name_repetitions,
clippy::too_many_lines,
Expand All @@ -22,7 +23,6 @@ mod parse;
mod write;

use crate::parse::parse_xid_properties;
use anyhow::Result;
use std::collections::{BTreeMap as Map, VecDeque};
use std::convert::TryFrom;
use std::fs;
Expand All @@ -34,20 +34,11 @@ const CHUNK: usize = 64;
const UCD: &str = "UCD";
const TABLES: &str = "src/tables.rs";

fn main() -> Result<()> {
fn main() {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let unicode_ident_dir = manifest_dir.parent().unwrap();
let ucd_dir = unicode_ident_dir.join(UCD);
if !ucd_dir.exists() {
writeln!(
io::stderr(),
"Not found: {}\nDownload from https://www.unicode.org/Public/zipped/l5.0.0/UCD.zip and unzip.",
ucd_dir.display(),
)?;
process::exit(1);
}

let properties = parse_xid_properties(&ucd_dir)?;
let properties = parse_xid_properties(&ucd_dir);

let mut chunkmap = Map::<[u8; CHUNK], u8>::new();
let mut dense = Vec::<[u8; CHUNK]>::new();
Expand Down Expand Up @@ -158,6 +149,8 @@ fn main() -> Result<()> {

let out = write::output(&properties, &index_start, &index_continue, &halfdense);
let path = unicode_ident_dir.join(TABLES);
fs::write(path, out)?;
Ok(())
if let Err(err) = fs::write(&path, out) {
let _ = writeln!(io::stderr(), "{}: {err}", path.display());
process::exit(1);
}
}
23 changes: 15 additions & 8 deletions generate/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::{bail, Result};
use std::collections::BTreeSet as Set;
use std::fs;
use std::io::{self, Write};
use std::path::Path;
use std::process;

pub struct Properties {
xid_start: Set<u32>,
Expand All @@ -18,23 +19,29 @@ impl Properties {
}
}

pub fn parse_xid_properties(ucd_dir: &Path) -> Result<Properties> {
pub fn parse_xid_properties(ucd_dir: &Path) -> Properties {
let mut properties = Properties {
xid_start: Set::new(),
xid_continue: Set::new(),
};

let filename = "DerivedCoreProperties.txt";
let path = ucd_dir.join(filename);
let contents = fs::read_to_string(path)?;
let contents = fs::read_to_string(path).unwrap_or_else(|err| {
let suggestion =
"Download from https://www.unicode.org/Public/zipped/l5.0.0/UCD.zip and unzip.";
let _ = writeln!(io::stderr(), "{}: {err}\n{suggestion}", ucd_dir.display());
process::exit(1);
});

for (i, line) in contents.lines().enumerate() {
if line.starts_with('#') || line.trim().is_empty() {
continue;
}
let (lo, hi, name) = match parse_line(line) {
Some(line) => line,
None => bail!("{} line {} is unexpected:\n{}", filename, i, line),
};
let (lo, hi, name) = parse_line(line).unwrap_or_else(|| {
let _ = writeln!(io::stderr(), "{filename} line {i} is unexpected:\n{line}");
process::exit(1);
});
let set = match name {
"XID_Start" => &mut properties.xid_start,
"XID_Continue" => &mut properties.xid_continue,
Expand All @@ -43,7 +50,7 @@ pub fn parse_xid_properties(ucd_dir: &Path) -> Result<Properties> {
set.extend(lo..=hi);
}

Ok(properties)
properties
}

fn parse_line(line: &str) -> Option<(u32, u32, &str)> {
Expand Down

0 comments on commit 726d043

Please sign in to comment.