use core::fmt;
use vize_l0::{String, cstr};
use crate::dump::Error as DumpError;
pub trait DumpValue: Sized {
fn print_value<W: fmt::Write>(&self, w: &mut W) -> fmt::Result;
fn parse_value(text: &str, line: usize) -> Result<Self, DumpError>;
}
impl DumpValue for bool {
fn print_value<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
write!(w, "{self}")
}
fn parse_value(text: &str, line: usize) -> Result<Self, DumpError> {
match text {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(DumpError::new(line, cstr!("invalid bool `{text}`"))),
}
}
}
impl DumpValue for String {
fn print_value<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
w.write_str(self.as_str())
}
fn parse_value(text: &str, _line: usize) -> Result<Self, DumpError> {
Ok(String::from(text))
}
}
macro_rules! impl_folio_value_for_int {
($($int:ty),* $(,)?) => {$(
impl DumpValue for $int {
fn print_value<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
write!(w, "{self}")
}
fn parse_value(text: &str, line: usize) -> Result<Self, DumpError> {
text.parse::<$int>()
.map_err(|_| DumpError::new(line, cstr!("invalid integer `{text}`")))
}
}
)*};
}
impl_folio_value_for_int!(
u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
);