From 8f72c6074bae7f3f5af50f78671be2ace1b62308 Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 19 Jul 2022 19:04:53 -0600 Subject: [PATCH] move ItemStateAction to its own file --- src/ship/items/actions.rs | 3 +- src/ship/items/itemstateaction.rs | 137 ++++++++++++++++++++++++++++++ src/ship/items/mod.rs | 6 +- src/ship/items/state.rs | 135 ----------------------------- 4 files changed, 141 insertions(+), 140 deletions(-) create mode 100644 src/ship/items/itemstateaction.rs diff --git a/src/ship/items/actions.rs b/src/ship/items/actions.rs index bc0180e..02e774d 100644 --- a/src/ship/items/actions.rs +++ b/src/ship/items/actions.rs @@ -8,8 +8,9 @@ use std::pin::Pin; use crate::ship::map::MapArea; use crate::entity::character::{CharacterEntity, CharacterEntityId}; use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction}; -use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, InventoryItem, AddItemResult, FloorItemDetail, +use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateError, FloorItem, InventoryItem, AddItemResult, FloorItemDetail, StackedItemDetail, BankItem, BankItemDetail, InventoryItemDetail, IndividualItemDetail}; +use crate::ship::items::itemstateaction::{ItemStateAction, ItemAction}; use crate::ship::items::apply_item::apply_item; use crate::entity::item::{ItemDetail, NewItemEntity, TradeId}; use crate::entity::item::tool::Tool; diff --git a/src/ship/items/itemstateaction.rs b/src/ship/items/itemstateaction.rs new file mode 100644 index 0000000..3b60548 --- /dev/null +++ b/src/ship/items/itemstateaction.rs @@ -0,0 +1,137 @@ +use std::future::Future; + +#[async_trait::async_trait] +pub trait ItemAction { + type Input; + type Output; + type Start; + type Error; + + async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error>; + async fn commit(&self, v: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error>; +} + + +pub struct ItemStateAction { + _t: std::marker::PhantomData, + _s: std::marker::PhantomData, + _e: std::marker::PhantomData, +} + +impl Default for ItemStateAction { + fn default() -> ItemStateAction { + ItemStateAction { + _t: std::marker::PhantomData, + _s: std::marker::PhantomData, + _e: std::marker::PhantomData, + } + } +} + +impl ItemStateAction +where + T: Send + Sync, + S: Send + Sync, + E: Send + Sync, +{ + pub fn act(self, f: F) -> ItemActionStage, F, Fut, S, E> + where + F: Fn(S, ()) -> Fut + Send + Sync, + Fut: Future> + Send + { + ItemActionStage { + _s: Default::default(), + _e: std::marker::PhantomData, + prev: self, + actionf: f, + } + } +} + +pub struct ItemActionStage +where + P: ItemAction, + F: Fn(S, P::Output) -> Fut + Send + Sync, + Fut: Future> + Send, +{ + _s: std::marker::PhantomData, + _e: std::marker::PhantomData, + prev: P, + actionf: F, +} + +#[async_trait::async_trait] +impl ItemAction for ItemActionStage +where + P: ItemAction + ItemAction + Send + Sync, + F: Fn(S, P::Output) -> Fut + Send + Sync, + Fut: Future> + Send, + S: Send + Sync, + P::Output: Send + Sync, + E: Send + Sync, + O: Send + Sync, + P::Error: Send + Sync, +{ + type Input = P::Output; + type Output = O; + type Start = S; + type Error = P::Error; + + async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error> { + (self.actionf)(s, i).await + } + + async fn commit(&self, i: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error> { + let (i, prev) = self.prev.commit(i).await?; + self.action(i, prev).await + } +} + +impl ItemActionStage +where + P: ItemAction + Send + Sync, + F: Fn(S, P::Output) -> Fut + Send + Sync, + Fut: Future> + Send, + S: Send + Sync, + P::Output: Send + Sync, + E: Send + Sync, + O: Send + Sync, + P::Error: Send + Sync, +{ + #[allow(clippy::type_complexity)] + pub fn act(self, g: G) -> ItemActionStage, G, GFut, S, E> + where + S: Send + Sync, + G: Fn(S, as ItemAction>::Output) -> GFut + Send + Sync, + GFut: Future> + Send, + O2: Send + Sync, + { + ItemActionStage { + _s: Default::default(), + _e: Default::default(), + prev: self, + actionf: g, + } + } +} + +#[async_trait::async_trait] +impl ItemAction for ItemStateAction +where + T: Send + Sync, + S: Send + Sync, + E: Send + Sync, +{ + type Input = T; + type Output = (); + type Start = T; + type Error = E; + + async fn action(&self, s: Self::Start, _i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error> { + Ok((s, ())) + } + + async fn commit(&self, i: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error> { + Ok((i, ())) + } +} diff --git a/src/ship/items/mod.rs b/src/ship/items/mod.rs index f2a8da1..16d1134 100644 --- a/src/ship/items/mod.rs +++ b/src/ship/items/mod.rs @@ -1,9 +1,7 @@ pub mod state; pub mod actions; pub mod apply_item; -use serde::{Serialize, Deserialize}; +pub mod itemstateaction; -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, derive_more::Display)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize, derive_more::Display)] pub struct ClientItemId(pub u32); - - diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index 4ff10d3..2d2afc4 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -84,141 +84,6 @@ pub enum FloorType { } -#[async_trait::async_trait] -pub trait ItemAction { - type Input; - type Output; - type Start; - type Error; - - async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error>; - async fn commit(&self, v: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error>; -} - - -pub struct ItemStateAction { - _t: std::marker::PhantomData, - _s: std::marker::PhantomData, - _e: std::marker::PhantomData, -} - -impl Default for ItemStateAction { - fn default() -> ItemStateAction { - ItemStateAction { - _t: std::marker::PhantomData, - _s: std::marker::PhantomData, - _e: std::marker::PhantomData, - } - } -} - -impl ItemStateAction -where - T: Send + Sync, - S: Send + Sync, - E: Send + Sync, -{ - pub fn act(self, f: F) -> ItemActionStage, F, Fut, S, E> - where - F: Fn(S, ()) -> Fut + Send + Sync, - Fut: Future> + Send - { - ItemActionStage { - _s: Default::default(), - _e: std::marker::PhantomData, - prev: self, - actionf: f, - } - } -} - -pub struct ItemActionStage -where - P: ItemAction, - F: Fn(S, P::Output) -> Fut + Send + Sync, - Fut: Future> + Send, -{ - _s: std::marker::PhantomData, - _e: std::marker::PhantomData, - prev: P, - actionf: F, -} - -#[async_trait::async_trait] -impl ItemAction for ItemActionStage -where - P: ItemAction + ItemAction + Send + Sync, - F: Fn(S, P::Output) -> Fut + Send + Sync, - Fut: Future> + Send, - S: Send + Sync, - P::Output: Send + Sync, - E: Send + Sync, - O: Send + Sync, - P::Error: Send + Sync, -{ - type Input = P::Output; - type Output = O; - type Start = S; - type Error = P::Error; - - async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error> { - (self.actionf)(s, i).await - } - - async fn commit(&self, i: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error> { - let (i, prev) = self.prev.commit(i).await?; - self.action(i, prev).await - } -} - -impl ItemActionStage -where - P: ItemAction + Send + Sync, - F: Fn(S, P::Output) -> Fut + Send + Sync, - Fut: Future> + Send, - S: Send + Sync, - P::Output: Send + Sync, - E: Send + Sync, - O: Send + Sync, - P::Error: Send + Sync, -{ - #[allow(clippy::type_complexity)] - pub fn act(self, g: G) -> ItemActionStage, G, GFut, S, E> - where - S: Send + Sync, - G: Fn(S, as ItemAction>::Output) -> GFut + Send + Sync, - GFut: Future> + Send, - O2: Send + Sync, - { - ItemActionStage { - _s: Default::default(), - _e: Default::default(), - prev: self, - actionf: g, - } - } -} - -#[async_trait::async_trait] -impl ItemAction for ItemStateAction -where - T: Send + Sync, - S: Send + Sync, - E: Send + Sync, -{ - type Input = T; - type Output = (); - type Start = T; - type Error = E; - - async fn action(&self, s: Self::Start, _i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error> { - Ok((s, ())) - } - - async fn commit(&self, i: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error> { - Ok((i, ())) - } -} #[derive(Clone, Debug)]