Minimalist error handling by exiting on error

This commit is contained in:
David Tolnay 2022-11-03 23:35:08 -07:00
parent 632e122166
commit 726d043ecd
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 22 additions and 25 deletions

View File

@ -4,6 +4,3 @@ version = "0.0.0"
authors = ["David Tolnay <dtolnay@gmail.com>"]
edition = "2018"
publish = false
[dependencies]
anyhow = "1"

View File

@ -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,
@ -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;
@ -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();
@ -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);
}
}

View File

@ -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>,
@ -18,7 +19,7 @@ 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(),
@ -26,15 +27,21 @@ pub fn parse_xid_properties(ucd_dir: &Path) -> Result<Properties> {
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,
@ -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)> {