Skip to content

Commit

Permalink
remove the file engine feature in the html crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed Dec 18, 2024
1 parent c2952a7 commit 04d3b71
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/dioxus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ macro = ["dep:dioxus-core-macro"]
html = ["dep:dioxus-html"]
hooks = ["dep:dioxus-hooks"]
devtools = ["dep:dioxus-devtools", "dioxus-web?/devtools", "dioxus-fullstack?/devtools"]
mounted = ["dioxus-web?/mounted", "dioxus-html?/mounted"]
mounted = ["dioxus-web?/mounted"]
file_engine = ["dioxus-web?/file_engine"]
asset = ["dep:manganis"]
document = ["dioxus-web?/document", "dep:dioxus-document", "dep:dioxus-history"]
Expand Down
10 changes: 5 additions & 5 deletions packages/html/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ js-sys = { version = "0.3.56", optional = true }
euclid = "0.22.7"
enumset = "1.1.2"
keyboard-types = { version = "0.7", default-features = false }
async-trait = { version = "0.1.58", optional = true }
async-trait = { version = "0.1.58" }
tokio = { workspace = true, features = ["fs", "io-util"], optional = true }
futures-channel = { workspace = true }
serde_json = { version = "1", optional = true }
Expand All @@ -41,7 +41,7 @@ tokio = { workspace = true, features = ["time"] }
manganis = { workspace = true }

