yew_stdweb/services/
reader.rs

1//! Service to load files using `FileReader`.
2
3use crate::services::Task;
4use std::fmt;
5cfg_if::cfg_if! {
6    if #[cfg(feature = "std_web")] {
7        mod std_web;
8        pub use std_web::*;
9    } else if #[cfg(feature = "web_sys")] {
10        mod web_sys;
11        pub use self::web_sys::*;
12    }
13}
14
15/// Struct that represents data of a file.
16#[derive(Clone, Debug)]
17pub struct FileData {
18    /// Name of loaded file.
19    pub name: String,
20    /// Content of loaded file.
21    pub content: Vec<u8>,
22}
23
24/// Struct that represents a chunk of a file.
25#[derive(Clone, Debug)]
26pub enum FileChunk {
27    /// Reading of chunks started. Equals **0%** progress.
28    Started {
29        /// Name of loaded file.
30        name: String,
31    },
32    /// The next data chunk that read. Also provides a progress value.
33    DataChunk {
34        /// The chunk of binary data.
35        data: Vec<u8>,
36        /// The progress value in interval: `0 < progress <= 1`.
37        progress: f32,
38    },
39    /// Reading of chunks finished. Equals **100%** progress.
40    Finished,
41}
42
43/// A reader service attached to a user context.
44#[derive(Default, Debug)]
45pub struct ReaderService {}
46
47impl fmt::Debug for ReaderTask {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.write_str("ReaderTask")
50    }
51}
52
53impl Drop for ReaderTask {
54    fn drop(&mut self) {
55        if self.is_active() {
56            self.file_reader.abort();
57        }
58    }
59}