libikarus/objects/
entity_folder.rs1use crate::args::validate::{ObjectValidationError, StringValidationError};
2use crate::objects::entity::Entity;
3use crate::objects::entity::EntityScope;
4use crate::objects::folder::*;
5use crate::objects::id::TypedId;
6use crate::objects::object::*;
7use crate::objects::{ExpiredConnectionError, FromId, FromIdError, FromObjectScopeError};
8use crate::persistence::project::ProjectError;
9use crate::types::location::FolderPosition;
10use crate::{define_folder_funcs, define_object};
11use sqrite::connection::{Connection, DbError};
12use std::fmt::Formatter;
13use std::rc::Rc;
14use tracing::*;
15
16#[derive(thiserror::Error, Debug)]
17pub enum EntityFolderError {
18 #[error("error interfacing with project: {0}")]
19 ProjectError(#[from] ProjectError),
20 #[error("connection has expired")]
21 ExpiredConnection(#[from] ExpiredConnectionError),
22 #[error("error interfacing with database: {0}")]
23 DbError(#[from] DbError),
24 #[error("unable to validate object: {0}")]
25 ObjectValidationError(#[from] ObjectValidationError),
26 #[error("unable to validate string argument: {0}")]
27 StringValidationError(#[from] StringValidationError),
28 #[error("unable to interface with entity folder as object: {0}")]
29 ObjectError(#[from] ObjectHelperError),
30 #[error("unable to interface with entity folder as folder: {0}")]
31 FolderError(#[from] FolderHelperError),
32 #[error("entity folder cannot be constructed from {0}")]
33 InvalidFolder(Folder),
34 #[error("unable to interface with entity folder as entity object: {0}")]
35 EntityObjectError(#[from] EntityObjectError),
36 #[error("invalid scope: {0}")]
37 InvalidScope(#[from] FromObjectScopeError),
38}
39
40#[derive(thiserror::Error, Debug)]
41pub enum EntityObjectError {
42 #[error("entity object cannot be constructed from id {0}")]
43 InvalidId(TypedId),
44}
45
46#[derive(Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
47pub enum EntityObject {
48 Entity(Entity),
49 Folder(EntityFolder),
50}
51
52impl std::fmt::Display for EntityObject {
53 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54 write!(f, "{:?}", self)
55 }
56}
57
58impl FromId for EntityObject {
59 fn from_id(conn: Rc<Connection>, id: TypedId) -> Result<Self, FromIdError>
60 where
61 Self: Sized,
62 {
63 match id.get_object_type() {
64 ObjectType::Entity => Ok(EntityObject::Entity(Entity {
65 id,
66 conn: Rc::downgrade(&conn),
67 })),
68 ObjectType::EntityFolder => Ok(EntityObject::Folder(EntityFolder {
69 id,
70 conn: Rc::downgrade(&conn),
71 })),
72 _ => Err(FromIdError::InvalidId("entity object", id)),
73 }
74 }
75}
76
77define_object!(EntityFolder, EntityFolder, EntityScope, EntityFolderError);
78define_folder_funcs!(
79 EntityFolder,
80 EntityObject,
81 Value ObjectScope::Entities,
82 EntityFolderError
83);