diff --git a/visions/server/src/asset_db.rs b/visions/server/src/asset_db.rs index 23a7a4d..d32fa82 100644 --- a/visions/server/src/asset_db.rs +++ b/visions/server/src/asset_db.rs @@ -5,6 +5,7 @@ use std::{ }; use mime::Mime; +use serde::{Deserialize, Serialize}; use thiserror::Error; #[derive(Debug, Error)] @@ -30,7 +31,7 @@ impl From for Error { } } -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub struct AssetId(String); impl Display for AssetId { diff --git a/visions/server/src/core.rs b/visions/server/src/core.rs index aac0c15..d5bcaf0 100644 --- a/visions/server/src/core.rs +++ b/visions/server/src/core.rs @@ -15,6 +15,12 @@ use crate::{ types::{AppError, Message, Tabletop, RGB}, }; +const DEFAULT_BACKGROUND_COLOR: RGB = RGB { + red: 0xca, + green: 0xb9, + blue: 0xbb, +}; + #[derive(Debug)] struct WebsocketClient { sender: Option>, @@ -39,11 +45,7 @@ impl Core { asset_db: Box::new(assetdb), clients: HashMap::new(), tabletop: Tabletop { - background_color: RGB { - red: 0xca, - green: 0xb9, - blue: 0xbb, - }, + background_color: DEFAULT_BACKGROUND_COLOR, background_image: None, }, }))) @@ -112,10 +114,10 @@ impl Core { .collect() } - pub fn set_background_image(&self, path: String) -> Result<(), AppError> { + pub fn set_background_image(&self, asset: AssetId) -> Result<(), AppError> { let tabletop = { let mut state = self.0.write().unwrap(); - state.tabletop.background_image = Some(path.clone()); + state.tabletop.background_image = Some(asset.clone()); state.tabletop.clone() }; self.publish(Message::UpdateTabletop(tabletop)); @@ -187,4 +189,23 @@ mod test { assert_eq!(data, "abcdefg".as_bytes()); }); } + + #[tokio::test] + async fn it_can_retrieve_the_default_tabletop() { + let core = test_core(); + assert_matches!(core.tabletop(), Tabletop{ background_color, background_image } => { + assert_eq!(background_color, DEFAULT_BACKGROUND_COLOR); + assert_eq!(background_image, None); + }); + } + + #[tokio::test] + async fn it_can_change_the_tabletop_background() { + let core = test_core(); + assert_matches!(core.set_background_image(AssetId::from("asset_1")), Ok(())); + assert_matches!(core.tabletop(), Tabletop{ background_color, background_image } => { + assert_eq!(background_color, DEFAULT_BACKGROUND_COLOR); + assert_eq!(background_image, Some(AssetId::from("asset_1"))); + }); + } } diff --git a/visions/server/src/handlers.rs b/visions/server/src/handlers.rs index bf94976..ae31f61 100644 --- a/visions/server/src/handlers.rs +++ b/visions/server/src/handlers.rs @@ -143,7 +143,7 @@ pub async fn handle_connect_websocket( pub async fn handle_set_background_image(core: Core, image_name: String) -> impl Reply { handler(async move { - let _ = core.set_background_image(image_name); + let _ = core.set_background_image(AssetId::from(image_name)); Ok(Response::builder() .header("Access-Control-Allow-Origin", "*") diff --git a/visions/server/src/types.rs b/visions/server/src/types.rs index f47a25b..f5c5307 100644 --- a/visions/server/src/types.rs +++ b/visions/server/src/types.rs @@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize}; use typeshare::typeshare; +use crate::asset_db::AssetId; + #[derive(Debug)] pub enum AppError { NotFound(String), @@ -10,7 +12,7 @@ pub enum AppError { UnexpectedError(String), } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] #[typeshare] pub struct RGB { @@ -24,7 +26,7 @@ pub struct RGB { #[typeshare] pub struct Tabletop { pub background_color: RGB, - pub background_image: Option, + pub background_image: Option, } #[derive(Clone, Debug, Deserialize, Serialize)]