use crate::args::validate::{ObjectValidationError, StringValidationError};
use crate::objects::folder::*;
use crate::objects::id::TypedId;
use crate::objects::object::*;
use crate::objects::property::Property;
use crate::objects::property::PropertyScope;
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 PropertyFolderError {
#[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 property folder as object: {0}")]
ObjectError(#[from] ObjectHelperError),
#[error("unable to interface with property folder as folder: {0}")]
FolderError(#[from] FolderHelperError),
#[error("property folder cannot be constructed from {0}")]
InvalidFolder(Folder),
#[error("unable to interface with property folder as property object: {0}")]
PropertyObjectError(#[from] PropertyObjectError),
#[error("invalid scope: {0}")]
InvalidScope(#[from] FromObjectScopeError),
}
#[derive(thiserror::Error, Debug)]
pub enum PropertyObjectError {
#[error("property object cannot be constructed from id {0}")]
InvalidId(TypedId),
}
#[derive(Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub enum PropertyObject {
Property(Property),
Folder(PropertyFolder),
}
impl std::fmt::Display for PropertyObject {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl FromId for PropertyObject {
fn from_id(conn: Rc<Connection>, id: TypedId) -> Result<Self, FromIdError>
where
Self: Sized,
{
match id.get_object_type() {
ObjectType::Property => Ok(PropertyObject::Property(Property {
id,
conn: Rc::downgrade(&conn),
})),
ObjectType::PropertyFolder => Ok(PropertyObject::Folder(PropertyFolder {
id,
conn: Rc::downgrade(&conn),
})),
_ => Err(FromIdError::InvalidId("property object", id)),
}
}
}
define_object!(
PropertyFolder,
PropertyFolder,
PropertyScope,
PropertyFolderError
);
define_folder_funcs!(
PropertyFolder,
PropertyObject,
Type PropertyScope,
PropertyFolderError
);