Skip to content

Commit

Permalink
Move error helpers to their own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 30, 2024
1 parent 5b83e91 commit d69b141
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 219 deletions.
20 changes: 17 additions & 3 deletions src/collada/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,14 @@ fn parse_effect_color<'a>(
// texcoord,
};
}
"param" => warn::unsupported_child_elem(child),
"param" => {
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
}
_ => {}
}
}
Expand All @@ -517,7 +524,14 @@ fn parse_effect_float(node: xml::Node<'_, '_>) -> io::Result<Option<f32>> {
.ok_or_else(|| format_err!("error while parsing a float"))?,
);
}
"param" => warn::unsupported_child_elem(child),
"param" => {
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
}
_ => return Err(error::unexpected_child_elem(child)),
}
}
Expand Down Expand Up @@ -552,7 +566,7 @@ impl FromStr for Opaque {
"A_ONE" => Self::A_ONE,
"RGB_ZERO" => Self::RGB_ZERO,
"RGB_ONE" => Self::RGB_ONE,
_ => bail!("unknown shade type {:?}", s),
_ => bail!("unknown opaque type {:?}", s),
})
}
}
40 changes: 40 additions & 0 deletions src/collada/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::*;

#[cold]
pub(super) fn one_or_more_elems(node: xml::Node<'_, '_>, name: &str) -> io::Error {
format_err!(
"<{}> element must be contain one or more <{}> elements ({})",
node.tag_name().name(),
name,
node.node_location()
)
}

#[cold]
pub(super) fn exactly_one_elem(node: xml::Node<'_, '_>, name: &str) -> io::Error {
format_err!(
"<{}> element must be contain exactly one <{}> element ({})",
node.tag_name().name(),
name,
node.node_location()
)
}

#[cold]
pub(super) fn multiple_elems(node: xml::Node<'_, '_>) -> io::Error {
format_err!(
"multiple <{}> elements ({})",
node.tag_name().name(),
node.node_location()
)
}

#[cold]
pub(super) fn unexpected_child_elem(child: xml::Node<'_, '_>) -> io::Error {
format_err!(
"unexpected child element <{}> in <{}> element ({})",
child.tag_name().name(),
child.parent_element().unwrap().tag_name().name(),
child.node_location()
)
}
16 changes: 14 additions & 2 deletions src/collada/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ fn parse_geometry<'a>(
mesh = Some(parse_mesh(cx, node)?);
}
"convex_mesh" | "spline" | "brep" => {
warn::unsupported_child_elem(node);
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
return Ok(None);
}
"asset" | "extra" => { /* skip */ }
Expand Down Expand Up @@ -479,7 +484,14 @@ fn parse_primitive<'a>(node: xml::Node<'a, '_>, ty: PrimitiveType) -> io::Result
}
}
}
"ph" => warn::unsupported_child_elem(node),
"ph" => {
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
}
"extra" => { /* skip */ }
_ => return Err(error::unexpected_child_elem(node)),
}
Expand Down
58 changes: 1 addition & 57 deletions src/collada/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![allow(clippy::wildcard_imports)] // TODO

mod effect;
mod error;
mod geometry;
mod image;
mod instance;
Expand Down Expand Up @@ -885,60 +886,3 @@ impl FromStr for InputSemantic {
})
}
}

mod error {
use super::*;

#[cold]
pub(super) fn one_or_more_elems(node: xml::Node<'_, '_>, name: &str) -> io::Error {
format_err!(
"<{}> element must be contain one or more <{}> elements ({})",
node.tag_name().name(),
name,
node.node_location()
)
}

#[cold]
pub(super) fn exactly_one_elem(node: xml::Node<'_, '_>, name: &str) -> io::Error {
format_err!(
"<{}> element must be contain exactly one <{}> element ({})",
node.tag_name().name(),
name,
node.node_location()
)
}

#[cold]
pub(super) fn multiple_elems(node: xml::Node<'_, '_>) -> io::Error {
format_err!(
"multiple <{}> elements ({})",
node.tag_name().name(),
node.node_location()
)
}

#[cold]
pub(super) fn unexpected_child_elem(child: xml::Node<'_, '_>) -> io::Error {
format_err!(
"unexpected child element <{}> in <{}> element ({})",
child.tag_name().name(),
child.parent_element().unwrap().tag_name().name(),
child.node_location()
)
}
}

mod warn {
use super::*;

#[cold]
pub(super) fn unsupported_child_elem(_child: xml::Node<'_, '_>) {
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
}
}
18 changes: 16 additions & 2 deletions src/collada/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ fn parse_visual_scene<'a>(node: xml::Node<'a, '_>, nodes: &mut Vec<Node<'a>>) ->
"node" => {
scene_nodes.push(parse_node(child, nodes, this_index)?);
}
"evaluate_scene" => warn::unsupported_child_elem(child),
"evaluate_scene" => {
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
}
"asset" | "extra" => { /* skip */ }
_ => return Err(error::unexpected_child_elem(child)),
}
Expand Down Expand Up @@ -404,7 +411,14 @@ fn parse_instance_material<'a>(node: xml::Node<'a, '_>) -> io::Result<SemanticMa
// },
// );
}
"bind" => warn::unsupported_child_elem(child),
"bind" => {
// warn!(
// "<{}> child element in <{}> element is unsupported ({})",
// child.tag_name().name(),
// child.parent_element().unwrap().tag_name().name(),
// child.node_location()
// );
}
"extra" => { /* skip */ }
_ => return Err(error::unexpected_child_elem(child)),
}
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{fmt, path::Path};
#[cfg(any(feature = "obj", feature = "stl"))]
use crate::utils::bytes::{bytecount_naive, memrchr_naive};

