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.
// 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<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>
where
S: RecipeStore,
@ -78,12 +90,14 @@ pub struct AsyncFileStore {
path: PathBuf,
}
#[cfg(not(target_arch = "wasm32"))]
impl AsyncFileStore {
pub fn new<P: Into<PathBuf>>(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<String> for HttpStore {
impl RecipeStore for HttpStore {
#[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();
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<String> for HttpStore {
}
#[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();
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))?)

View File

@ -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<AsyncFileStore> {
use_context::<AppService<AsyncFileStore>>()
}
#[cfg(target_arch = "wasm32")]
pub fn get_appservice_from_context() -> AppService<AsyncFileStore> {
pub fn get_appservice_from_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 tracing::{debug, error, info, instrument};
use recipe_store::{self, AsyncFileStore};
use recipe_store::{self, *};
use sycamore::{
context::{ContextProvider, ContextProviderProps},
futures::spawn_local_in_scope,