use leptos::error::Error; use rocie_client::{ apis::{ api_get_inventory_api::amount_by_id, api_get_product_api::{ product_by_id, product_by_name, product_suggestion_by_name, products_in_storage, products_registered, }, api_get_unit_api::unit_by_id, api_get_unit_property_api::{unit_properties, unit_property_by_id}, api_set_barcode_api::buy_barcode, api_set_product_api::{associate_barcode, register_product}, configuration::Configuration, }, models::{ Barcode, BarcodeId, Product, ProductAmount, ProductId, ProductStub, Unit, UnitId, UnitProperty, UnitPropertyId, }, }; macro_rules! get_config { () => {{ use crate::ConfigStateStoreFields; use leptos::prelude::Read; let config = leptos::prelude::expect_context::>(); config.config().read() }}; } pub(crate) use get_config; macro_rules! mk_wrapper { ( $orig:ident($($arg_name:ident : $arg_type:ty),*) -> $output_type:ty as $new_name:ident ) => { pub(crate) async fn $new_name( $($arg_name : $arg_type),* ) -> Result<$output_type, Error> { let config = get_config!(); $orig(&config, $($arg_name),*) .await .map_err(Into::::into) } }; ( @treat_404_as_None $orig:ident($($arg_name:ident : $arg_type:ty),*) -> Option<$output_type:ty> as $new_name:ident ) => { pub(crate) async fn $new_name( $($arg_name : $arg_type),* ) -> Result, Error> { let config = get_config!(); match $orig(&config, $($arg_name),*).await { Ok(ok) => Ok::<_, leptos::error::Error>(Some(ok)), Err(err) => match err { rocie_client::apis::Error::ResponseError(ref response_content) => { match response_content.status.as_u16() { 404 => Ok(None), _ => Err(err.into()), } } err => Err(err.into()), }, } } }; ( @external_config $orig:ident(&config, $($arg_name:ident : $arg_type:ty),*) -> $output_type:ty as $new_name:ident ) => { pub(crate) async fn $new_name( config: &Configuration, $($arg_name : $arg_type),* ) -> Result<$output_type, Error> { $orig(config, $($arg_name),*) .await .map_err(Into::::into) } } } mk_wrapper!(product_by_id(product_id: ProductId) -> Product as product_by_id_wrapped); mk_wrapper!(@treat_404_as_None product_by_name(name: &str) -> Option as product_by_name_404_wrapped); mk_wrapper!(@external_config product_by_name(&config, name: &str) -> Product as product_by_name_external_wrapped); mk_wrapper!(product_suggestion_by_name(part_name: &str) -> Vec as product_suggestion_by_name_wrapped); mk_wrapper!(unit_by_id(unit_id: UnitId) -> Unit as unit_by_id_wrapped); mk_wrapper!(unit_property_by_id(unit_id: UnitPropertyId) -> UnitProperty as unit_property_by_id_wrapped); mk_wrapper!(unit_properties() -> Vec as unit_properties_wrapped); mk_wrapper!(amount_by_id(product_id: ProductId) -> ProductAmount as amount_by_id_wrapped); mk_wrapper!(products_registered() -> Vec as products_registered_wrapped); mk_wrapper!(products_in_storage() -> Vec as products_in_storage_wrapped); mk_wrapper!(@external_config buy_barcode(&config, barcode_number: BarcodeId, times: u32) -> () as buy_barcode_external_wrapped); mk_wrapper!(@external_config register_product(&config, product_stub: ProductStub) -> ProductId as register_product_external_wrapped); mk_wrapper!(@external_config associate_barcode(&config, id: ProductId, barcode: Barcode) -> () as associate_barcode_external_wrapped);