FileEngine: expose native file types on different platforms (#1258)

* Get native file

* Correct visibility
This commit is contained in:
wooden-worm authored and GitHub committed 2023-08-17 08:03:56 -05:00
1 parent fa39408d58
commit f652474a6e
4 files changed
+40 -1

No files matched your search

+10 -1
View File
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fmt::Debug};
use std::{any::Any, collections::HashMap, fmt::Debug};
use dioxus_core::Event;
@@ -45,6 +45,12 @@ impl FileEngine for SerializedFileEngine {
.await
.map(|bytes| String::from_utf8_lossy(&bytes).to_string())
}
async fn get_native_file(&self, file: &str) -> Option<Box<dyn Any>> {
self.read_file(file)
.await
.map(|val| Box::new(val) as Box<dyn Any>)
}
}
#[cfg(feature = "serialize")]
@@ -89,6 +95,9 @@ pub trait FileEngine {
// read a file to string
async fn read_file_to_string(&self, file: &str) -> Option<String>;
// returns a file in platform's native representation
async fn get_native_file(&self, file: &str) -> Option<Box<dyn Any>>;
}
impl_event! {
@@ -1,3 +1,4 @@
use std::any::Any;
use std::path::PathBuf;
use crate::FileEngine;
@@ -40,4 +41,9 @@ impl FileEngine for NativeFileEngine {
Some(contents)
}
async fn get_native_file(&self, file: &str) -> Option<Box<dyn Any>> {
let file = File::open(file).await.ok()?;
Some(Box::new(file))
}
}