leptos_struct_table/
cell_value.rsuse leptos::prelude::*;
#[derive(Default, Clone, Copy)]
pub struct NumberRenderOptions {
pub precision: Option<usize>,
}
pub trait CellValue<M: ?Sized = ()> {
type RenderOptions: Default + Clone + Send + Sync + 'static;
fn render_value(self, options: Self::RenderOptions) -> impl IntoView;
}
impl<V> CellValue<()> for V
where
V: IntoView,
{
type RenderOptions = ();
fn render_value(self, _options: Self::RenderOptions) -> impl IntoView {
self
}
}
macro_rules! viewable_primitive {
($($child_type:ty),* $(,)?) => {
$(
impl CellValue<$child_type> for $child_type {
type RenderOptions = ();
#[inline(always)]
fn render_value(self, _options: Self::RenderOptions) -> impl IntoView {
self.to_string()
}
}
)*
};
}
viewable_primitive![
&String,
char,
bool,
std::net::IpAddr,
std::net::SocketAddr,
std::net::SocketAddrV4,
std::net::SocketAddrV6,
std::net::Ipv4Addr,
std::net::Ipv6Addr,
std::char::ToUppercase,
std::char::ToLowercase,
std::num::NonZeroI8,
std::num::NonZeroU8,
std::num::NonZeroI16,
std::num::NonZeroU16,
std::num::NonZeroI32,
std::num::NonZeroU32,
std::num::NonZeroI64,
std::num::NonZeroU64,
std::num::NonZeroI128,
std::num::NonZeroU128,
std::num::NonZeroIsize,
std::num::NonZeroUsize,
std::panic::Location<'_>,
];
macro_rules! viewable_number_primitive {
($($child_type:ty),* $(,)?) => {
$(
impl CellValue<$child_type> for $child_type {
type RenderOptions = NumberRenderOptions;
#[inline(always)]
fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
if let Some(value) = options.precision.as_ref() {
view! {
<>{format!("{:.value$}", self)}</>
}
}
else {
view! {
<>{self.to_string()}</>
}
}
}
}
)*
};
}
viewable_number_primitive![
usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64,
];