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

Improve error reporting #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/ttf2check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn main() {

let mut font = match ttf2mesh::TTFFile::from_file(font) {
Ok(font) => font,
Err(e) => {
println!(" - font load failed: {:?}", e);
Err(error) => {
println!(" - font load failed: {}", error);
std::process::exit(255);
}
};
Expand Down
18 changes: 9 additions & 9 deletions examples/ttf2mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn main() {

match ttf2mesh::Quality::from_str(&quality_str) {
Ok(q) => q,
Err(e) => {
Err(error) => {
println!(
"Can not parse quality ({}): {:?}. Try 'low', 'medium' or 'high'",
quality_str, e
"Can not parse quality ({}): {}. Try 'low', 'medium' or 'high'",
quality_str, error
);
std::process::exit(255);
}
Expand All @@ -33,8 +33,8 @@ fn main() {

let mut font = match ttf2mesh::TTFFile::from_file(font) {
Ok(font) => font,
Err(e) => {
println!(" - font load failed: {:?}", e);
Err(error) => {
println!(" - font load failed: {}", error);
std::process::exit(255);
}
};
Expand All @@ -43,8 +43,8 @@ fn main() {
println!("Mesh data char {:?}", char);
let mut glyph = match font.glyph_from_char(char) {
Ok(g) => g,
Err(_) => {
println!("- can not find glyph in the font file");
Err(error) => {
println!("- can not find glyph in the font file: {}", error);
continue;
}
};
Expand Down Expand Up @@ -74,8 +74,8 @@ fn main() {
);
println!("");
}
Err(e) => {
println!(" - could not generate 2d mesh: {:?}", e);
Err(error) => {
println!(" - could not generate 2d mesh: {}", error);
}
}

Expand Down
14 changes: 7 additions & 7 deletions examples/ttf2obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn main() {

match ttf2mesh::Quality::from_str(&quality_str) {
Ok(q) => q,
Err(e) => {
Err(error) => {
println!(
"Can not parse quality ({}): {:?}. Try 'low', 'medium' or 'high'",
quality_str, e
"Can not parse quality ({}): {}. Try 'low', 'medium' or 'high'",
quality_str, error
);
std::process::exit(255);
}
Expand All @@ -34,8 +34,8 @@ fn main() {

let mut font = match ttf2mesh::TTFFile::from_file(font) {
Ok(font) => font,
Err(e) => {
println!(" - font load failed: {:?}", e);
Err(error) => {
println!(" - font load failed: {}", error);
std::process::exit(255);
}
};
Expand All @@ -44,8 +44,8 @@ fn main() {

match font.export_to_obj(obj_file, quality) {
Ok(_) => println!("Done"),
Err(e) => {
println!(" - export failed: {:?}", e);
Err(error) => {
println!(" - export failed: {}", error);
std::process::exit(255);
}
}
Expand Down
115 changes: 109 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,124 @@
/// Represents an error by the library
use core::fmt;

/// Represents an error in the library
#[derive(Debug)]
pub enum Error {
/// Font could not be loaded. The library doesn't support all font types
FontLoadError,
FontLoadError(Ttf2MeshError),

/// Font could not be exported to an obj file
ObjExportError,
ObjExportError(Ttf2MeshError),

/// Mesh generation failed
Glyph2MeshError,
Glyph2MeshError(Ttf2MeshError),

/// Glyph is not found in the font file
GlyphNotFound,

/// Quality could not be parsed from input
QualityParse,
QualityParse(String),

/// File to be opened was not found
FileNotFound,
FileNotFound(Option<String>),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::FontLoadError(error) => write!(f, "could not load font: {}", error),
Error::ObjExportError(error) => write!(f, "could not export obj file: {}", error),
Error::Glyph2MeshError(error) => {
write!(f, "glyph could not be converted to mesh: {}", error)
}
Error::GlyphNotFound => write!(f, "the font file does not contain requested glyph"),
Error::QualityParse(value) => write!(
f,
"quality could not be parsed from input ({}), please set to high, medium or low",
value
),
Error::FileNotFound(path) => write!(f, "file {:?} was not found", path),
}
}
}

/// Error from ttf2mesh library
#[derive(Debug)]
pub enum Ttf2MeshError {
// not enough memory
NoMem,

// file size > TTF_MAX_FILE
Size,

// error opening file
Open,

// unsupported file version
Ver,

// invalid file structure
Fmt,

// no required tables in file
NoTab,

// invalid file or table checksum
CSum,

// unsupported table format
UTab,

// unable to create mesh
Mesher,

// glyph has no outline
NoOutline,

// error writing file
Writing,

// unknown error
Unknown,
}

impl std::error::Error for Ttf2MeshError {}

impl fmt::Display for Ttf2MeshError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Ttf2MeshError::NoMem => write!(f, "not enough memory (malloc failed)"),
Ttf2MeshError::Size => write!(f, "file size > TTF_MAX_FILE"),
Ttf2MeshError::Open => write!(f, "error opening file"),
Ttf2MeshError::Ver => write!(f, "unsupported file version"),
Ttf2MeshError::Fmt => write!(f, "invalid file structure"),
Ttf2MeshError::NoTab => write!(f, "no required tables in file"),
Ttf2MeshError::CSum => write!(f, "invalid file or table checksum"),
Ttf2MeshError::UTab => write!(f, "unsupported table format"),
Ttf2MeshError::Mesher => write!(f, "unable to create mesh"),
Ttf2MeshError::NoOutline => write!(f, "glyph has no outline"),
Ttf2MeshError::Writing => write!(f, "error writing file"),
Ttf2MeshError::Unknown => write!(f, "unknown error"),
}
}
}

impl From<i32> for Ttf2MeshError {
fn from(value: i32) -> Self {
match value {
1 => Ttf2MeshError::NoMem,
2 => Ttf2MeshError::Size,
3 => Ttf2MeshError::Open,
4 => Ttf2MeshError::Ver,
5 => Ttf2MeshError::Fmt,
6 => Ttf2MeshError::NoTab,
7 => Ttf2MeshError::CSum,
8 => Ttf2MeshError::UTab,
9 => Ttf2MeshError::Mesher,
10 => Ttf2MeshError::NoOutline,
11 => Ttf2MeshError::Writing,
_ => Ttf2MeshError::Unknown,
}
}
}
6 changes: 3 additions & 3 deletions src/glyph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a> Glyph<'a> {
pub fn to_2d_mesh<'b>(&mut self, quality: Quality) -> Result<Mesh<'b, Mesh2d>, Error> {
let mut mesh = MaybeUninit::uninit();

let features = sys::TTF_FEATURES_DFLT;
let features = sys::TTF_FEATURES_DFLT | sys::TTF_FEATURE_IGN_ERR;

let error = unsafe {
sys::ttf_glyph2mesh(
Expand All @@ -43,7 +43,7 @@ impl<'a> Glyph<'a> {
};

if error != ttf2mesh_sys::TTF_DONE as i32 {
return Err(Error::Glyph2MeshError);
return Err(Error::Glyph2MeshError(error.into()));
}

let mesh = unsafe { mesh.assume_init() };
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'a> Glyph<'a> {
};

if error != ttf2mesh_sys::TTF_DONE as i32 {
return Err(Error::Glyph2MeshError);
return Err(Error::Glyph2MeshError(error.into()));
}

let mesh = unsafe { mesh.assume_init() };
Expand Down
2 changes: 1 addition & 1 deletion src/quality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Quality {
"low" => Quality::Low,
"medium" => Quality::Medium,
"high" => Quality::High,
_ => return Err(Error::QualityParse),
_ => return Err(Error::QualityParse(val.into())),
});
}
};
Expand Down
14 changes: 8 additions & 6 deletions src/ttf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ impl TTFFile {

/// Load TTF font from a file
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<TTFFile, Error> {
if !Path::new(path.as_ref().as_os_str()).exists() {
return Err(Error::FileNotFound);
let path_check = Path::new(path.as_ref().as_os_str());

if !path_check.exists() {
return Err(Error::FileNotFound(
path_check.to_str().map(|v| v.to_string()),
));
}

let file_name = path_to_cstring(path);
Expand All @@ -82,8 +86,7 @@ impl TTFFile {

fn load(ttf: MaybeUninit<*mut sys::ttf_file>, error: i32) -> Result<TTFFile, Error> {
if error != ttf2mesh_sys::TTF_DONE as i32 {
// fprintf(stderr, "Unable to load font: %s\n", ttf_error_str[error]);
return Err(Error::FontLoadError);
return Err(Error::FontLoadError(error.into()));
}

Ok(Self {
Expand All @@ -103,8 +106,7 @@ impl TTFFile {
unsafe { sys::ttf_export_to_obj(self.ttf, file_name.as_ptr(), quality.as_u8()) };

if error != ttf2mesh_sys::TTF_DONE as i32 {
// fprintf(stderr, "Unable to export font: %s\n", ttf_error_str[error]);
return Err(Error::ObjExportError);
return Err(Error::ObjExportError(error.into()));
}

Ok(())
Expand Down