From f1aeb43e8ba6d2fce47097e77490f27984d02495 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sat, 13 Aug 2022 13:27:03 -0400 Subject: [PATCH] Fix conditional compilation errors --- recipe-store/src/lib.rs | 37 +++++++++++++++++++++++-------------- web/src/service.rs | 6 ++---- web/src/web.rs | 2 +- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/recipe-store/src/lib.rs b/recipe-store/src/lib.rs index 88dcc63..0c3ce16 100644 --- a/recipe-store/src/lib.rs +++ b/recipe-store/src/lib.rs @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(not(target_arch = "wasm32"))] use async_std::{ fs::{read_dir, read_to_string, DirEntry, File}, io::{self, ReadExt}, @@ -20,7 +21,11 @@ use async_std::{ use async_trait::async_trait; #[cfg(target_arch = "wasm32")] use reqwasm; -use tracing::{info, instrument, warn}; +#[cfg(target_arch = "wasm32")] +use tracing::debug; +use tracing::instrument; +#[cfg(not(target_arch = "wasm32"))] +use tracing::{info, warn}; #[derive(Debug)] pub struct Error(String); @@ -43,6 +48,13 @@ impl From for Error { } } +#[cfg(target_arch = "wasm32")] +impl From for Error { + fn from(item: reqwasm::Error) -> Self { + Error(format!("{:?}", item)) + } +} + pub trait TenantStoreFactory where S: RecipeStore, @@ -78,12 +90,14 @@ pub struct AsyncFileStore { path: PathBuf, } +#[cfg(not(target_arch = "wasm32"))] impl AsyncFileStore { pub fn new>(root: P) -> Self { Self { path: root.into() } } } +#[cfg(not(target_arch = "wasm32"))] #[async_trait] // TODO(jwall): We need to model our own set of errors for this. impl RecipeStore for AsyncFileStore { @@ -131,6 +145,7 @@ impl RecipeStore for AsyncFileStore { } #[cfg(target_arch = "wasm32")] +#[derive(Clone, Debug)] pub struct HttpStore { root: String, } @@ -144,20 +159,17 @@ impl HttpStore { #[cfg(target_arch = "wasm32")] #[async_trait(?Send)] -impl RecipeStore for HttpStore { +impl RecipeStore for HttpStore { #[instrument] - async fn get_categories(&self) -> Result, String> { + async fn get_categories(&self) -> Result, Error> { let mut path = self.root.clone(); path.push_str("/categories"); - let resp = match reqwasm::http::Request::get(&path).send().await { - Ok(resp) => resp, - Err(e) => return Err(format!("Error: {}", e)), - }; + let resp = reqwasm::http::Request::get(&path).send().await?; if resp.status() == 404 { debug!("Categories returned 404"); Ok(None) } else if resp.status() != 200 { - Err(format!("Status: {}", resp.status())) + Err(format!("Status: {}", resp.status()).into()) } else { debug!("We got a valid response back!"); let resp = resp.text().await; @@ -166,15 +178,12 @@ impl RecipeStore for HttpStore { } #[instrument] - async fn get_recipes(&self) -> Result>, String> { + async fn get_recipes(&self) -> Result>, Error> { let mut path = self.root.clone(); path.push_str("/recipes"); - let resp = match reqwasm::http::Request::get(&path).send().await { - Ok(resp) => resp, - Err(e) => return Err(format!("Error: {}", e)), - }; + let resp = reqwasm::http::Request::get(&path).send().await?; if resp.status() != 200 { - Err(format!("Status: {}", resp.status())) + Err(format!("Status: {}", resp.status()).into()) } else { debug!("We got a valid response back!"); Ok(resp.json().await.map_err(|e| format!("{}", e))?) diff --git a/web/src/service.rs b/web/src/service.rs index eb1d29b..4df9eb1 100644 --- a/web/src/service.rs +++ b/web/src/service.rs @@ -13,14 +13,12 @@ // limitations under the License. use std::collections::{BTreeMap, BTreeSet}; -#[cfg(target_arch = "wasm32")] -use reqwasm::http; use serde_json::{from_str, to_string}; use sycamore::{context::use_context, prelude::*}; use tracing::{debug, error, info, instrument, warn}; use web_sys::{window, Storage}; -use recipe_store::{AsyncFileStore, RecipeStore}; +use recipe_store::*; use recipes::{parse, Ingredient, IngredientAccumulator, Recipe}; #[cfg(not(target_arch = "wasm32"))] @@ -28,7 +26,7 @@ pub fn get_appservice_from_context() -> AppService { use_context::>() } #[cfg(target_arch = "wasm32")] -pub fn get_appservice_from_context() -> AppService { +pub fn get_appservice_from_context() -> AppService { use_context::>() } diff --git a/web/src/web.rs b/web/src/web.rs index b45bbe7..3e396b5 100644 --- a/web/src/web.rs +++ b/web/src/web.rs @@ -15,7 +15,7 @@ use crate::pages::*; use crate::{app_state::*, components::*, router_integration::*, service::AppService}; use tracing::{debug, error, info, instrument}; -use recipe_store::{self, AsyncFileStore}; +use recipe_store::{self, *}; use sycamore::{ context::{ContextProvider, ContextProviderProps}, futures::spawn_local_in_scope,