1 use webcore::value::{Value, Reference};
2 use webcore::try_from::TryInto;
3 use webapi::blob::IBlob;
4 use webapi::event_target::{IEventTarget, EventTarget};
5 use webapi::array_buffer::ArrayBuffer;
6 use private::TODO;
7 
8 /// The FileReader object lets web applications asynchronously read the contents of files
9 /// (or raw data buffers) stored on the user's computer, using [File](struct.File.html)
10 /// or [Blob](struct.Blob.html) objects to specify the file or data to read.
11 ///
12 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader)
13 // https://w3c.github.io/FileAPI/#dfn-filereader
14 #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
15 #[reference(instance_of = "FileReader")]
16 #[reference(subclass_of(EventTarget))]
17 pub struct FileReader( Reference );
18 
19 impl IEventTarget for FileReader {}
20 
21 /// The [result](struct.FileReader.html#method.result) of a read operation performed with a [FileReader](struct.File.html).
22 #[derive(Clone, Debug)]
23 pub enum FileReaderResult {
24     /// A string; a result of calling [FileReader::read_as_text](struct.FileReader.html#method.read_as_text).
25     String( String ),
26 
27     /// An [ArrayBuffer](struct.ArrayBuffer.html); a result of calling [FileReader::read_as_array_buffer](struct.FileReader.html#method.read_as_array_buffer).
28     ArrayBuffer( ArrayBuffer )
29 }
30 
31 /// A number indicating the state of the `FileReader`.
32 ///
33 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readyState)
34 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
35 pub enum FileReaderReadyState {
36     /// No data has been loaded yet.
37     Empty,
38     /// Data is currently being loaded.
39     Loading,
40     /// The entire read request has been completed.
41     Done
42 }
43 
44 impl FileReader {
45     /// Returns a newly constructed `FileReader`.
46     ///
47     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/FileReader)
48     // https://w3c.github.io/FileAPI/#dom-filereader-filereader
new() -> FileReader49     pub fn new() -> FileReader {
50         js!( return new FileReader(); ).try_into().unwrap()
51     }
52 
53     /// Starts reading the contents of the specified blob. Once finished
54     /// the `result` attribute will contain the contents of the file as a text string.
55     ///
56     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText)
57     // https://w3c.github.io/FileAPI/#ref-for-dfn-readAsText
read_as_text< T: IBlob >( &self, blob: &T ) -> Result< (), TODO >58     pub fn read_as_text< T: IBlob >( &self, blob: &T ) -> Result< (), TODO > {
59         js!( @{self}.readAsText( @{blob.as_ref()} ); );
60         Ok(())
61     }
62 
63     /// Starts reading the contents of the specified blob. Once finished
64     /// the `result` attribute will contain the contents of the file as an [TypedArray](struct.ArrayBuffer.html).
65     ///
66     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer)
67     // https://w3c.github.io/FileAPI/#ref-for-dfn-readAsArrayBuffer
read_as_array_buffer< T: IBlob >( &self, blob: &T ) -> Result< (), TODO >68     pub fn read_as_array_buffer< T: IBlob >( &self, blob: &T ) -> Result< (), TODO > {
69         js!( @{self}.readAsArrayBuffer( @{blob.as_ref()} ); );
70         Ok(())
71     }
72 
73     /// Aborts the read operation. Upon return, the `ready_state` will be `Done`.
74     ///
75     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/abort)
76     // https://w3c.github.io/FileAPI/#ref-for-dfn-abort%E2%91%A0
abort( &self )77     pub fn abort( &self ) {
78         js!( return @{self}.abort(); );
79     }
80 
81     /// Returns the current state of the reader.
82     ///
83     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readyState)
84     // https://w3c.github.io/FileAPI/#ref-for-dfn-readyState
ready_state( &self ) -> FileReaderReadyState85     pub fn ready_state( &self ) -> FileReaderReadyState {
86         let state: i32 = js!( return @{self}.readyState; ).try_into().unwrap();
87         match state {
88             0 => FileReaderReadyState::Empty,
89             1 => FileReaderReadyState::Loading,
90             2 => FileReaderReadyState::Done,
91             _ => unreachable!( "Unexpected value of FileReader::readyState: {}", state )
92         }
93     }
94 
95     /// The file's contents. This method will only return a value after the read operation
96     /// is complete, and the format of the data depends on which of the methods was used
97     /// to initiate the read operation.
98     ///
99     /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/result)
100     // https://w3c.github.io/FileAPI/#ref-for-dfn-result
result( &self ) -> Option< FileReaderResult >101     pub fn result( &self ) -> Option< FileReaderResult > {
102         let result = js!( return @{self}.result; );
103         match result {
104             Value::Undefined | Value::Null => None,
105             Value::String( text ) => Some( FileReaderResult::String( text ) ),
106             Value::Reference( reference ) => Some( FileReaderResult::ArrayBuffer( reference.try_into().unwrap() ) ),
107             _ => unreachable!( "Unexpected result of a FileReader: {:?}", result )
108         }
109     }
110 }
111