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