hgame 0.26.4

CG production management structs, e.g. of assets, personnels, progress, etc.
Documentation
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// Contains all actions for which authorization is required within the app.
pub enum Action {
    ProjMetadata(ProjMetadataPerm),

    #[cfg(feature = "stage_graph")]
    ProjWorkflow(ProjWorkflowPerm),

    Brief(BriefPerm),

    Staff(StaffPerm),

    Asset(AssetPerm),

    #[cfg(feature = "ticket")]
    Defect(DefectTrackPerm),

    #[cfg(feature = "ticket")]
    SubDefect(DefectItemPerm),

    Review(ProgressPerm),

    Status(StatusActionPerm),

    #[cfg(feature = "stage_graph")]
    Stage(StageActionPerm),

    #[cfg(feature = "query_message")]
    Qms(QueryMsgPerm),

    #[cfg(feature = "review_item")]
    Notice(NoticeSignalPerm),
}

/// Something that checks the given "permission sum" against its enum variants.
pub trait Pass<T> {
    /// Given an integer, performs bitwise AND operator to yield
    // whether the given action is authorized or not.
    fn authorized(perm: &u16, action: &T) -> bool;
}

#[allow(non_camel_case_types)]
pub trait Pass_u8<T> {
    /// Given an integer, performs bitwise AND operator to yield
    // whether the given action is authorized or not.
    fn authorized(perm: &u8, action: &T) -> bool;
}

// ----------------------------------------------------------------------------
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs metadata-related actions of a project.
pub enum ProjMetadataPerm {
    #[default]
    ViewLink = 2_u16.pow(0),
    CreateLink = 2_u16.pow(1),
    EditLink = 2_u16.pow(2),
    DeleteLink = 2_u16.pow(3),

    ViewCriterion = 2_u16.pow(4),
    CreateCriterion = 2_u16.pow(5),
    EditCriterion = 2_u16.pow(6),
    AssignCriterion = 2_u16.pow(7),
    DeleteCriterion = 2_u16.pow(8),
    ObliterateCriteria = 2_u16.pow(9),

    ViewProjThumb = 2_u16.pow(10),
    EditProjThumb = 2_u16.pow(11),
    DeleteProjThumb = 2_u16.pow(12),
}

