use std::{
collections::HashMap,
sync::{Arc, OnceLock},
};
use topcoat::{context::Cx, view::*};
use super::{Column, ColumnWidth};
use crate::{
Lens,
schema::{Binding, EmbeddedForm, FieldResolver, Schema},
};
pub struct EmbeddedColumn<M, T> {
lens: Lens<M, T>,
binding: Binding,
label: Option<String>,
schema: Arc<OnceLock<Schema>>,
width: ColumnWidth,
}
impl<M, T> EmbeddedColumn<M, T>
where
M: toasty::schema::Model,
T: EmbeddedForm,
{
pub fn new(lens: Lens<M, T>) -> Self {
let binding = Binding::of(lens.path());
Self {
lens,
binding,
label: None,
schema: Arc::default(),
width: ColumnWidth::Wide,
}
}
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
pub fn width(mut self, width: ColumnWidth) -> Self {
self.width = width;
self
}
fn spelled(&self, row: &M) -> Option<(&Schema, HashMap<String, String>)> {
let schema = self.schema.get()?;
let mut values = HashMap::new();
self.lens
.read(row)
.write_node(schema.embedded_root(), &mut values);
Some((schema, values))
}
}
impl<M, T> Column<M> for EmbeddedColumn<M, T>
where
M: toasty::schema::Model + Send + Sync + 'static,
T: EmbeddedForm + Send + Sync + 'static,
{
fn name(&self) -> &str {
self.binding.name()
}
fn label(&self) -> &str {
self.label.as_deref().unwrap_or(self.binding.label())
}
fn text(&self, _cx: &Cx, row: &M) -> String {
let Some((schema, values)) = self.spelled(row) else {
return String::new();
};
schema
.embedded_root()
.shown(&schema.fields, &values)
.into_iter()
.filter_map(|index| {
let field = &schema.fields[index];
let value = values.get(field.name()).map_or("", String::as_str);
let read = field.read(value)?;
Some(format!("{}: {read}", field.label_str()))
})
.collect::<Vec<_>>()
.join(", ")
}
fn entry<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
match self.spelled(row) {
Some((schema, values)) => schema.embedded_root().display(cx, &schema.fields, &values),
None => ().boxed(),
}
}
fn column_width(&self) -> ColumnWidth {
self.width
}
fn misdeclared(&self) -> Option<crate::DeclarationErrorKind> {
if let Some(error) = self.binding.misdeclared() {
return Some(error);
}
match self.schema.get() {
Some(schema) => schema.declaration_errors().into_iter().next(),
None => Some(crate::DeclarationErrorKind::Unbound {
item: std::any::type_name::<T>(),
}),
}
}
fn bind(&self, resolver: &FieldResolver) {
self.binding.bind(resolver);
self.schema
.get_or_init(|| T::build_schema(resolver, self.lens.path().clone()));
}
}
impl<M, T> Clone for EmbeddedColumn<M, T> {
fn clone(&self) -> Self {
Self {
lens: self.lens.clone(),
binding: self.binding.clone(),
label: self.label.clone(),
schema: Arc::clone(&self.schema),
width: self.width,
}
}
}
impl<M, T> std::fmt::Debug for EmbeddedColumn<M, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EmbeddedColumn")
.field("name", &self.binding.name())
.field(
"label",
&self.label.as_deref().unwrap_or(self.binding.label()),
)
.field("value", &std::any::type_name::<T>())
.field("bound", &self.schema.get().is_some())
.finish_non_exhaustive()
}
}