use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{Deletable, Identifiable, Info, WithCode, WithEntity, WithName};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InfoWithEntity<E>
where
E: Clone,
{
#[serde(flatten)]
pub info: Info,
pub entity: Option<E>,
}
impl<E> InfoWithEntity<E>
where
E: Clone,
{
pub fn new(
id: Option<i64>,
code: String,
name: String,
delete_time: Option<DateTime<Utc>>,
entity: Option<E>,
) -> Self {
Self {
info: Info::new(id, code, name, delete_time),
entity,
}
}
}
impl<E> Default for InfoWithEntity<E>
where
E: Clone,
{
fn default() -> Self {
Self {
info: Info::default(),
entity: None,
}
}
}
impl<E> Identifiable for InfoWithEntity<E>
where
E: Clone,
{
fn id(&self) -> Option<i64> {
self.info.id()
}
fn set_id(&mut self, id: Option<i64>) {
self.info.set_id(id);
}
}
impl<E> WithCode for InfoWithEntity<E>
where
E: Clone,
{
fn code(&self) -> &str {
self.info.code()
}
fn set_code(&mut self, code: &str) {
self.info.set_code(code);
}
}
impl<E> WithName for InfoWithEntity<E>
where
E: Clone,
{
fn name(&self) -> &str {
self.info.name()
}
fn set_name(&mut self, name: &str) {
self.info.set_name(name);
}
}
impl<E> Deletable for InfoWithEntity<E>
where
E: Clone,
{
fn delete_time(&self) -> Option<DateTime<Utc>> {
self.info.delete_time()
}
fn set_delete_time(&mut self, time: Option<DateTime<Utc>>) {
self.info.set_delete_time(time);
}
}
impl<E> WithEntity<E> for InfoWithEntity<E>
where
E: Clone,
{
fn entity(&self) -> Option<&E> {
self.entity.as_ref()
}
fn set_entity(&mut self, entity: Option<E>) {
self.entity = entity;
}
}