|
| 1 | +use eventually::{aggregate, message}; |
| 2 | + |
| 3 | +pub type LightswitchId = String; |
| 4 | + |
| 5 | +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] |
| 6 | +pub enum LightswitchError { |
| 7 | + #[error("NotImplemented")] |
| 8 | + NotImplemented, |
| 9 | + #[error("Light switch is already on")] |
| 10 | + AlreadyOn, |
| 11 | + #[error("Light switch is already off")] |
| 12 | + AlreadyOff, |
| 13 | +} |
| 14 | + |
| 15 | +// events |
| 16 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 17 | +pub struct Installed { |
| 18 | + id: LightswitchId, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 22 | +pub struct SwitchedOn { |
| 23 | + id: LightswitchId, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 27 | +pub struct SwitchedOff { |
| 28 | + id: LightswitchId, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 32 | +pub enum LightswitchEvent { |
| 33 | + Installed(Installed), |
| 34 | + SwitchedOn(SwitchedOn), |
| 35 | + SwitchedOff(SwitchedOff), |
| 36 | +} |
| 37 | + |
| 38 | +impl message::Message for LightswitchEvent { |
| 39 | + fn name(&self) -> &'static str { |
| 40 | + match self { |
| 41 | + LightswitchEvent::SwitchedOn(_) => "SwitchedOn", |
| 42 | + LightswitchEvent::SwitchedOff(_) => "SwitchedOff", |
| 43 | + LightswitchEvent::Installed(_) => "Installed", |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// aggregate |
| 49 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 50 | +pub enum LightswitchState { |
| 51 | + On, |
| 52 | + Off, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Clone)] |
| 56 | +pub struct Lightswitch { |
| 57 | + id: LightswitchId, |
| 58 | + state: LightswitchState, |
| 59 | +} |
| 60 | + |
| 61 | +impl aggregate::Aggregate for Lightswitch { |
| 62 | + type Id = LightswitchId; |
| 63 | + type Event = LightswitchEvent; |
| 64 | + type Error = LightswitchError; |
| 65 | + |
| 66 | + fn type_name() -> &'static str { |
| 67 | + "Lightswitch" |
| 68 | + } |
| 69 | + |
| 70 | + fn aggregate_id(&self) -> &Self::Id { |
| 71 | + &self.id |
| 72 | + } |
| 73 | + |
| 74 | + fn apply(state: Option<Self>, event: Self::Event) -> Result<Self, Self::Error> { |
| 75 | + match state { |
| 76 | + None => match event { |
| 77 | + LightswitchEvent::Installed(installed) => Ok(Lightswitch { |
| 78 | + id: installed.id, |
| 79 | + state: LightswitchState::Off, |
| 80 | + }), |
| 81 | + LightswitchEvent::SwitchedOn(_) | LightswitchEvent::SwitchedOff(_) => { |
| 82 | + Err(LightswitchError::NotImplemented) |
| 83 | + }, |
| 84 | + }, |
| 85 | + Some(mut light_switch) => match event { |
| 86 | + LightswitchEvent::Installed(_) => Err(LightswitchError::NotImplemented), |
| 87 | + LightswitchEvent::SwitchedOn(_) => match light_switch.state { |
| 88 | + LightswitchState::On => Err(LightswitchError::AlreadyOn), |
| 89 | + LightswitchState::Off => { |
| 90 | + light_switch.state = LightswitchState::On; |
| 91 | + Ok(light_switch) |
| 92 | + }, |
| 93 | + }, |
| 94 | + LightswitchEvent::SwitchedOff(_) => match light_switch.state { |
| 95 | + LightswitchState::On => { |
| 96 | + light_switch.state = LightswitchState::Off; |
| 97 | + Ok(light_switch) |
| 98 | + }, |
| 99 | + LightswitchState::Off => Err(LightswitchError::AlreadyOff), |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// root |
| 107 | +#[derive(Debug, Clone)] |
| 108 | +pub struct LightswitchRoot(aggregate::Root<Lightswitch>); |
| 109 | + |
| 110 | +impl From<eventually::aggregate::Root<Lightswitch>> for LightswitchRoot { |
| 111 | + fn from(root: eventually::aggregate::Root<Lightswitch>) -> Self { |
| 112 | + Self(root) |
| 113 | + } |
| 114 | +} |
| 115 | +impl From<LightswitchRoot> for eventually::aggregate::Root<Lightswitch> { |
| 116 | + fn from(value: LightswitchRoot) -> Self { |
| 117 | + value.0 |
| 118 | + } |
| 119 | +} |
| 120 | +impl std::ops::Deref for LightswitchRoot { |
| 121 | + type Target = eventually::aggregate::Root<Lightswitch>; |
| 122 | + fn deref(&self) -> &Self::Target { |
| 123 | + &self.0 |
| 124 | + } |
| 125 | +} |
| 126 | +impl std::ops::DerefMut for LightswitchRoot { |
| 127 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 128 | + &mut self.0 |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl LightswitchRoot { |
| 133 | + pub fn install(id: LightswitchId) -> Result<Self, LightswitchError> { |
| 134 | + aggregate::Root::<Lightswitch>::record_new( |
| 135 | + LightswitchEvent::Installed(Installed { id }).into(), |
| 136 | + ) |
| 137 | + .map(Self) |
| 138 | + } |
| 139 | + pub fn turn_on(&mut self, id: LightswitchId) -> Result<(), LightswitchError> { |
| 140 | + if self.state == LightswitchState::On { |
| 141 | + return Err(LightswitchError::AlreadyOn); |
| 142 | + } |
| 143 | + |
| 144 | + self.record_that(LightswitchEvent::SwitchedOn(SwitchedOn { id }).into()) |
| 145 | + } |
| 146 | + pub fn turn_off(&mut self, id: LightswitchId) -> Result<(), LightswitchError> { |
| 147 | + if self.state == LightswitchState::Off { |
| 148 | + return Err(LightswitchError::AlreadyOff); |
| 149 | + } |
| 150 | + |
| 151 | + self.record_that(LightswitchEvent::SwitchedOff(SwitchedOff { id }).into()) |
| 152 | + } |
| 153 | + pub fn get_switch_state(&self) -> Result<LightswitchState, LightswitchError> { |
| 154 | + Ok(self.state.clone()) |
| 155 | + } |
| 156 | +} |
0 commit comments