#[cfg(any(feature = "collada", feature = "obj"))]
#[cfg(feature = "collada")]
macro_rules! format_err {
($msg:expr $(,)?) => {
crate::error::invalid_data($msg)
Expand All @@ -15,7 +15,7 @@ macro_rules! format_err {
};
}

#[cfg(any(feature = "collada", feature = "obj"))]
#[cfg(feature = "collada")]
macro_rules! bail {
($($tt:tt)*) => {
return Err(format_err!($($tt)*))
Expand Down
53 changes: 53 additions & 0 deletions src/obj/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{fmt, io, path::Path, str};

#[cfg_attr(test, derive(Debug))]
pub(super) enum ErrorKind {
ExpectedSpace(&'static str, usize),
ExpectedNewline(&'static str, usize),
Expected(&'static str, usize),
Float(usize),
Int(usize),
InvalidW(usize),
InvalidFaceIndex(usize),
Oob(usize, usize),
Io(io::Error),
}

impl ErrorKind {
#[cold]
#[inline(never)]
pub(super) fn into_io_error(self, start: &[u8], path: Option<&Path>) -> io::Error {
let remaining = match self {
Self::Expected(.., n)
| Self::ExpectedNewline(.., n)
| Self::ExpectedSpace(.., n)
| Self::Float(n)
| Self::Int(n)
| Self::InvalidW(n)
| Self::InvalidFaceIndex(n)
| Self::Oob(.., n) => n,
Self::Io(e) => return e,
};
crate::error::with_location(
&crate::error::invalid_data(self.to_string()),
&crate::error::Location::find(remaining, start, path),
)
}
}

impl fmt::Display for ErrorKind {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::ExpectedSpace(msg, ..) => write!(f, "expected space after {msg}"),
Self::ExpectedNewline(msg, ..) => write!(f, "expected newline after {msg}"),
Self::Expected(msg, ..) => write!(f, "expected {msg}"),
Self::InvalidW(..) => write!(f, "w in homogeneous vector must not zero"),
Self::InvalidFaceIndex(..) => write!(f, "invalid face index"),
Self::Float(..) => write!(f, "error while parsing a float"),
Self::Int(..) => write!(f, "error while parsing an integer"),
Self::Oob(i, ..) => write!(f, "face index out of bounds ({i})"),
Self::Io(ref e) => write!(f, "{e}"),
}
}
}
62 changes: 4 additions & 58 deletions src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#![allow(clippy::collapsible_if, clippy::many_single_char_names)]

mod error;

use std::{
collections::HashMap,
io, mem,
Expand Down Expand Up @@ -878,15 +880,15 @@ fn read_mtl_internal(
// clearcoat_factor: mat.clearcoat_thickness,
// clearcoat_roughness_factor: mat.clearcoat_roughness,
// anisotropy_factor: mat.anisotropy,
color: crate::Colors {
color: common::Colors {
ambient: color4(mat.ambient),
diffuse: color4(mat.diffuse),
specular: color4(mat.specular),
emissive: color4(mat.emissive),
transparent: color4(mat.transparent),
reflective: None,
},
texture: crate::Textures {
texture: common::Textures {
diffuse: texture_path(mat.diffuse_texture, mtl_dir),
ambient: texture_path(mat.ambient_texture, mtl_dir),
emissive: texture_path(mat.emissive_texture, mtl_dir),
Expand Down Expand Up @@ -1320,59 +1322,3 @@ fn name(s: &[u8]) -> (&[u8], &[u8]) {
}
(name, s_next)
}

mod error {
use std::{fmt, io, path::Path, str};

#[cfg_attr(test, derive(Debug))]
pub(super) enum ErrorKind {
ExpectedSpace(&'static str, usize),
ExpectedNewline(&'static str, usize),
Expected(&'static str, usize),
Float(usize),
Int(usize),
InvalidW(usize),
InvalidFaceIndex(usize),
Oob(usize, usize),
Io(io::Error),
}

impl ErrorKind {
#[cold]
#[inline(never)]
pub(super) fn into_io_error(self, start: &[u8], path: Option<&Path>) -> io::Error {
let remaining = match self {
Self::Expected(.., n)
| Self::ExpectedNewline(.., n)
| Self::ExpectedSpace(.., n)
| Self::Float(n)
| Self::Int(n)
| Self::InvalidW(n)
| Self::InvalidFaceIndex(n)
| Self::Oob(.., n) => n,
Self::Io(e) => return e,
};
crate::error::with_location(
&crate::error::invalid_data(self.to_string()),
&crate::error::Location::find(remaining, start, path),
)
}
}

impl fmt::Display for ErrorKind {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::ExpectedSpace(msg, ..) => write!(f, "expected space after {msg}"),
Self::ExpectedNewline(msg, ..) => write!(f, "expected newline after {msg}"),
Self::Expected(msg, ..) => write!(f, "expected {msg}"),
Self::InvalidW(..) => write!(f, "w in homogeneous vector must not zero"),
Self::InvalidFaceIndex(..) => write!(f, "invalid face index"),
Self::Float(..) => write!(f, "error while parsing a float"),
Self::Int(..) => write!(f, "error while parsing an integer"),
Self::Oob(i, ..) => write!(f, "face index out of bounds ({i})"),
Self::Io(ref e) => write!(f, "{e}"),
}
}
}
}
Loading

0 comments on commit d69b141

Please sign in to comment.