use crate::builtin;
pub fn new_str(text: &str) -> ErrorString {
ErrorString::new(text)
}
pub const fn new_static(text: &'static str) -> ErrorStaticString {
ErrorStaticString { s: text }
}
#[derive(Debug)]
pub struct ErrorString {
s: String,
}
impl ErrorString {
pub fn new(text: &str) -> Self {
Self { s: text.to_owned() }
}
}
impl std::fmt::Display for ErrorString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.s)
}
}
impl std::error::Error for ErrorString {
}
impl builtin::Error for ErrorString {
fn error(&self) -> String {
self.s.clone()
}
}
#[derive(Debug, Clone, Copy)]
pub struct ErrorStaticString {
s: &'static str,
}
impl ErrorStaticString {
pub fn new(text: &'static str) -> Self {
Self { s: text }
}
}
impl std::fmt::Display for ErrorStaticString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.s)
}
}
impl std::error::Error for ErrorStaticString {}
impl builtin::Error for ErrorStaticString {
fn error(&self) -> String {
self.s.to_string()
}
}
pub fn new_stdio_other_error(message: String) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::Other, message)
}
pub fn new_unexpected_eof() -> std::io::Error {
std::io::Error::from(std::io::ErrorKind::UnexpectedEof)
}
pub fn copy_stdio_error(e: &std::io::Error) -> std::io::Error {
std::io::Error::new(e.kind(), e.to_string())
}
pub fn copy_stdio_option_error(e: &Option<std::io::Error>) -> Option<std::io::Error> {
e.as_ref().map(copy_stdio_error)
}
pub const RUST_EOF: std::io::Result<usize> = Ok(0);
pub fn is_rust_eof(res: &std::io::Result<usize>) -> bool {
if let Ok(value) = res {
*value == 0
} else {
false
}
}
pub fn iores_to_result(iores: crate::io::IoRes) -> std::io::Result<usize> {
if iores.0 > 0 {
Ok(iores.0)
} else if let Some(err) = iores.1 {
Err(err)
} else {
Ok(0)
}
}