use std::borrow::Cow;
use crate::attributes::state::Names;
use crate::collection::Collection;
use crate::error::{OrmError, Result};
use crate::model::Model;
use crate::record::Record;
pub trait Condition<T> {
fn holds(self, subject: &T) -> bool;
}
impl<T> Condition<T> for bool {
fn holds(self, _subject: &T) -> bool {
self
}
}
impl<T, F: FnOnce(&T) -> bool> Condition<T> for F {
fn holds(self, subject: &T) -> bool {
self(subject)
}
}
fn names<N: Into<Cow<'static, str>>>(names: impl IntoIterator<Item = N>) -> Names {
names.into_iter().map(Into::into).collect()
}
fn merged(mut list: Names, more: Names) -> Names {
for name in more {
if !list.contains(&name) {
list.push(name);
}
}
list
}
fn defaults(names: &'static [&'static str]) -> Names {
names.iter().map(|name| Cow::Borrowed(*name)).collect()
}
impl<M: Model> Record<M> {
pub fn hidden(&self) -> Vec<&str> {
match &self.attributes.hidden {
Some(list) => list.iter().map(AsRef::as_ref).collect(),
None => M::meta().hidden_names().to_vec(),
}
}
pub fn visible(&self) -> Vec<&str> {
match &self.attributes.visible {
Some(list) => list.iter().map(AsRef::as_ref).collect(),
None => M::meta().visible_names().to_vec(),
}
}
pub fn appends(&self) -> Vec<&str> {
self.appended_names()
}
pub(crate) fn hidden_names(&self) -> Names {
self.attributes
.hidden
.clone()
.unwrap_or_else(|| defaults(M::meta().hidden_names()))
}
pub(crate) fn visible_names(&self) -> Names {
self.attributes
.visible
.clone()
.unwrap_or_else(|| defaults(M::meta().visible_names()))
}
pub(crate) fn appended_names(&self) -> Vec<&str> {
match &self.attributes.appends {
Some(list) => list.iter().map(AsRef::as_ref).collect(),
None => M::meta().appended_names().to_vec(),
}
}
pub fn make_visible<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let shown = self::names(names);
let hidden: Names = self
.hidden_names()
.into_iter()
.filter(|name| !shown.contains(name))
.collect();
self.attributes.hidden = Some(hidden);
let visible = self.visible_names();
if !visible.is_empty() {
self.attributes.visible = Some(merged(visible, shown));
}
self
}
pub fn make_visible_if<N: Into<Cow<'static, str>>>(
&mut self,
condition: impl Condition<Self>,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
if condition.holds(self) {
self.make_visible(names);
}
self
}
pub fn make_hidden<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let hidden = merged(self.hidden_names(), self::names(names));
self.attributes.hidden = Some(hidden);
self
}
pub fn make_hidden_if<N: Into<Cow<'static, str>>>(
&mut self,
condition: impl Condition<Self>,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
if condition.holds(self) {
self.make_hidden(names);
}
self
}
pub fn set_hidden<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
self.attributes.hidden = Some(self::names(names));
self
}
pub fn merge_hidden<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
self.make_hidden(names)
}
pub fn set_visible<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
self.attributes.visible = Some(self::names(names));
self
}
pub fn merge_visible<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let visible = merged(self.visible_names(), self::names(names));
self.attributes.visible = Some(visible);
self
}
pub fn append<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> Result<&mut Self> {
let appends = merged(
self.current_appends(),
checked_accessors::<M>(self::names(names))?,
);
self.attributes.appends = Some(appends);
Ok(self)
}
pub fn set_appends<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> Result<&mut Self> {
self.attributes.appends = Some(checked_accessors::<M>(self::names(names))?);
Ok(self)
}
pub fn merge_appends<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> Result<&mut Self> {
self.append(names)
}
pub fn without_appends(&mut self) -> &mut Self {
self.attributes.appends = Some(Vec::new());
self
}
pub fn has_appended(&self, name: &str) -> bool {
self.appended_names().contains(&name)
}
fn current_appends(&self) -> Names {
self.attributes
.appends
.clone()
.unwrap_or_else(|| defaults(M::meta().appended_names()))
}
}
fn checked_accessors<M: Model>(names: Names) -> Result<Names> {
let accessors = M::accessors();
for name in &names {
if !accessors
.iter()
.any(|accessor| accessor.name() == name.as_ref())
{
let known: Vec<&str> = accessors.iter().map(|accessor| accessor.name()).collect();
return Err(OrmError::InvalidQuery(format!(
"{} has no accessor named `{name}` to append ({})",
M::meta().name(),
if known.is_empty() {
"it has no #[accessors] block".to_owned()
} else {
format!("its accessors: {}", known.join(", "))
}
)));
}
}
Ok(names)
}
impl<M: Model> Collection<M> {
pub fn make_hidden<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let names = self::names(names);
for record in self.iter_mut() {
record.make_hidden(names.iter().cloned());
}
self
}
pub fn merge_hidden<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
self.make_hidden(names)
}
pub fn set_hidden<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let names = self::names(names);
for record in self.iter_mut() {
record.set_hidden(names.iter().cloned());
}
self
}
pub fn make_visible<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let names = self::names(names);
for record in self.iter_mut() {
record.make_visible(names.iter().cloned());
}
self
}
pub fn merge_visible<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let names = self::names(names);
for record in self.iter_mut() {
record.merge_visible(names.iter().cloned());
}
self
}
pub fn set_visible<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> &mut Self {
let names = self::names(names);
for record in self.iter_mut() {
record.set_visible(names.iter().cloned());
}
self
}
pub fn append<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> Result<&mut Self> {
let names = checked_accessors::<M>(self::names(names))?;
for record in self.iter_mut() {
record.append(names.iter().cloned())?;
}
Ok(self)
}
pub fn set_appends<N: Into<Cow<'static, str>>>(
&mut self,
names: impl IntoIterator<Item = N>,
) -> Result<&mut Self> {
let names = checked_accessors::<M>(self::names(names))?;
for record in self.iter_mut() {
record.set_appends(names.iter().cloned())?;
}
Ok(self)
}
pub fn without_appends(&mut self) -> &mut Self {
for record in self.iter_mut() {
record.without_appends();
}
self
}
}