From 1d2946eec6f65a30611e66e7ae942da1a95da708 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 28 Nov 2020 00:39:34 +0000 Subject: [PATCH] Revert "wip split off daemon/, before revert" This isn't actually so easy. In particular PieceSpec will cause a lot of trouble. Also lots of dupes from filename completion. So undo it all. This reverts commit 7b6c42eb3e71ee3094009bf7c494ad4d68c88a23. Signed-off-by: Ian Jackson --- daemon/error.rs | 97 ------------------------------------ daemon/imports.rs | 27 ---------- daemon/main.rs | 2 +- {daemon => src}/accounts.rs | 25 +++++++--- {daemon => src}/api.rs | 0 src/commands.rs | 17 ------- src/error.rs | 91 +++++++++++++++++++++++++++++++++ {daemon => src}/gamestate.rs | 13 +++++ {daemon => src}/global.rs | 0 src/imports.rs | 10 ++++ src/lib.rs | 8 +++ src/spec.rs | 30 +++-------- {daemon => src}/sse.rs | 0 {daemon => src}/updates.rs | 0 14 files changed, 149 insertions(+), 171 deletions(-) delete mode 100644 daemon/error.rs delete mode 100644 daemon/imports.rs rename {daemon => src}/accounts.rs (97%) rename {daemon => src}/api.rs (100%) rename {daemon => src}/gamestate.rs (96%) rename {daemon => src}/global.rs (100%) rename {daemon => src}/sse.rs (100%) rename {daemon => src}/updates.rs (100%) diff --git a/daemon/error.rs b/daemon/error.rs deleted file mode 100644 index 24742b0c..00000000 --- a/daemon/error.rs +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2020 Ian Jackson -// SPDX-License-Identifier: AGPL-3.0-or-later -// There is NO WARRANTY. - -use crate::imports::*; - -#[derive(Error,Debug)] -pub enum OnlineError { - #[error("Game in process of being destroyed")] - GameBeingDestroyed, - #[error("client session not recognised (terminated by server?)")] - NoClient, - #[error("player not part of game (removed?)")] - NoPlayer(#[from] PlayerNotFound), - #[error("invalid Z coordinate")] - InvalidZCoord, - #[error("Server operational problems - consult administrator: {0:?}")] - ServerFailure(#[from] InternalError), - #[error("JSON deserialisation error: {0:?}")] - BadJSON(serde_json::Error), - #[error("referenced piece is gone (maybe race)")] - PieceGone, - #[error("improper piece hold status for op (maybe race)")] - PieceHeld, - #[error("improper UI operation")] - BadOperation, -} -from_instance_lock_error!{OnlineError} - -pub use OnlineError::{NoClient,NoPlayer}; - -#[derive(Error,Debug)] -pub enum InstanceLockError { - GameCorrupted, - GameBeingDestroyed, -} -#[macro_export] -macro_rules! from_instance_lock_error { - ($into:ident) => { - impl From for $into { - fn from(e: InstanceLockError) -> $into { - use InstanceLockError::*; - match e { - GameBeingDestroyed => $into::GameBeingDestroyed, - GameCorrupted => InternalError::GameCorrupted.into(), - } - } - } - } -} - -pub trait ById { - type Id; - type Entry; - type Error; - #[throws(Self::Error)] - fn byid(&self, t: Self::Id) -> &Self::Entry; - #[throws(Self::Error)] - fn byid_mut(&mut self, t: Self::Id) -> &mut Self::Entry; -} - -pub trait IdForById { - type Error; - #[allow(clippy::declare_interior_mutable_const)] -// https://github.com/rust-lang/rust-clippy/issues/3962#issuecomment-667957112 - const ERROR : Self::Error; -} - -macro_rules! some_slotmap { - ($slotmap:ident) => { - impl ById for $slotmap { - type Id = I; - type Entry = T; - type Error = ::Error; - fn byid (& self, t: Self::Id) -> Result<& T, Self::Error> { - self.get (t).ok_or(::ERROR) - } - fn byid_mut(&mut self, t: Self::Id) -> Result<&mut T, Self::Error> { - self.get_mut(t).ok_or(::ERROR) - } - } - } -} - -some_slotmap!{DenseSlotMap} -some_slotmap!{SecondarySlotMap} - -impl IdForById for T where T : AccessId { - type Error = T::Error; - const ERROR : Self::Error = ::ERROR; -} - -impl IdForById for PieceId { - type Error = OE; - const ERROR : OE = OE::PieceGone; -} - diff --git a/daemon/imports.rs b/daemon/imports.rs deleted file mode 100644 index 8711b25e..00000000 --- a/daemon/imports.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 Ian Jackson -// SPDX-License-Identifier: AGPL-3.0-or-later -// There is NO WARRANTY. - -use otter::imports::*; - -pub mod session; -pub mod updates; -pub mod sse; -pub mod api; -pub mod cmdlistener; -pub mod global; -pub mod accounts; -pub mod gamestate; - -pub use crate::gamestate::*; -pub use crate::updates::*; -pub use crate::sse; -pub use crate::cmdlistener::*; -pub use crate::global::*; -pub use crate::accounts::*; -pub use crate::accounts::loaded_acl::{self,LoadedAcl,EffectiveACL,PermSet}; - -pub use crate::api::{Lens,TransparentLens,ApiPieceOpError}; -pub use crate::api::{PresentationLayout,AbbrevPresentationLayout}; - -pub type OE = OnlineError; diff --git a/daemon/main.rs b/daemon/main.rs index 67403d55..57561c56 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -9,7 +9,7 @@ use rocket_contrib::serve::StaticFiles; use rocket::response::Content; use rocket::fairing; -pub mod imports; +use otter::imports::*; #[derive(Serialize,Debug)] struct FrontPageRenderContext { } diff --git a/daemon/accounts.rs b/src/accounts.rs similarity index 97% rename from daemon/accounts.rs rename to src/accounts.rs index c2367a24..789d019b 100644 --- a/daemon/accounts.rs +++ b/src/accounts.rs @@ -8,10 +8,29 @@ use parking_lot::{Mutex, const_mutex, MutexGuard}; //---------- simple types ---------- +slotmap::new_key_type!{ + pub struct AccountId; +} + +#[derive(Debug,Clone,Deserialize,Serialize)] +#[derive(Eq,PartialEq,Ord,PartialOrd,Hash)] +pub enum AccountScope { + Server, + Unix { user : String }, +} + type AS = AccountScope; type ME = MgmtError; type IE = InternalError; +#[derive(Debug,Clone)] +#[derive(Eq,PartialEq,Ord,PartialOrd,Hash)] +#[derive(DeserializeFromStr,SerializeDisplay)] +pub struct AccountName { + pub scope: AccountScope, + pub subaccount: String, +} + /// Record of acess for a player. Newtype prevents mutable access /// without invalidating old tokens and permissions check. #[derive(Serialize,Deserialize,Debug)] @@ -556,10 +575,4 @@ pub mod loaded_acl { ).collect() } } } - - impl Perm for spec::TablePermission { - type Auth = InstanceName; - const TEST_EXISTENCE : Self = TablePermission::TestExistence; - const NOT_FOUND : MgmtError = MgmtError::GameNotFound; - } } diff --git a/daemon/api.rs b/src/api.rs similarity index 100% rename from daemon/api.rs rename to src/api.rs diff --git a/src/commands.rs b/src/commands.rs index 3a1a6dee..1e08f922 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4,23 +4,6 @@ use crate::imports::*; -//---------- basic types ---------- - -visible_slotmap_key!{ PlayerId('#') } - -slotmap::new_key_type!{ - pub struct PieceId; -} - -#[derive(Copy,Clone,Debug,Ord,PartialOrd,Eq,PartialEq)] -#[derive(Serialize,Deserialize)] -#[serde(transparent)] -pub struct Generation (pub u64); - -visible_slotmap_key!{ VisiblePieceId('.') } - -//---------- command ---------- - #[derive(Debug,Serialize,Deserialize)] pub enum MgmtCommand { Noop, diff --git a/src/error.rs b/src/error.rs index cb2cb2b0..47ccdf98 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,6 +6,29 @@ use crate::imports::*; type IE = InternalError; +#[derive(Error,Debug)] +pub enum OnlineError { + #[error("Game in process of being destroyed")] + GameBeingDestroyed, + #[error("client session not recognised (terminated by server?)")] + NoClient, + #[error("player not part of game (removed?)")] + NoPlayer(#[from] PlayerNotFound), + #[error("invalid Z coordinate")] + InvalidZCoord, + #[error("Server operational problems - consult administrator: {0:?}")] + ServerFailure(#[from] InternalError), + #[error("JSON deserialisation error: {0:?}")] + BadJSON(serde_json::Error), + #[error("referenced piece is gone (maybe race)")] + PieceGone, + #[error("improper piece hold status for op (maybe race)")] + PieceHeld, + #[error("improper UI operation")] + BadOperation, +} +from_instance_lock_error!{OnlineError} + #[derive(Error,Debug)] pub enum InternalError { #[error("Game corrupted by previous crash")] @@ -63,6 +86,74 @@ display_as_debug!{PieceOpError} pub type StartupError = anyhow::Error; +pub use OnlineError::{NoClient,NoPlayer}; + +#[derive(Error,Debug)] +pub enum InstanceLockError { + GameCorrupted, + GameBeingDestroyed, +} +#[macro_export] +macro_rules! from_instance_lock_error { + ($into:ident) => { + impl From for $into { + fn from(e: InstanceLockError) -> $into { + use InstanceLockError::*; + match e { + GameBeingDestroyed => $into::GameBeingDestroyed, + GameCorrupted => InternalError::GameCorrupted.into(), + } + } + } + } +} + +pub trait ById { + type Id; + type Entry; + type Error; + #[throws(Self::Error)] + fn byid(&self, t: Self::Id) -> &Self::Entry; + #[throws(Self::Error)] + fn byid_mut(&mut self, t: Self::Id) -> &mut Self::Entry; +} + +pub trait IdForById { + type Error; + #[allow(clippy::declare_interior_mutable_const)] +// https://github.com/rust-lang/rust-clippy/issues/3962#issuecomment-667957112 + const ERROR : Self::Error; +} + +macro_rules! some_slotmap { + ($slotmap:ident) => { + impl ById for $slotmap { + type Id = I; + type Entry = T; + type Error = ::Error; + fn byid (& self, t: Self::Id) -> Result<& T, Self::Error> { + self.get (t).ok_or(::ERROR) + } + fn byid_mut(&mut self, t: Self::Id) -> Result<&mut T, Self::Error> { + self.get_mut(t).ok_or(::ERROR) + } + } + } +} + +some_slotmap!{DenseSlotMap} +some_slotmap!{SecondarySlotMap} + +impl IdForById for T where T : AccessId { + type Error = T::Error; + const ERROR : Self::Error = ::ERROR; +} + +impl IdForById for PieceId { + type Error = OE; + const ERROR : OE = OE::PieceGone; +} + #[macro_export] macro_rules! display_as_debug { {$x:ty} => { diff --git a/daemon/gamestate.rs b/src/gamestate.rs similarity index 96% rename from daemon/gamestate.rs rename to src/gamestate.rs index 0bd7c775..dcab0298 100644 --- a/daemon/gamestate.rs +++ b/src/gamestate.rs @@ -6,6 +6,19 @@ use crate::imports::*; // ---------- newtypes and type aliases ---------- +visible_slotmap_key!{ PlayerId('#') } + +slotmap::new_key_type!{ + pub struct PieceId; +} + +#[derive(Copy,Clone,Debug,Ord,PartialOrd,Eq,PartialEq)] +#[derive(Serialize,Deserialize)] +#[serde(transparent)] +pub struct Generation (pub u64); + +visible_slotmap_key!{ VisiblePieceId('.') } + #[derive(Clone,Serialize,Deserialize,Hash,Eq,Ord,PartialEq,PartialOrd)] #[serde(transparent)] pub struct Html (pub String); diff --git a/daemon/global.rs b/src/global.rs similarity index 100% rename from daemon/global.rs rename to src/global.rs diff --git a/src/imports.rs b/src/imports.rs index 8e6f1cc0..2332c73d 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -87,18 +87,27 @@ pub use vecdeque_stableix::Deque as StableIndexVecDeque; pub use zcoord::{self, ZCoord}; +pub use crate::global::*; +pub use crate::gamestate::*; pub use crate::pieces::*; pub use crate::keydata::*; +pub use crate::updates::*; +pub use crate::sse; pub use crate::error::*; pub use crate::commands::*; pub use crate::slotmap_slot_idx::*; +pub use crate::cmdlistener::*; pub use crate::mgmtchannel::*; +pub use crate::api::{Lens,TransparentLens,ApiPieceOpError}; +pub use crate::api::{PresentationLayout,AbbrevPresentationLayout}; pub use crate::utils::*; pub use crate::spec::*; pub use crate::debugreader::DebugReader; pub use crate::shapelib; pub use crate::tz::*; pub use crate::config::*; +pub use crate::accounts::*; +pub use crate::accounts::loaded_acl::{self,LoadedAcl,EffectiveACL,PermSet}; pub use crate::toml_de; pub type SecondarySlotMap = slotmap::secondary::SecondaryMap; @@ -112,3 +121,4 @@ pub enum Impossible { } display_as_debug!(Impossible); pub type AE = anyhow::Error; +pub type OE = OnlineError; diff --git a/src/lib.rs b/src/lib.rs index 3e2fb15e..b8f51b70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,16 +8,24 @@ #![allow(clippy::redundant_closure_call)] pub mod imports; +pub mod global; pub mod pieces; +pub mod gamestate; pub mod keydata; +pub mod updates; +pub mod sse; pub mod error; +pub mod session; +pub mod api; pub mod spec; +pub mod cmdlistener; pub mod commands; pub mod utils; pub mod mgmtchannel; pub mod debugreader; pub mod shapelib; pub mod tz; +pub mod accounts; pub mod config; #[path="toml-de.rs"] pub mod toml_de; #[path="slotmap-slot-idx.rs"] pub mod slotmap_slot_idx; diff --git a/src/spec.rs b/src/spec.rs index 185028c2..98e2a5e0 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -5,8 +5,6 @@ // game specs use serde::{Serialize,Deserialize}; -use serde_with::DeserializeFromStr; -use serde_with::SerializeDisplay; use fehler::throws; use index_vec::{define_index_type,IndexVec}; use crate::gamestate::PieceSpec; @@ -14,6 +12,7 @@ use std::fmt::Debug; use std::collections::hash_set::HashSet; use thiserror::Error; use crate::error::display_as_debug; +use crate::accounts::AccountName; use std::hash::Hash; use num_derive::{ToPrimitive, FromPrimitive}; @@ -62,27 +61,6 @@ pub enum SpecError { } display_as_debug!{SpecError} -//---------- Accounts ---------- - -slotmap::new_key_type!{ - pub struct AccountId; -} - -#[derive(Debug,Clone,Deserialize,Serialize)] -#[derive(Eq,PartialEq,Ord,PartialOrd,Hash)] -pub enum AccountScope { - Server, - Unix { user : String }, -} - -#[derive(Debug,Clone)] -#[derive(Eq,PartialEq,Ord,PartialOrd,Hash)] -#[derive(DeserializeFromStr,SerializeDisplay)] -pub struct AccountName { - pub scope: AccountScope, - pub subaccount: String, -} - //---------- Table TOML file ---------- #[derive(Debug,Serialize,Deserialize)] @@ -297,6 +275,12 @@ pub mod implementation { } } + impl loaded_acl::Perm for TablePermission { + type Auth = InstanceName; + const TEST_EXISTENCE : Self = TablePermission::TestExistence; + const NOT_FOUND : MgmtError = MgmtError::GameNotFound; + } + impl TablePlayerSpec { pub fn account_glob(&self) -> String { fn scope_glob(scope: AccountScope) -> String { diff --git a/daemon/sse.rs b/src/sse.rs similarity index 100% rename from daemon/sse.rs rename to src/sse.rs diff --git a/daemon/updates.rs b/src/updates.rs similarity index 100% rename from daemon/updates.rs rename to src/updates.rs -- 2.30.2