[features]
default = ["serialize", "mounted", "file_engine"]
default = ["serialize"]
serialize = [
"dep:serde",
"dep:serde_json",
Expand All @@ -50,10 +50,10 @@ serialize = [
"keyboard-types/serde",
"dioxus-core/serialize"
]
# TODO: Remove the mounted feature flag in the next major release. It no longer activates any extra code
mounted = []
file_engine = [
"dep:async-trait",
]
# TODO: Remove the file engine feature flag in the next major release. It no longer activates any extra code
file_engine = []
hot-reload-context = ["dep:dioxus-rsx"]
html-to-rsx = []

Expand Down
4 changes: 0 additions & 4 deletions packages/html/src/events/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl DragData {
}

impl crate::HasFileData for DragData {
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn crate::file_data::FileEngine>> {
self.inner.files()
}
Expand Down Expand Up @@ -112,7 +111,6 @@ impl PointerInteraction for DragData {
pub struct SerializedDragData {
pub mouse: crate::point_interaction::SerializedPointInteraction,

#[cfg(feature = "file_engine")]
#[serde(default)]
files: Option<crate::file_data::SerializedFileEngine>,
}
Expand All @@ -122,7 +120,6 @@ impl SerializedDragData {
fn new(drag: &DragData) -> Self {
Self {
mouse: crate::point_interaction::SerializedPointInteraction::from(drag),
#[cfg(feature = "file_engine")]
files: None,
}
}
Expand All @@ -137,7 +134,6 @@ impl HasDragData for SerializedDragData {

#[cfg(feature = "serialize")]
impl crate::file_data::HasFileData for SerializedDragData {
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn crate::file_data::FileEngine>> {
self.files
.as_ref()
Expand Down
8 changes: 0 additions & 8 deletions packages/html/src/events/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ impl FormData {
}

/// Get the files of the form event
#[cfg(feature = "file_engine")]
pub fn files(&self) -> Option<std::sync::Arc<dyn crate::file_data::FileEngine>> {
self.inner.files()
}
Expand Down Expand Up @@ -177,7 +176,6 @@ pub struct SerializedFormData {
#[serde(default)]
valid: bool,

#[cfg(feature = "file_engine")]
#[serde(default)]
files: Option<crate::file_data::SerializedFileEngine>,
}
Expand All @@ -190,12 +188,10 @@ impl SerializedFormData {
value,
values,
valid: true,
#[cfg(feature = "file_engine")]
files: None,
}
}

#[cfg(feature = "file_engine")]
/// Add files to the serialized form data object
pub fn with_files(mut self, files: crate::file_data::SerializedFileEngine) -> Self {
self.files = Some(files);
Expand All @@ -208,7 +204,6 @@ impl SerializedFormData {
value: data.value(),
values: data.values(),
valid: data.valid(),
#[cfg(feature = "file_engine")]
files: {
match data.files() {
Some(files) => {
Expand All @@ -234,7 +229,6 @@ impl SerializedFormData {
value: data.value(),
values: data.values(),
valid: data.valid(),
#[cfg(feature = "file_engine")]
files: None,
}
}
Expand All @@ -261,15 +255,13 @@ impl HasFormData for SerializedFormData {

#[cfg(feature = "serialize")]
impl HasFileData for SerializedFormData {
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn crate::FileEngine>> {
self.files
.as_ref()
.map(|files| std::sync::Arc::new(files.clone()) as _)
}
}

#[cfg(feature = "file_engine")]
impl HasFileData for FormData {
fn files(&self) -> Option<std::sync::Arc<dyn crate::FileEngine>> {
self.inner.files()
Expand Down
7 changes: 0 additions & 7 deletions packages/html/src/file_data.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
pub trait HasFileData: std::any::Any {
// NOTE: The methods of this trait are config'ed out instead of the trait
// itself because several other traits inherit from this trait and there isn't a clean way to
// conditionally inherit from a trait based on a config.
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn FileEngine>> {
None
}
}

#[cfg(feature = "serialize")]
#[cfg(feature = "file_engine")]
/// A file engine that serializes files to bytes
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedFileEngine {
pub files: std::collections::HashMap<String, Vec<u8>>,
}

#[cfg(feature = "serialize")]
#[cfg(feature = "file_engine")]
#[async_trait::async_trait(?Send)]
impl FileEngine for SerializedFileEngine {
fn files(&self) -> Vec<String> {
Expand Down Expand Up @@ -46,7 +40,6 @@ impl FileEngine for SerializedFileEngine {
}
}

#[cfg(feature = "file_engine")]
#[async_trait::async_trait(?Send)]
pub trait FileEngine {
// get a list of file names
Expand Down
2 changes: 1 addition & 1 deletion packages/liveview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tokio-stream = { version = "0.1.11", features = ["net"] }
tokio-util = { version = "0.7.4", features = ["rt"] }
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
dioxus-html = { workspace = true, features = ["serialize", "mounted"] }
dioxus-html = { workspace = true, features = ["serialize"] }
dioxus-document = { workspace = true }
dioxus-history = { workspace = true }
rustc-hash = { workspace = true }
Expand Down
36 changes: 21 additions & 15 deletions packages/web/src/events/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,31 @@ impl HasDragData for Synthetic<DragEvent> {
}

impl HasFileData for Synthetic<DragEvent> {
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn dioxus_html::FileEngine>> {
use wasm_bindgen::JsCast;

let files = self
.event
.dyn_ref::<web_sys::DragEvent>()
.and_then(|drag_event| {
drag_event.data_transfer().and_then(|dt| {
dt.files().and_then(|files| {
#[allow(clippy::arc_with_non_send_sync)]
crate::file_engine::WebFileEngine::new(files).map(|f| {
std::sync::Arc::new(f) as std::sync::Arc<dyn dioxus_html::FileEngine>
#[cfg(feature = "file_engine")]
{
use wasm_bindgen::JsCast;
let files = self
.event
.dyn_ref::<web_sys::DragEvent>()
.and_then(|drag_event| {
drag_event.data_transfer().and_then(|dt| {
dt.files().and_then(|files| {
#[allow(clippy::arc_with_non_send_sync)]
crate::file_engine::WebFileEngine::new(files).map(|f| {
std::sync::Arc::new(f)
as std::sync::Arc<dyn dioxus_html::FileEngine>
})
})
})
})
});
});

files
files
}
#[cfg(not(feature = "file_engine"))]
{
None
}
}
}

Expand Down
32 changes: 19 additions & 13 deletions packages/web/src/events/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ use dioxus_html::HasFileData;
use super::Synthetic;

impl HasFileData for Synthetic<web_sys::Event> {
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn dioxus_html::FileEngine>> {
use wasm_bindgen::JsCast;
#[cfg(feature = "file_engine")]
{
use wasm_bindgen::JsCast;

let files = self
.event
.dyn_ref()
.and_then(|input: &web_sys::HtmlInputElement| {
input.files().and_then(|files| {
#[allow(clippy::arc_with_non_send_sync)]
crate::file_engine::WebFileEngine::new(files).map(|f| {
std::sync::Arc::new(f) as std::sync::Arc<dyn dioxus_html::FileEngine>
let files = self
.event
.dyn_ref()
.and_then(|input: &web_sys::HtmlInputElement| {
input.files().and_then(|files| {
#[allow(clippy::arc_with_non_send_sync)]
crate::file_engine::WebFileEngine::new(files).map(|f| {
std::sync::Arc::new(f) as std::sync::Arc<dyn dioxus_html::FileEngine>
})
})
})
});
});

files
files
}
#[cfg(not(feature = "file_engine"))]
{
None
}
}
}
30 changes: 18 additions & 12 deletions packages/web/src/events/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,27 @@ impl HasFormData for WebFormData {
}

impl HasFileData for WebFormData {
#[cfg(feature = "file_engine")]
fn files(&self) -> Option<std::sync::Arc<dyn dioxus_html::FileEngine>> {
let files = self
.element
.dyn_ref()
.and_then(|input: &web_sys::HtmlInputElement| {
input.files().and_then(|files| {
#[allow(clippy::arc_with_non_send_sync)]
crate::file_engine::WebFileEngine::new(files).map(|f| {
std::sync::Arc::new(f) as std::sync::Arc<dyn dioxus_html::FileEngine>
#[cfg(feature = "file_engine")]
{
let files = self
.element
.dyn_ref()
.and_then(|input: &web_sys::HtmlInputElement| {
input.files().and_then(|files| {
#[allow(clippy::arc_with_non_send_sync)]
crate::file_engine::WebFileEngine::new(files).map(|f| {
std::sync::Arc::new(f) as std::sync::Arc<dyn dioxus_html::FileEngine>
})
})
})
});
});

files
files
}
#[cfg(not(feature = "file_engine"))]
{
None
}
}
}

Expand Down

0 comments on commit 04d3b71

Please sign in to comment.