From a1fe74f53e4a6bfff3d492bed60fb3941a7bc92a Mon Sep 17 00:00:00 2001 From: Adrien Date: Tue, 21 May 2024 12:38:40 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Use=20of=20"target=5Ffa?= =?UTF-8?q?mily"=20instead=20of=20feature=20to=20manage=20wasm=20platform?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 4 +- src/infrastructure/services/mozaik_builder.rs | 26 ++++++- .../services/random_svg_generators.rs | 73 ++++++++++--------- src/main.rs | 6 +- 4 files changed, 66 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1be2323..562a6b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,7 @@ version = "0.1.0" edition = "2021" [features] -default = ["web"] -desktop = ["dioxus/desktop", "tracing-subscriber/time"] -web = ["dioxus/web", "matrix-sdk/js"] +default = [] [dependencies] # Errors diff --git a/src/infrastructure/services/mozaik_builder.rs b/src/infrastructure/services/mozaik_builder.rs index d9d5375..fb946b4 100644 --- a/src/infrastructure/services/mozaik_builder.rs +++ b/src/infrastructure/services/mozaik_builder.rs @@ -6,6 +6,12 @@ use image::{DynamicImage, ImageFormat}; use image::{GenericImage, RgbImage}; use tracing::{error, warn}; +cfg_if! { + if #[cfg(not(target_family = "wasm"))] { + use tokio::task::spawn_blocking; + } +} + fn from_raw_to_image(raw: &Vec) -> Option { match Reader::new(Cursor::new(raw)).with_guessed_format() { Ok(reader) => match reader.decode() { @@ -19,7 +25,7 @@ fn from_raw_to_image(raw: &Vec) -> Option { None } -pub fn create_mozaik( +fn create_mozaik_( width_px: u32, height_px: u32, images: &[Vec], @@ -90,3 +96,21 @@ pub fn create_mozaik( bytes } + +pub async fn create_mozaik( + width_px: u32, + height_px: u32, + images: Vec>, + padding_image: Option>, +) -> Vec { + cfg_if! { + if #[cfg(target_family = "wasm")] { + create_mozaik_(width_px, height_px, &images, &padding_image) + } + else { + spawn_blocking(move || { + create_mozaik_(width_px, height_px, &images, &padding_image) + }).await.unwrap() + } + } +} diff --git a/src/infrastructure/services/random_svg_generators.rs b/src/infrastructure/services/random_svg_generators.rs index dd4faf3..2ca21e0 100644 --- a/src/infrastructure/services/random_svg_generators.rs +++ b/src/infrastructure/services/random_svg_generators.rs @@ -7,11 +7,13 @@ use rand::distributions::{Alphanumeric, DistString}; use reqwest::Result as RequestResult; use tracing::error; -#[cfg(feature = "desktop")] -use tokio::fs::read_to_string; - -#[cfg(feature = "web")] -use web_sys; +cfg_if! { + if #[cfg(target_family = "wasm")] { + use web_sys; + } else { + use tokio::fs::read_to_string; + } +} #[derive(Eq, PartialEq, Hash)] pub enum AvatarFeeling { @@ -145,39 +147,38 @@ async fn fetch_dicebear_svg( text.unwrap_or("".to_string()) } -#[cfg(feature = "desktop")] -fn gen_placeholder_fetcher(path: &'static str) -> Box>> { - let path = format!("./public/{}", &path); - Box::new(async move { - match read_to_string(&path).await { - Ok(content) => Some(content), - Err(err) => { - error!( - "Error during the access to the {path} file: {}", - err.to_string() - ); - None - } +cfg_if! { + if #[cfg(target_family = "wasm")] { + fn gen_placeholder_fetcher<'a>(path: &'static str) -> Box>> { + Box::new(async move { + let url = format!("{}{}", web_sys::window().unwrap().origin(), path); + match fetch_text(url).await { + Ok(content) => Some(content), + Err(err) => { + error!("Error during {path} fetching: {}", err.to_string()); + None + } + } + }) } - }) -} -#[cfg(feature = "web")] -fn gen_placeholder_fetcher<'a>(path: &'static str) -> Box>> { - Box::new(async move { - let url = format!("{}{}", web_sys::window().unwrap().origin(), path); - match fetch_text(url).await { - Ok(content) => Some(content), - Err(err) => { - error!("Error during {path} fetching: {}", err.to_string()); - None - } + } + else { + fn gen_placeholder_fetcher(path: &'static str) -> Box>> { + let path = format!("./public/{}", &path); + Box::new(async move { + match read_to_string(&path).await { + Ok(content) => Some(content), + Err(err) => { + error!( + "Error during the access to the {path} file: {}", + err.to_string() + ); + None + } + } + }) } - }) -} - -#[cfg(not(any(feature = "desktop", feature = "web")))] -fn gen_placeholder_fetcher<'a>(_path: &'static str) -> Box>> { - Box::new(async move { None }) + } } pub async fn generate_random_svg_avatar<'a>(config: Option<&'a AvatarConfig<'a>>) -> String { diff --git a/src/main.rs b/src/main.rs index 9739b50..562b69c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,13 +16,13 @@ use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; cfg_if! { - if #[cfg(feature = "desktop")] { + if #[cfg(target_family = "wasm")] { + use tracing_web::MakeWebConsoleWriter; + } else { use dioxus::desktop::Config; use std::fs::File; use time::format_description::well_known::Iso8601; use tracing_subscriber::fmt::time::UtcTime; - } else if #[cfg(feature = "web")] { - use tracing_web::MakeWebConsoleWriter; } }