create native file engine bindings for TUI/Blitz
This commit is contained in:
1 parent
d7eae79509
commit
9fa912bb59
4 files changed
+51
No files matched your search
@@ -21,6 +21,8 @@ enumset = "1.0.11"
|
|||||||
keyboard-types = "0.6.2"
|
keyboard-types = "0.6.2"
|
||||||
async-trait = "0.1.58"
|
async-trait = "0.1.58"
|
||||||
serde-value = "0.7.0"
|
serde-value = "0.7.0"
|
||||||
|
tokio = { version = "1.27", features = ["fs", "io-util"], optional = true }
|
||||||
|
rfd = { version = "0.11.3", optional = true }
|
||||||
|
|
||||||
[dependencies.web-sys]
|
[dependencies.web-sys]
|
||||||
optional = true
|
optional = true
|
||||||
@@ -48,4 +50,5 @@ serde_json = "1"
|
|||||||
default = ["serialize"]
|
default = ["serialize"]
|
||||||
serialize = ["serde", "serde_repr", "euclid/serde", "keyboard-types/serde", "dioxus-core/serialize"]
|
serialize = ["serde", "serde_repr", "euclid/serde", "keyboard-types/serde", "dioxus-core/serialize"]
|
||||||
wasm-bind = ["web-sys", "wasm-bindgen"]
|
wasm-bind = ["web-sys", "wasm-bindgen"]
|
||||||
|
native-bind = ["tokio", "rfd"]
|
||||||
hot-reload-context = ["dioxus-rsx"]
|
hot-reload-context = ["dioxus-rsx"]
|
||||||
@@ -20,6 +20,8 @@ pub mod events;
|
|||||||
pub mod geometry;
|
pub mod geometry;
|
||||||
mod global_attributes;
|
mod global_attributes;
|
||||||
pub mod input_data;
|
pub mod input_data;
|
||||||
|
#[cfg(feature = "native-bind")]
|
||||||
|
pub mod native_bind;
|
||||||
mod render_template;
|
mod render_template;
|
||||||
#[cfg(feature = "wasm-bind")]
|
#[cfg(feature = "wasm-bind")]
|
||||||
mod web_sys_bind;
|
mod web_sys_bind;
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
mod native_file_engine;
|
||||||
|
|
||||||
|
pub use native_file_engine::*;
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use crate::FileEngine;
|
||||||
|
use tokio::fs::File;
|
||||||
|
use tokio::io::AsyncReadExt;
|
||||||
|
|
||||||
|
pub struct NativeFileEngine {
|
||||||
|
files: Vec<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NativeFileEngine {
|
||||||
|
pub fn new(files: Vec<PathBuf>) -> Self {
|
||||||
|
Self { files }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait(?Send)]
|
||||||
|
impl FileEngine for NativeFileEngine {
|
||||||
|
fn files(&self) -> Vec<String> {
|
||||||
|
self.files
|
||||||
|
.iter()
|
||||||
|
.filter_map(|f| Some(f.to_str()?.to_string()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_file(&self, file: &str) -> Option<Vec<u8>> {
|
||||||
|
let mut file = File::open(file).await.ok()?;
|
||||||
|
|
||||||
|
let mut contents = Vec::new();
|
||||||
|
file.read_to_end(&mut contents).await.ok()?;
|
||||||
|
|
||||||
|
Some(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_file_to_string(&self, file: &str) -> Option<String> {
|
||||||
|
let mut file = File::open(file).await.ok()?;
|
||||||
|
|
||||||
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents).await.ok()?;
|
||||||
|
|
||||||
|
Some(contents)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user