mod ser;
pub use ser::*;
pub use serde_view_macros::View;
use std::{collections::HashSet, hash::Hash};
pub trait ViewFields: Clone + Copy + Hash + PartialEq + Eq {
fn as_str(&self) -> &'static str;
fn from_str(name: &str) -> Option<Self>;
fn from_str_iter<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<Self> {
names.into_iter().filter_map(Self::from_str).collect()
}
}
pub struct ViewContext<'v, T>
where
T: View,
{
inner: &'v T,
fields: HashSet<T::Fields>,
}
impl<'v, T> ViewContext<'v, T>
where
T: View,
{
pub fn with_fields<I>(mut self, fields: I) -> Self
where
I: IntoIterator<Item = T::Fields>,
{
self.fields = fields.into_iter().collect();
self
}
pub fn add_fields<I>(mut self, fields: I) -> Self
where
I: IntoIterator<Item = T::Fields>,
{
self.fields.extend(fields);
self
}
pub fn add_field(mut self, field: T::Fields) -> Self {
self.fields.insert(field);
self
}
}
pub trait View: Sized + serde::Serialize {
type Fields: ViewFields;
fn as_view(self: &Self) -> ViewContext<Self> {
ViewContext {
inner: self,
fields: Default::default(),
}
}
}