impl Pass<ProjMetadataPerm> for ProjMetadataPerm {
    fn authorized(perm: &u16, action: &ProjMetadataPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(feature = "stage_graph")]
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs metadata-related actions of a project.
pub enum ProjWorkflowPerm {
    #[default]
    ViewWorkflow = 2_u16.pow(0),
    CreateWorkflow = 2_u16.pow(1),
    EditWorkflow = 2_u16.pow(2),
    DeleteWorkflow = 2_u16.pow(3),
    MakeWorkflowDefault = 2_u16.pow(4),
}

#[cfg(feature = "stage_graph")]
impl Pass<ProjWorkflowPerm> for ProjWorkflowPerm {
    fn authorized(perm: &u16, action: &ProjWorkflowPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs brief-related actions of a project or an asset.
pub enum BriefPerm {
    #[default]
    View = 2_u16.pow(0),
    Create = 2_u16.pow(1),
    Edit = 2_u16.pow(2),
    Delete = 2_u16.pow(3),
}

impl Pass<BriefPerm> for BriefPerm {
    fn authorized(perm: &u16, action: &BriefPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs staff-related actions of a project.
pub enum StaffPerm {
    #[default]
    View = 2_u16.pow(0),
    Create = 2_u16.pow(1),
    Edit = 2_u16.pow(2),
    Delete = 2_u16.pow(3),
    #[cfg(feature = "ldap")]
    LdapQuery = 2_u16.pow(4),
}

impl Pass<StaffPerm> for StaffPerm {
    fn authorized(perm: &u16, action: &StaffPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------

#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs `ProductionAsset`-related actions of a project.
pub enum AssetPerm {
    #[default]
    View = 2_u16.pow(0),
    Create = 2_u16.pow(1),
    Edit = 2_u16.pow(2),
    Delete = 2_u16.pow(3),
}

impl Pass<AssetPerm> for AssetPerm {
    fn authorized(perm: &u16, action: &AssetPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(feature = "ticket")]
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs "ticket"-related actions within Defect Tracking.
pub enum DefectTrackPerm {
    #[default]
    View = 2_u16.pow(0),
    Create = 2_u16.pow(1),
    Edit = 2_u16.pow(2),
    Delete = 2_u16.pow(3),
}

#[cfg(feature = "ticket")]
impl Pass<DefectTrackPerm> for DefectTrackPerm {
    fn authorized(perm: &u16, action: &DefectTrackPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(feature = "ticket")]
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs "subtickets"-related actions within a Defect Tracking ticket.
pub enum DefectItemPerm {
    // View,
    #[default]
    Create = 2_u16.pow(1),
    Edit = 2_u16.pow(2),
    Delete = 2_u16.pow(3),
    StatusOpen = 2_u16.pow(4),
    StatusAddressed = 2_u16.pow(5),
    StatusClosed = 2_u16.pow(6),
    // ViewReply,
    /// Includes permission to upload the images embedded within the reply.
    CreateReply = 2_u16.pow(8),
    EditReply = 2_u16.pow(9),
    DeleteReply = 2_u16.pow(10),
}

#[cfg(feature = "ticket")]
impl Pass<DefectItemPerm> for DefectItemPerm {
    fn authorized(perm: &u16, action: &DefectItemPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs WIP/Approval snapshots-related for Progress Review.
pub enum ProgressPerm {
    #[default]
    View = 2_u16.pow(0),
    Create = 2_u16.pow(1),
    // Edit,
    // Delete
}

impl Pass<ProgressPerm> for ProgressPerm {
    fn authorized(perm: &u16, action: &ProgressPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(feature = "query_message")]
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs QMS -- (Delayed) Query Message System -- related actions.
pub enum QueryMsgPerm {
    #[default]
    View = 2_u16.pow(0),
    /// Permission to send new message.
    Create = 2_u16.pow(1),
}

#[cfg(feature = "query_message")]
impl Pass<QueryMsgPerm> for QueryMsgPerm {
    fn authorized(perm: &u16, action: &QueryMsgPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(feature = "review_item")]
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs review items -- review `Notice`s -- related actions.
pub enum NoticeSignalPerm {
    #[default]
    ViewPanProject = 2_u16.pow(0),
    /// Permission to submit `Supervision::Wip`
    SignalWip = 2_u16.pow(1),
    /// Permission to submit `Supervision::Approval`
    SignalApproval = 2_u16.pow(2),
    /// Permission to change "viewed by" state of Art Director.
    AdReviewTarget = 2_u16.pow(3),
}

#[cfg(feature = "review_item")]
impl Pass<NoticeSignalPerm> for NoticeSignalPerm {
    fn authorized(perm: &u16, action: &NoticeSignalPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs WIP/Approval snapshots-related for Progress Review.
pub enum StatusActionPerm {
    #[default]
    Toggle = 2_u16.pow(0),
    AnnounceClientFeedback = 2_u16.pow(1),
    AnnounceAdFeedback = 2_u16.pow(2),
    AnnounceTeamLeadFeedback = 2_u16.pow(3),
    FeedbackApproved = 2_u16.pow(4),
    AdApproved = 2_u16.pow(5),
    ClientApproved = 2_u16.pow(6),
}

impl Pass<StatusActionPerm> for StatusActionPerm {
    fn authorized(perm: &u16, action: &StatusActionPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(feature = "stage_graph")]
#[repr(u16)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, strum::EnumIter)]
/// Governs WIP/Approval snapshots-related for Progress Review.
pub enum StageActionPerm {
    #[default]
    Toggle = 2_u16.pow(0),
}

#[cfg(feature = "stage_graph")]
impl Pass<StageActionPerm> for StageActionPerm {
    fn authorized(perm: &u16, action: &StageActionPerm) -> bool {
        (perm & (*action as u16)) != 0
    }
}

// ----------------------------------------------------------------------------
#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;
    #[test]
    fn bitwise_and() {
        // eprintln!("1 & 2 is {:?}", 1 & 2);
        // eprintln!("1 & 3 is {:?}", 1 & 3);
        // eprintln!("1 & 4 is {:?}", 1 & 4);
        // eprintln!("2 & 3 is {:?}", 2 & 3);
        // eprintln!("3 & 4 is {:?}", 3 & 4);
        // eprintln!("5 & 7 is {:?}", 5 & 7);
        // eprintln!("23 & 49 is {:?}", 23 & 49);
        assert_eq!(15 & 1, 1);
        #[cfg(feature = "ticket")]
        assert_eq!(
            DefectTrackPerm::authorized(&15, &DefectTrackPerm::View),
            true
        );
    }
}