Fix conditional compilation errors

This commit is contained in:
Jeremy Wall 2022-08-13 13:27:03 -04:00
parent ca21beb04a
commit f1aeb43e8b
3 changed files with 26 additions and 19 deletions

View File

@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#[cfg(not(target_arch = "wasm32"))]
use async_std::{ use async_std::{
fs::{read_dir, read_to_string, DirEntry, File}, fs::{read_dir, read_to_string, DirEntry, File},
io::{self, ReadExt}, io::{self, ReadExt},
@ -20,7 +21,11 @@ use async_std::{
use async_trait::async_trait; use async_trait::async_trait;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use reqwasm; 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)] #[derive(Debug)]
pub struct Error(String); pub struct Error(String);
@ -43,6 +48,13 @@ impl From<std::string::FromUtf8Error> for Error {
} }
} }
#[cfg(target_arch = "wasm32")]
impl From<reqwasm::Error> for Error {
fn from(item: reqwasm::Error) -> Self {
Error(format!("{:?}", item))
}
}
pub trait TenantStoreFactory<S> pub trait TenantStoreFactory<S>
where where
S: RecipeStore, S: RecipeStore,
@ -78,12 +90,14 @@ pub struct AsyncFileStore {
path: PathBuf, path: PathBuf,
} }
#[cfg(not(target_arch = "wasm32"))]
impl AsyncFileStore { impl AsyncFileStore {
pub fn new<P: Into<PathBuf>>(root: P) -> Self { pub fn new<P: Into<PathBuf>>(root: P) -> Self {
Self { path: root.into() } Self { path: root.into() }
} }
} }
#[cfg(not(target_arch = "wasm32"))]
#[async_trait] #[async_trait]
// TODO(jwall): We need to model our own set of errors for this. // TODO(jwall): We need to model our own set of errors for this.
impl RecipeStore for AsyncFileStore { impl RecipeStore for AsyncFileStore {
@ -131,6 +145,7 @@ impl RecipeStore for AsyncFileStore {
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
#[derive(Clone, Debug)]
pub struct HttpStore { pub struct HttpStore {
root: String, root: String,
} }
@ -144,20 +159,17 @@ impl HttpStore {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
#[async_trait(?Send)] #[async_trait(?Send)]
impl RecipeStore<String> for HttpStore { impl RecipeStore for HttpStore {
#[instrument] #[instrument]
async fn get_categories(&self) -> Result<Option<String>, String> { async fn get_categories(&self) -> Result<Option<String>, Error> {
let mut path = self.root.clone(); let mut path = self.root.clone();
path.push_str("/categories"); path.push_str("/categories");
let resp = match reqwasm::http::Request::get(&path).send().await { let resp = reqwasm::http::Request::get(&path).send().await?;
Ok(resp) => resp,
Err(e) => return Err(format!("Error: {}", e)),
};
if resp.status() == 404 { if resp.status() == 404 {
debug!("Categories returned 404"); debug!("Categories returned 404");
Ok(None) Ok(None)
} else if resp.status() != 200 { } else if resp.status() != 200 {
Err(format!("Status: {}", resp.status())) Err(format!("Status: {}", resp.status()).into())
} else { } else {
debug!("We got a valid response back!"); debug!("We got a valid response back!");
let resp = resp.text().await; let resp = resp.text().await;
@ -166,15 +178,12 @@ impl RecipeStore<String> for HttpStore {
} }
#[instrument] #[instrument]
async fn get_recipes(&self) -> Result<Option<Vec<String>>, String> { async fn get_recipes(&self) -> Result<Option<Vec<String>>, Error> {
let mut path = self.root.clone(); let mut path = self.root.clone();
path.push_str("/recipes"); path.push_str("/recipes");
let resp = match reqwasm::http::Request::get(&path).send().await { let resp = reqwasm::http::Request::get(&path).send().await?;
Ok(resp) => resp,
Err(e) => return Err(format!("Error: {}", e)),
};
if resp.status() != 200 { if resp.status() != 200 {
Err(format!("Status: {}", resp.status())) Err(format!("Status: {}", resp.status()).into())
} else { } else {
debug!("We got a valid response back!"); debug!("We got a valid response back!");
Ok(resp.json().await.map_err(|e| format!("{}", e))?) Ok(resp.json().await.map_err(|e| format!("{}", e))?)

View File

@ -13,14 +13,12 @@
// limitations under the License. // limitations under the License.
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
#[cfg(target_arch = "wasm32")]
use reqwasm::http;
use serde_json::{from_str, to_string}; use serde_json::{from_str, to_string};
use sycamore::{context::use_context, prelude::*}; use sycamore::{context::use_context, prelude::*};
use tracing::{debug, error, info, instrument, warn}; use tracing::{debug, error, info, instrument, warn};
use web_sys::{window, Storage}; use web_sys::{window, Storage};
use recipe_store::{AsyncFileStore, RecipeStore}; use recipe_store::*;
use recipes::{parse, Ingredient, IngredientAccumulator, Recipe}; use recipes::{parse, Ingredient, IngredientAccumulator, Recipe};
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -28,7 +26,7 @@ pub fn get_appservice_from_context() -> AppService<AsyncFileStore> {
use_context::<AppService<AsyncFileStore>>() use_context::<AppService<AsyncFileStore>>()
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub fn get_appservice_from_context() -> AppService<AsyncFileStore> { pub fn get_appservice_from_context() -> AppService<HttpStore> {
use_context::<AppService<HttpStore>>() use_context::<AppService<HttpStore>>()
} }

View File

@ -15,7 +15,7 @@ use crate::pages::*;
use crate::{app_state::*, components::*, router_integration::*, service::AppService}; use crate::{app_state::*, components::*, router_integration::*, service::AppService};
use tracing::{debug, error, info, instrument}; use tracing::{debug, error, info, instrument};
use recipe_store::{self, AsyncFileStore}; use recipe_store::{self, *};
use sycamore::{ use sycamore::{
context::{ContextProvider, ContextProviderProps}, context::{ContextProvider, ContextProviderProps},
futures::spawn_local_in_scope, futures::spawn_local_in_scope,