Skip to main content

ass_core/utils/errors/
conversions.rs

1//! `From` conversions into [`CoreError`] from related error types.
2//!
3//! Centralizes the trait implementations that let foreign error types
4//! propagate into [`CoreError`] via the `?` operator.
5
6use super::CoreError;
7
8#[cfg(not(feature = "std"))]
9extern crate alloc;
10#[cfg(not(feature = "std"))]
11use alloc::format;
12
13/// Convert from parser errors
14impl From<crate::parser::ParseError> for CoreError {
15    fn from(err: crate::parser::ParseError) -> Self {
16        Self::Parse(err)
17    }
18}
19
20/// Convert from standard I/O errors (when std is available)
21#[cfg(feature = "std")]
22impl From<std::io::Error> for CoreError {
23    fn from(err: std::io::Error) -> Self {
24        Self::Io(format!("{err}"))
25    }
26}
27
28/// Convert from `core::str::Utf8Error`
29impl From<::core::str::Utf8Error> for CoreError {
30    fn from(err: ::core::str::Utf8Error) -> Self {
31        Self::Utf8Error {
32            position: 0, // Position not available from Utf8Error
33            message: format!("{err}"),
34        }
35    }
36}
37
38/// Convert from integer parse errors
39impl From<::core::num::ParseIntError> for CoreError {
40    fn from(err: ::core::num::ParseIntError) -> Self {
41        Self::InvalidNumeric(format!("Integer parse error: {err}"))
42    }
43}
44
45/// Convert from float parse errors
46impl From<::core::num::ParseFloatError> for CoreError {
47    fn from(err: ::core::num::ParseFloatError) -> Self {
48        Self::InvalidNumeric(format!("Float parse error: {err}"))
49    }
50}