#[cfg(not(redb_no_std))]
pub use std::io::Error;
#[cfg(redb_no_std)]
pub use no_std::Error;
pub(crate) fn invalid_data(message: &str) -> Error {
#[cfg(not(redb_no_std))]
{
Error::new(std::io::ErrorKind::InvalidData, message)
}
#[cfg(redb_no_std)]
{
Error::other(message)
}
}
pub(crate) fn invalid_input(message: &str) -> Error {
#[cfg(not(redb_no_std))]
{
Error::new(std::io::ErrorKind::InvalidInput, message)
}
#[cfg(redb_no_std)]
{
Error::other(message)
}
}
#[cfg(any(redb_no_std, test))]
#[cfg_attr(not(redb_no_std), allow(dead_code))]
mod no_std {
use alloc::string::String;
use core::fmt::{Debug, Display, Formatter};
#[derive(Debug)]
#[non_exhaustive]
pub struct Error {
message: String,
}
impl Error {
pub fn other(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.message)
}
}
impl core::error::Error for Error {}
#[cfg(test)]
mod test {
use super::Error;
use alloc::format;
#[test]
fn display_is_the_message() {
let error = Error::other("Index out-of-range.");
assert_eq!(format!("{error}"), "Index out-of-range.");
}
#[test]
fn accepts_an_owned_message() {
let error = Error::other(format!("page {} is out of range", 7));
assert_eq!(format!("{error}"), "page 7 is out of range");
}
}
}