use std::fmt::Display;
pub use bytes::Bytes;
macro_rules! bail {
($($t:tt)*) => {
return Err($crate::Error::InvalidInput($crate::printable(format!($($t)*))))
};
}
macro_rules! ensure {
($cond:expr, $($t:tt)*) => {
if !$cond {
bail!($($t)*)
}
};
}
#[allow(unused_macros)]
macro_rules! invalid {
($($t:tt)*) => {
$crate::Error::InvalidInput($crate::printable(format!($($t)*)))
};
}
#[allow(dead_code, reason = "which readers use it depends on the enabled features")]
pub(crate) fn shown(text: &[u8]) -> String {
const MOST: usize = 32;
let text = String::from_utf8_lossy(text);
let mut cut: String = text.chars().take(MOST).collect();
if text.chars().nth(MOST).is_some() {
cut.push('…');
}
format!("{cut:?}")
}
pub(crate) fn printable(message: String) -> String {
if !message.chars().any(char::is_control) {
return message;
}
message
.chars()
.map(|c| {
if c.is_control() {
c.escape_debug().to_string()
} else {
c.to_string()
}
})
.collect()
}
pub mod assets;
pub mod document;
mod flatten;
pub mod formats;
mod gaussian;
pub mod gltf;
pub mod images;
mod json;
mod keep;
mod material;
pub mod mesh;
pub mod meshopt;
mod num;
pub mod rig;
pub mod source;
#[cfg(any(
feature = "webp",
feature = "skp",
feature = "tga",
feature = "bmp",
feature = "gif",
feature = "tiff",
feature = "psd",
feature = "dds",
feature = "amf"
))]
mod png;
#[cfg(any(
feature = "tga",
feature = "bmp",
feature = "gif",
feature = "tiff",
feature = "psd",
feature = "dds"
))]
mod raster;
#[cfg(test)]
mod test_rng;
#[cfg(feature = "webp")]
mod webp;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
InvalidInput(String),
#[error(transparent)]
Io(#[from] std::io::Error),
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[allow(dead_code)]
pub(crate) trait Context<T> {
fn context(self, what: impl Display) -> Result<T>;
fn with_context<D: Display>(self, what: impl FnOnce() -> D) -> Result<T>;
}
impl<T> Context<T> for Option<T> {
fn context(self, what: impl Display) -> Result<T> {
self.ok_or_else(|| Error::InvalidInput(printable(what.to_string())))
}
fn with_context<D: Display>(self, what: impl FnOnce() -> D) -> Result<T> {
self.ok_or_else(|| Error::InvalidInput(printable(what().to_string())))
}
}
impl<T, E: Display> Context<T> for std::result::Result<T, E> {
fn context(self, what: impl Display) -> Result<T> {
self.map_err(|e| Error::InvalidInput(printable(format!("{what}: {e}"))))
}
fn with_context<D: Display>(self, what: impl FnOnce() -> D) -> Result<T> {
self.map_err(|e| Error::InvalidInput(printable(format!("{}: {e}", what()))))
}
}
macro_rules! invalid_from {
($($t:ty),* $(,)?) => {
$(impl From<$t> for Error {
fn from(e: $t) -> Self {
Error::InvalidInput(e.to_string())
}
})*
};
}
invalid_from!(
std::num::ParseIntError,
std::num::ParseFloatError,
std::num::TryFromIntError,
std::array::TryFromSliceError,
std::str::Utf8Error,
serde_json::Error,
);
#[cfg(any(feature = "3mf", feature = "usd"))]
invalid_from!(zip::result::ZipError);
#[cfg(feature = "3mf")]
invalid_from!(quick_xml::Error);