use std::borrow::Cow;
use std::marker::PhantomData;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use serde::{Serialize, Serializer};
use crate::column::{Column, ColumnName};
use crate::factory::faker::Faker;
use crate::factory::sources::{ParentSource, ResolveParent};
use crate::model::Model;
use crate::value::{Field, Value, ValueFor, to_json};
pub struct FactoryContext<'a, M: Model> {
index: usize,
count: usize,
now: DateTime<Utc>,
faker: &'a mut Faker,
parents: &'a mut Vec<PendingParent>,
_model: PhantomData<fn() -> M>,
}
impl<'a, M: Model> FactoryContext<'a, M> {
pub(crate) fn new(
index: usize,
count: usize,
now: DateTime<Utc>,
faker: &'a mut Faker,
parents: &'a mut Vec<PendingParent>,
) -> Self {
FactoryContext {
index,
count,
now,
faker,
parents,
_model: PhantomData,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn count(&self) -> usize {
self.count
}
pub fn fake(&mut self) -> &mut Faker {
self.faker
}
pub fn now(&self) -> DateTime<Utc> {
self.now
}
pub fn belongs_to<T, S>(&mut self, column: Column<M, T>, parent: S) -> T
where
T: Field + Default,
S: ParentSource,
<S::Parent as Model>::Key: ValueFor<T>,
{
let empty = T::default();
let placeholder = column
.encode(&empty)
.unwrap_or_else(|_| <T as Field>::null());
let name: Cow<'static, str> = Cow::Owned(column.column_name().into_owned());
self.parents.retain(|pending| pending.column != name);
self.parents.push(PendingParent {
column: name,
placeholder,
source: parent.into_parent().0,
});
empty
}
}
pub(crate) struct PendingParent {
pub(crate) column: Cow<'static, str>,
pub(crate) placeholder: Value,
pub(crate) source: Arc<dyn ResolveParent>,
}
impl PendingParent {
pub(crate) fn is_open<M: Model>(&self, model: &M) -> crate::Result<bool> {
Ok(match model.value(&self.column)? {
Some(value) => crate::value::same_value(&value, &self.placeholder),
None => false,
})
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Attributes {
values: Vec<(&'static str, Value)>,
}
impl Attributes {
pub fn get(&self, column: &str) -> Option<&Value> {
self.values
.iter()
.find(|(name, _)| *name == column)
.map(|(_, value)| value)
}
pub fn iter(&self) -> impl Iterator<Item = &(&'static str, Value)> {
self.values.iter()
}
pub fn len(&self) -> usize {
self.values.len()
}
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
pub fn into_vec(self) -> Vec<(&'static str, Value)> {
self.values
}
pub fn to_json(&self) -> serde_json::Value {
serde_json::Value::Object(
self.values
.iter()
.map(|(column, value)| ((*column).to_owned(), to_json(value)))
.collect(),
)
}
}
impl From<Vec<(&'static str, Value)>> for Attributes {
fn from(values: Vec<(&'static str, Value)>) -> Self {
Attributes { values }
}
}
impl IntoIterator for Attributes {
type Item = (&'static str, Value);
type IntoIter = std::vec::IntoIter<(&'static str, Value)>;
fn into_iter(self) -> Self::IntoIter {
self.values.into_iter()
}
}
impl Serialize for Attributes {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.to_json().serialize(serializer)
}
}