use crate::args::validate::{ObjectValidationError, StringValidationError};
use crate::objects::entity::Entity;
use crate::objects::entity::EntityScope;
use crate::objects::folder::*;
use crate::objects::id::TypedId;
use crate::objects::object::*;
use crate::objects::{ExpiredConnectionError, FromId, FromIdError, FromObjectScopeError};
use crate::persistence::project::ProjectError;
use crate::types::location::FolderPosition;
use crate::{define_folder_funcs, define_object};
use sqrite::connection::{Connection, DbError};
use std::fmt::Formatter;
use std::rc::Rc;
use tracing::*;
#[derive(thiserror::Error, Debug)]
pub enum EntityFolderError {
#[error("error interfacing with project: {0}")]
ProjectError(#[from] ProjectError),
#[error("connection has expired")]
ExpiredConnection(#[from] ExpiredConnectionError),
#[error("error interfacing with database: {0}")]
DbError(#[from] DbError),
#[error("unable to validate object: {0}")]
ObjectValidationError(#[from] ObjectValidationError),
#[error("unable to validate string argument: {0}")]
StringValidationError(#[from] StringValidationError),
#[error("unable to interface with entity folder as object: {0}")]
ObjectError(#[from] ObjectHelperError),
#[error("unable to interface with entity folder as folder: {0}")]
FolderError(#[from] FolderHelperError),
#[error("entity folder cannot be constructed from {0}")]
InvalidFolder(Folder),
#[error("unable to interface with entity folder as entity object: {0}")]
EntityObjectError(#[from] EntityObjectError),
#[error("invalid scope: {0}")]
InvalidScope(#[from] FromObjectScopeError),
}
#[derive(thiserror::Error, Debug)]
pub enum EntityObjectError {
#[error("entity object cannot be constructed from id {0}")]
InvalidId(TypedId),
}
#[derive(Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub enum EntityObject {
Entity(Entity),
Folder(EntityFolder),
}
impl std::fmt::Display for EntityObject {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl FromId for EntityObject {
fn from_id(conn: Rc<Connection>, id: TypedId) -> Result<Self, FromIdError>
where
Self: Sized,
{
match id.get_object_type() {
ObjectType::Entity => Ok(EntityObject::Entity(Entity {
id,
conn: Rc::downgrade(&conn),
})),
ObjectType::EntityFolder => Ok(EntityObject::Folder(EntityFolder {
id,
conn: Rc::downgrade(&conn),
})),
_ => Err(FromIdError::InvalidId("entity object", id)),
}
}
}
define_object!(EntityFolder, EntityFolder, EntityScope, EntityFolderError);
define_folder_funcs!(
EntityFolder,
EntityObject,
Value ObjectScope::Entities,
EntityFolderError
);