♻️ Use of "target_family" instead of feature to manage wasm platform
This commit is contained in:
@@ -4,9 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["web"]
|
default = []
|
||||||
desktop = ["dioxus/desktop", "tracing-subscriber/time"]
|
|
||||||
web = ["dioxus/web", "matrix-sdk/js"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# Errors
|
# Errors
|
||||||
|
@@ -6,6 +6,12 @@ use image::{DynamicImage, ImageFormat};
|
|||||||
use image::{GenericImage, RgbImage};
|
use image::{GenericImage, RgbImage};
|
||||||
use tracing::{error, warn};
|
use tracing::{error, warn};
|
||||||
|
|
||||||
|
cfg_if! {
|
||||||
|
if #[cfg(not(target_family = "wasm"))] {
|
||||||
|
use tokio::task::spawn_blocking;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn from_raw_to_image(raw: &Vec<u8>) -> Option<DynamicImage> {
|
fn from_raw_to_image(raw: &Vec<u8>) -> Option<DynamicImage> {
|
||||||
match Reader::new(Cursor::new(raw)).with_guessed_format() {
|
match Reader::new(Cursor::new(raw)).with_guessed_format() {
|
||||||
Ok(reader) => match reader.decode() {
|
Ok(reader) => match reader.decode() {
|
||||||
@@ -19,7 +25,7 @@ fn from_raw_to_image(raw: &Vec<u8>) -> Option<DynamicImage> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_mozaik(
|
fn create_mozaik_(
|
||||||
width_px: u32,
|
width_px: u32,
|
||||||
height_px: u32,
|
height_px: u32,
|
||||||
images: &[Vec<u8>],
|
images: &[Vec<u8>],
|
||||||
@@ -90,3 +96,21 @@ pub fn create_mozaik(
|
|||||||
|
|
||||||
bytes
|
bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_mozaik(
|
||||||
|
width_px: u32,
|
||||||
|
height_px: u32,
|
||||||
|
images: Vec<Vec<u8>>,
|
||||||
|
padding_image: Option<Vec<u8>>,
|
||||||
|
) -> Vec<u8> {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -7,11 +7,13 @@ use rand::distributions::{Alphanumeric, DistString};
|
|||||||
use reqwest::Result as RequestResult;
|
use reqwest::Result as RequestResult;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
#[cfg(feature = "desktop")]
|
cfg_if! {
|
||||||
use tokio::fs::read_to_string;
|
if #[cfg(target_family = "wasm")] {
|
||||||
|
use web_sys;
|
||||||
#[cfg(feature = "web")]
|
} else {
|
||||||
use web_sys;
|
use tokio::fs::read_to_string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash)]
|
#[derive(Eq, PartialEq, Hash)]
|
||||||
pub enum AvatarFeeling {
|
pub enum AvatarFeeling {
|
||||||
@@ -145,8 +147,23 @@ async fn fetch_dicebear_svg(
|
|||||||
text.unwrap_or("".to_string())
|
text.unwrap_or("".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "desktop")]
|
cfg_if! {
|
||||||
fn gen_placeholder_fetcher(path: &'static str) -> Box<impl Future<Output = Option<String>>> {
|
if #[cfg(target_family = "wasm")] {
|
||||||
|
fn gen_placeholder_fetcher<'a>(path: &'static str) -> Box<impl Future<Output = Option<String>>> {
|
||||||
|
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<impl Future<Output = Option<String>>> {
|
||||||
let path = format!("./public/{}", &path);
|
let path = format!("./public/{}", &path);
|
||||||
Box::new(async move {
|
Box::new(async move {
|
||||||
match read_to_string(&path).await {
|
match read_to_string(&path).await {
|
||||||
@@ -160,24 +177,8 @@ fn gen_placeholder_fetcher(path: &'static str) -> Box<impl Future<Output = Optio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
|
||||||
#[cfg(feature = "web")]
|
|
||||||
fn gen_placeholder_fetcher<'a>(path: &'static str) -> Box<impl Future<Output = Option<String>>> {
|
|
||||||
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(not(any(feature = "desktop", feature = "web")))]
|
|
||||||
fn gen_placeholder_fetcher<'a>(_path: &'static str) -> Box<impl Future<Output = Option<String>>> {
|
|
||||||
Box::new(async move { None })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn generate_random_svg_avatar<'a>(config: Option<&'a AvatarConfig<'a>>) -> String {
|
pub async fn generate_random_svg_avatar<'a>(config: Option<&'a AvatarConfig<'a>>) -> String {
|
||||||
|
@@ -16,13 +16,13 @@ use tracing_subscriber::prelude::*;
|
|||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(feature = "desktop")] {
|
if #[cfg(target_family = "wasm")] {
|
||||||
|
use tracing_web::MakeWebConsoleWriter;
|
||||||
|
} else {
|
||||||
use dioxus::desktop::Config;
|
use dioxus::desktop::Config;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use time::format_description::well_known::Iso8601;
|
use time::format_description::well_known::Iso8601;
|
||||||
use tracing_subscriber::fmt::time::UtcTime;
|
use tracing_subscriber::fmt::time::UtcTime;
|
||||||
} else if #[cfg(feature = "web")] {
|
|
||||||
use tracing_web::MakeWebConsoleWriter;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user