libikarus 0.1.14

The core functionality of Ikarus wrapped neatly in a rust library
Documentation
use crate::args::validate::{ObjectValidationError, StringValidationError};
use crate::objects::blueprint::Blueprint;
use crate::objects::blueprint::BlueprintScope;
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 BlueprintFolderError {
    #[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 blueprint folder as object: {0}")]
    ObjectError(#[from] ObjectHelperError),
    #[error("unable to interface with blueprint folder as folder: {0}")]
    FolderError(#[from] FolderHelperError),
    #[error("blueprint folder cannot be constructed from {0}")]
    InvalidFolder(Folder),
    #[error("unable to interface with blueprint folder as blueprint object: {0}")]
    BlueprintObjectError(#[from] BlueprintObjectError),
    #[error("invalid scope: {0}")]
    InvalidScope(#[from] FromObjectScopeError),
}

#[derive(thiserror::Error, Debug)]
pub enum BlueprintObjectError {
    #[error("blueprint object cannot be constructed from id {0}")]
    InvalidId(TypedId),
}

#[derive(Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub enum BlueprintObject {
    Blueprint(Blueprint),
    Folder(BlueprintFolder),
}

impl std::fmt::Display for BlueprintObject {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl FromId for BlueprintObject {
    fn from_id(conn: Rc<Connection>, id: TypedId) -> Result<Self, FromIdError>
    where
        Self: Sized,
    {
        match id.get_object_type() {
            ObjectType::Blueprint => Ok(BlueprintObject::Blueprint(Blueprint {
                id,
                conn: Rc::downgrade(&conn),
            })),
            ObjectType::BlueprintFolder => Ok(BlueprintObject::Folder(BlueprintFolder {
                id,
                conn: Rc::downgrade(&conn),
            })),
            _ => Err(FromIdError::InvalidId("blueprint object", id)),
        }
    }
}

define_object!(
    BlueprintFolder,
    BlueprintFolder,
    BlueprintScope,
    BlueprintFolderError
);
define_folder_funcs!(
    BlueprintFolder,
    BlueprintObject,
    Value ObjectScope::Blueprints,
    BlueprintFolderError
);