source2_demo/entity/
mod.rs1mod baseline;
2mod class;
3mod container;
4
5pub(crate) use baseline::*;
6pub(crate) mod field;
7pub use class::*;
8pub use container::*;
9
10use crate::error::EntityError;
11use crate::field::{FieldPath, FieldState};
12use crate::FieldValue;
13use std::rc::Rc;
14
15#[derive(Debug, Clone, Copy, Eq, PartialEq)]
16pub enum EntityEvents {
17 Created,
18 Updated,
19 Deleted,
20}
21
22impl EntityEvents {
23 #[inline]
24 pub(crate) fn from_cmd(cmd: u32) -> Self {
25 match cmd {
26 0 => EntityEvents::Updated,
27 2 => EntityEvents::Created,
28 3 => EntityEvents::Deleted,
29 _ => unreachable!(),
30 }
31 }
32}
33
34#[derive(Clone)]
35pub struct Entity {
36 pub(crate) index: u32,
37 pub(crate) serial: u32,
38 pub(crate) class: Rc<Class>,
39 pub(crate) state: FieldState,
40}
41
42impl Default for Entity {
43 fn default() -> Self {
44 Entity {
45 index: u32::MAX,
46 serial: 0,
47 class: Class::default().into(),
48 state: FieldState::default(),
49 }
50 }
51}
52
53impl Entity {
54 pub fn index(&self) -> u32 {
55 self.index
56 }
57
58 pub fn serial(&self) -> u32 {
59 self.serial
60 }
61
62 pub fn handle(&self) -> u32 {
63 self.serial << 14 | self.index
64 }
65
66 pub fn class(&self) -> &Class {
67 &self.class
68 }
69
70 pub fn get_property_by_name(&self, name: &str) -> Result<&FieldValue, EntityError> {
104 self.get_property_by_field_path(&self.class.serializer.get_field_path_for_name(name)?)
105 }
106
107 pub(crate) fn get_property_by_field_path(
108 &self,
109 fp: &FieldPath,
110 ) -> Result<&FieldValue, EntityError> {
111 self.state.get_value(fp).ok_or_else(|| {
112 EntityError::PropertyNameNotFound(
113 self.class.serializer.get_name_for_field_path(fp),
114 self.class.name().to_string(),
115 format!("{}", fp),
116 )
117 })
118 }
119}