Skip to main content

armour_core/
record_status.rs

1use rapira::{FromU8, Rapira};
2use serde::{Deserialize, Serialize};
3use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes};
4
5use crate::{
6    get_type::GetType,
7    typ::{SimpleEnumType, Typ},
8};
9
10/// Active: 0, Disabled: 1, Deleted: 255
11/// deleted - навсегда, disabled - можно потом восстановить.
12#[derive(
13    PartialOrd,
14    Ord,
15    Eq,
16    PartialEq,
17    Clone,
18    Copy,
19    Hash,
20    Serialize,
21    Deserialize,
22    Debug,
23    FromU8,
24    Default,
25    Rapira,
26    IntoBytes,
27    TryFromBytes,
28    Immutable,
29    KnownLayout,
30)]
31#[cfg_attr(
32    feature = "rkyv",
33    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
34)]
35#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
36#[cfg_attr(feature = "ts-rs", ts(export, export_to = "record_status.ts"))]
37#[repr(u8)]
38pub enum RecordStatus {
39    /// 0
40    #[default]
41    Active = 0,
42    /// 1
43    Disabled = 1,
44    /// 255
45    Deleted = 255,
46}
47
48impl RecordStatus {
49    pub const DELETED: u8 = Self::Deleted as u8;
50    pub const ACTIVE: u8 = Self::Active as u8;
51
52    /// `[255u8]`
53    pub const DELETED_PREFIX: [u8; 1] = [Self::DELETED];
54    /// `[0u8]`
55    pub const ACTIVE_PREFIX: [u8; 1] = [Self::ACTIVE];
56
57    pub fn is_active(&self) -> bool {
58        self == &Self::Active
59    }
60}
61
62impl GetType for RecordStatus {
63    const TYPE: Typ = Typ::SimpleEnum(SimpleEnumType {
64        name: "RecordStatus",
65        variants: &[(0, "Active"), (1, "Disabled"), (255, "Deleted")],
66    });
67}
68
69impl PartialEq<RecordStatus> for u8 {
70    fn eq(&self, other: &RecordStatus) -> bool {
71        *self == *other as u8
72    }
73}