pub static CLIENT_NAME: &str = "cart-manager"; pub enum Topic {} #[derive(Debug, Clone, thiserror::Error, serde::Serialize, serde::Deserialize)] pub enum Error { #[error("Internal server error")] InternalServerError, #[error("Failed to load account shopping carts")] NoCarts, #[error("Account does not have active shopping cart")] NoActiveCart, #[error("Failed to delete item {0:?}")] DeleteItem(model::ShoppingCartItemId), #[error("Failed to modify item {0:?}")] ModifyItem(model::ShoppingCartItemId), #[error("Failed to create item")] CreateItem, #[error("Failed to modify cart {0:?}")] ModifyCart(model::ShoppingCartId), #[error("Failed to load cart {0:?} items")] LoadItems(model::ShoppingCartId), } pub mod remove_product { use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub shopping_cart_id: model::ShoppingCartId, pub shopping_cart_item_id: model::ShoppingCartItemId, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Output { pub item: Option, pub error: Option, } impl Output { pub fn error(error: Error) -> Self { Self { error: Some(error), ..Default::default() } } pub fn item(item: model::ShoppingCartItem) -> Self { Self { item: Some(item), ..Default::default() } } } } pub mod modify_item { use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub buyer_id: model::AccountId, pub product_id: model::ProductId, pub quantity: model::Quantity, pub quantity_unit: model::QuantityUnit, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Output { pub item: Option, pub error: Option, } impl Output { pub fn error(error: Error) -> Self { Self { error: Some(error), ..Default::default() } } pub fn item(item: model::ShoppingCartItem) -> Self { Self { item: Some(item), ..Default::default() } } } } pub mod modify_cart { use super::{modify_item, Error}; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub buyer_id: model::AccountId, pub items: Vec, pub checkout_notes: String, pub payment_method: Option, } #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct CartDetails { pub cart_id: model::ShoppingCartId, pub items: Vec, pub checkout_notes: String, pub payment_method: model::PaymentMethod, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Output { pub cart: Option, pub error: Option, } impl Output { pub fn error(error: Error) -> Self { Self { error: Some(error), ..Default::default() } } pub fn cart(cart: CartDetails) -> Self { Self { cart: Some(cart), ..Default::default() } } } } pub mod active_shopping_cart { use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub buyer_id: model::AccountId, } #[derive(Debug, Default, serde::Serialize, serde::Deserialize)] pub struct Output { pub error: Option, pub cart: Option, } impl Output { pub fn error(error: Error) -> Self { Self { error: Some(error), ..Default::default() } } pub fn cart(cart: model::ShoppingCart) -> Self { Self { cart: Some(cart), ..Default::default() } } } } pub mod rpc { use config::SharedAppConfig; use super::{active_shopping_cart, modify_cart, modify_item, remove_product}; #[tarpc::service] pub trait Carts { /// Change shopping cart item. async fn modify_item(input: modify_item::Input) -> modify_item::Output; /// Change entire shopping cart content. async fn modify_cart(input: modify_cart::Input) -> modify_cart::Output; /// Remove product from shopping cart. async fn remove_product(input: remove_product::Input) -> remove_product::Output; async fn active_shopping_cart( input: active_shopping_cart::Input, ) -> active_shopping_cart::Output; } pub async fn create_client(config: SharedAppConfig) -> CartsClient { use tarpc::client; use tarpc::tokio_serde::formats::Bincode; let addr = { let l = config.lock(); ( l.account_manager().bind.clone(), l.account_manager().rpc_port, ) }; let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default); let client = CartsClient::new( client::Config::default(), transport.await.expect("Failed to connect to server"), ) .spawn(); client } }