1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//!
//! File system abstraction layer. Currently supporting storage on the filesystem
//! and the browser domain-associated local storage ([Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)).
//!
//! Storage APIs abstracted:
//! - Rust std file I/O (fs::xxx)
//! - NodeJS file I/O (fs::read_file_sync)
//! - Browser local storage
//!
//! By default, all I/O functions will use the name of the file as a key
//! for localstorage. If you want to manually specify the localstorage key.
//!

use crate::error::Error;
use crate::result::Result;
use cfg_if::cfg_if;
use js_sys::Reflect;
use js_sys::Uint8Array;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::path::{Path, PathBuf};
use wasm_bindgen::prelude::*;
use workflow_core::dirs;
use workflow_core::runtime;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends = Uint8Array)]
    #[derive(Clone, Debug)]
    pub type Buffer;

    #[wasm_bindgen(static_method_of = Buffer, js_name = from)]
    pub fn from_uint8_array(array: &Uint8Array) -> Buffer;

}

pub fn local_storage() -> web_sys::Storage {
    web_sys::window().unwrap().local_storage().unwrap().unwrap()
}

#[derive(Default)]
pub struct Options {
    pub local_storage_key: Option<String>,
}

impl Options {
    pub fn with_local_storage_key(key: &str) -> Self {
        Options {
            local_storage_key: Some(key.to_string()),
        }
    }

    pub fn local_storage_key(&self, filename: &Path) -> String {
        self.local_storage_key
            .clone()
            .unwrap_or(filename.file_name().unwrap().to_str().unwrap().to_string())
    }
}

cfg_if! {
    if #[cfg(target_arch = "wasm32")] {
        use workflow_core::hex::*;
        use workflow_wasm::jserror::*;
        use workflow_node as node;
        use js_sys::Object;
        use workflow_chrome::storage::LocalStorage as ChromeStorage;


        pub async fn exists_with_options<P : AsRef<Path>>(filename: P, options : Options) -> Result<bool> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                Ok(node::fs::exists_sync(filename.as_ref())?)
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    Ok(ChromeStorage::get_item(&key_name).await?.is_some())
                }else{
                    Ok(local_storage().get_item(&key_name)?.is_some())
                }
            }
        }

        pub fn exists_with_options_sync<P : AsRef<Path>>(filename: P, options : Options) -> Result<bool> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                Ok(node::fs::exists_sync(filename.as_ref())?)
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    Err(Error::Custom("localStorage api is unavailable, you can use exists_with_options() for chrome.storage.local api.".to_string()))
                }else{
                    Ok(local_storage().get_item(&key_name)?.is_some())
                }
            }
        }

        pub async fn read_to_string_with_options<P : AsRef<Path>>(filename: P, options : Options) -> Result<String> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                Reflect::set(&options, &"encoding".into(), &"utf-8".into())?;
                let js_value = node::fs::read_file_sync(&filename, options)?;
                let text = js_value.as_string().ok_or(Error::DataIsNotAString(filename))?;
                Ok(text)
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    if let Some(text) = ChromeStorage::get_item(&key_name).await?{
                        Ok(text)
                    }else {
                        Err(Error::NotFound(filename.as_ref().to_string_lossy().to_string()))
                    }
                }else if let Some(text) = local_storage().get_item(&key_name)? {
                    Ok(text)
                } else {
                    Err(Error::NotFound(filename.as_ref().to_string_lossy().to_string()))
                }
            }
        }

        pub fn read_to_string_with_options_sync<P : AsRef<Path>>(filename: P, options : Options) -> Result<String> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                Reflect::set(&options, &"encoding".into(), &"utf-8".into())?;
                let js_value = node::fs::read_file_sync(&filename, options)?;
                let text = js_value.as_string().ok_or(Error::DataIsNotAString(filename))?;
                Ok(text)
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    Err(Error::Custom("localStorage api is unavailable, you can use exists_with_options() for chrome.storage.local api.".to_string()))
                }else if let Some(text) = local_storage().get_item(&key_name)? {
                    Ok(text)
                } else {
                    Err(Error::NotFound(filename.as_ref().to_string_lossy().to_string()))
                }
            }
        }

        pub async fn read_binary_with_options<P : AsRef<Path>>(filename: P, options : Options) -> Result<Vec<u8>> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                let buffer = node::fs::read_file_sync(&filename, options)?;
                let data = buffer.dyn_into::<Uint8Array>()?;
                Ok(data.to_vec())
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                let data = if runtime::is_chrome_extension(){
                    ChromeStorage::get_item(&key_name).await?
                }else{
                    local_storage().get_item(&key_name)?
                };

                if let Some(text) = data{
                    let data = Vec::<u8>::from_hex(&text)?;
                    Ok(data)
                } else {
                    Err(Error::NotFound(filename.as_ref().to_string_lossy().to_string()))
                }
            }
        }

        pub fn read_binary_with_options_sync<P : AsRef<Path>>(filename: P, options : Options) -> Result<Vec<u8>> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                let buffer = node::fs::read_file_sync(&filename, options)?;
                let data = buffer.dyn_into::<Uint8Array>()?;
                Ok(data.to_vec())
            } else if runtime::is_chrome_extension(){
                    Err(Error::Custom("localStorage api is unavailable, you can use read_binary_with_options() for chrome.storage.local api.".to_string()))
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if let Some(text) = local_storage().get_item(&key_name)? {
                    let data = Vec::<u8>::from_hex(&text)?;
                    Ok(data)
                } else {
                    Err(Error::NotFound(filename.as_ref().to_string_lossy().to_string()))
                }
            }
        }

        pub async fn write_string_with_options<P : AsRef<Path>>(filename: P, options: Options, text : &str) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                Reflect::set(&options, &"encoding".into(), &"utf-8".into())?;
                let data = JsValue::from(text);
                node::fs::write_file_sync(&filename, data, options)?;
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    ChromeStorage::set_item(&key_name, text).await?;
                }else{
                    local_storage().set_item(&key_name, text)?;
                }
            }

            Ok(())
        }

        pub fn write_string_with_options_sync<P : AsRef<Path>>(filename: P, options: Options, text : &str) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                Reflect::set(&options, &"encoding".into(), &"utf-8".into())?;
                let data = JsValue::from(text);
                node::fs::write_file_sync(&filename, data, options)?;
            } else if runtime::is_chrome_extension(){
                return Err(Error::Custom("localStorage api is unavailable, you can use write_string_with_options() for chrome.storage.local api.".to_string()));
            }else{
                let key_name = options.local_storage_key(filename.as_ref());
                local_storage().set_item(&key_name, text)?;
            }
            Ok(())
        }

        pub async fn write_binary_with_options<P : AsRef<Path>>(filename: P, options: Options, data : &[u8]) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                let uint8_array = Uint8Array::from(data);
                let buffer = Buffer::from_uint8_array(&uint8_array);
                node::fs::write_file_sync(&filename, buffer.into(), options)?;
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    ChromeStorage::set_item(&key_name, data.to_hex().as_str()).await?;
                }else{
                    local_storage().set_item(&key_name, data.to_hex().as_str())?;
                }
            }
            Ok(())
        }

        pub fn write_binary_with_options_sync<P : AsRef<Path>>(filename: P, options: Options, data : &[u8]) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                let options = Object::new();
                let uint8_array = Uint8Array::from(data);
                let buffer = Buffer::from_uint8_array(&uint8_array);
                node::fs::write_file_sync(&filename, buffer.into(), options)?;
            } else if runtime::is_chrome_extension(){
                return Err(Error::Custom("localStorage api is unavailable, you can use write_binary_with_options() for chrome.storage.local api.".to_string()));
            }else{
                let key_name = options.local_storage_key(filename.as_ref());
                local_storage().set_item(&key_name, data.to_hex().as_str())?;
            }

            Ok(())
        }

        pub async fn remove_with_options<P : AsRef<Path>>(filename: P, options: Options) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                node::fs::unlink_sync(&filename)?;
            } else {
                let key_name = options.local_storage_key(filename.as_ref());
                if runtime::is_chrome_extension(){
                    ChromeStorage::remove_item(&key_name).await?;
                }else{
                    local_storage().remove_item(&key_name)?;
                }
            }
            Ok(())
        }

        pub fn remove_with_options_sync<P : AsRef<Path>>(filename: P, options: Options) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let filename = filename.as_ref().to_platform_string();
                node::fs::unlink_sync(&filename)?;
            } else if runtime::is_chrome_extension(){
                return Err(Error::Custom("localStorage api is unavailable, you can use remove_with_options() for chrome.storage.local api.".to_string()));
            }else{
                let key_name = options.local_storage_key(filename.as_ref());
                local_storage().remove_item(&key_name)?;
            }
            Ok(())
        }

        pub async fn rename<P : AsRef<Path>>(from: P, to: P) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let from = from.as_ref().to_platform_string();
                let to = to.as_ref().to_platform_string();
                node::fs::rename_sync(&from,&to)?;
                Ok(())
            } else {
                Err(Error::NotSupported)
            }
        }

        pub fn rename_sync<P : AsRef<Path>>(from: P, to: P) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let from = from.as_ref().to_platform_string();
                let to = to.as_ref().to_platform_string();
                node::fs::rename_sync(&from,&to)?;
                Ok(())
            } else {
                Err(Error::NotSupported)
            }
        }

        pub async fn create_dir_all<P : AsRef<Path>>(filename: P) -> Result<()> {
            create_dir_all_sync(filename)
        }

        pub fn create_dir_all_sync<P : AsRef<Path>>(filename: P) -> Result<()> {
            if runtime::is_node() || runtime::is_nw() {
                let options = Object::new();
                Reflect::set(&options, &JsValue::from("recursive"), &JsValue::from_bool(true))?;
                let filename = filename.as_ref().to_platform_string();
                node::fs::mkdir_sync(&filename, options)?;
            }

            Ok(())
        }


        async fn fetch_metadata(path: &str, entries : &mut [DirEntry]) -> std::result::Result<(),JsErrorData> {
            for entry in entries.iter_mut() {
                let path = format!("{}/{}",path, entry.file_name());
                let metadata = node::fs::stat_sync(&path).unwrap();
                entry.metadata = metadata.try_into().ok();
            }

            Ok(())
        }

        async fn readdir_impl(path: &Path, metadata : bool) -> std::result::Result<Vec<DirEntry>,JsErrorData> {
            let path_string = path.to_string_lossy().to_string();
            let files = node::fs::readdir(&path_string).await?;
            let list = files.dyn_into::<js_sys::Array>().expect("readdir: expecting resulting entries to be an array");
            let mut entries = list.to_vec().into_iter().map(|s| s.into()).collect::<Vec<DirEntry>>();

            if metadata {
                fetch_metadata(&path_string, &mut entries).await?; //.map_err(|e|e.to_string())?;
            }

            Ok(entries)
        }

        pub async fn readdir<P>(path: P, metadata : bool) -> Result<Vec<DirEntry>>
        where P : AsRef<Path> + Send + 'static
        {
            // this is a hack to bypass JsFuture being !Send
            // until I had a chance to setup a proper infrastructure
            // to relay JS promises within Send contexts.
            // we want to use async version of readdir to ensure
            // our executor is not blocked.

            use workflow_core::sendable::Sendable;
            use workflow_core::task::dispatch;
            use workflow_core::channel::oneshot;

            if runtime::is_node() || runtime::is_nw() {

                let (sender, receiver) = oneshot();
                dispatch(async move {
                    let path = path.as_ref();
                    let result = readdir_impl(path, metadata).await;
                    sender.send(Sendable(result)).await.unwrap();
                });

                Ok(receiver.recv().await.unwrap().unwrap()?)
            } else if runtime::is_chrome_extension(){
                let entries = ChromeStorage::keys().await?
                    .into_iter()
                    .map(DirEntry::from)
                    .collect::<Vec<_>>();
                Ok(entries)
            } else{
                let local_storage = local_storage();

                let mut entries = vec![];
                let length = local_storage.length().unwrap();
                for i in 0..length {
                    let key = local_storage.key(i)?;
                    if let Some(key) = key {
                        entries.push(DirEntry::from(key));
                    }
                }
                Ok(entries)
            }
        }

        // -----------------------------------------

    } else {  // cfg_if - native platforms

        // -----------------------------------------

        pub async fn exists_with_options<P : AsRef<Path>>(filename: P, _options: Options) -> Result<bool> {
            Ok(filename.as_ref().exists())
        }

        pub fn exists_with_options_sync<P : AsRef<Path>>(filename: P, _options: Options) -> Result<bool> {
            Ok(filename.as_ref().exists())
        }

        pub async fn read_to_string_with_options<P : AsRef<Path>>(filename: P, _options: Options) -> Result<String> {
            Ok(std::fs::read_to_string(filename)?)
        }

        pub fn read_to_string_with_options_sync<P : AsRef<Path>>(filename: P, _options: Options) -> Result<String> {
            Ok(std::fs::read_to_string(filename)?)
        }

        pub async fn read_binary_with_options<P : AsRef<Path>>(filename: P, _options: Options) -> Result<Vec<u8>> {
            Ok(std::fs::read(filename)?)
        }

        pub fn read_binary_with_options_sync<P : AsRef<Path>>(filename: P, _options: Options) -> Result<Vec<u8>> {
            Ok(std::fs::read(filename)?)
        }

        pub async fn write_string_with_options<P : AsRef<Path>>(filename: P, _options: Options, text : &str) -> Result<()> {
            Ok(std::fs::write(filename, text)?)
        }

        pub fn write_string_with_options_sync<P : AsRef<Path>>(filename: P, _options: Options, text : &str) -> Result<()> {
            Ok(std::fs::write(filename, text)?)
        }

        pub async fn write_binary_with_options<P : AsRef<Path>>(filename: P, _options: Options, data : &[u8]) -> Result<()> {
            Ok(std::fs::write(filename, data)?)
        }

        pub fn write_binary_with_options_sync<P : AsRef<Path>>(filename: P, _options: Options, data : &[u8]) -> Result<()> {
            Ok(std::fs::write(filename, data)?)
        }

        pub async fn remove_with_options<P : AsRef<Path>>(filename: P, _options: Options) -> Result<()> {
            std::fs::remove_file(filename)?;
            Ok(())
        }

        pub fn remove_with_options_sync<P : AsRef<Path>>(filename: P, _options: Options) -> Result<()> {
            std::fs::remove_file(filename)?;
            Ok(())
        }

        pub async fn rename<P : AsRef<Path>>(from: P, to: P) -> Result<()> {
            std::fs::rename(from,to)?;
            Ok(())
        }

        pub fn rename_sync<P : AsRef<Path>>(from: P, to: P) -> Result<()> {
            std::fs::rename(from,to)?;
            Ok(())
        }

        pub async fn create_dir_all<P : AsRef<Path>>(dir: P) -> Result<()> {
            std::fs::create_dir_all(dir)?;
            Ok(())
        }

        pub fn create_dir_all_sync<P : AsRef<Path>>(dir: P) -> Result<()> {
            std::fs::create_dir_all(dir)?;
            Ok(())
        }

        pub async fn readdir<P : AsRef<Path>>(path: P, metadata : bool) -> Result<Vec<DirEntry>> {
            let entries = std::fs::read_dir(path.as_ref())?;

            if metadata {
                let mut list = Vec::new();
                for de in entries {
                    let de = de?;
                    let metadata = std::fs::metadata(de.path())?;
                    let dir_entry = DirEntry::from((de,metadata));
                    list.push(dir_entry);
                }
                Ok(list)
            } else {
                Ok(entries.map(|r|r.map(|e|e.into())).collect::<std::result::Result<Vec<_>,_>>()?)
            }
        }

    }

}

#[derive(Clone, Debug)]
pub struct Metadata {
    created: u64,
    modified: u64,
    accessed: u64,
    len: u64,
}

impl Metadata {
    pub fn created(&self) -> u64 {
        self.created
    }

    pub fn modified(&self) -> u64 {
        self.modified
    }

    pub fn accessed(&self) -> u64 {
        self.accessed
    }

    pub fn len(&self) -> u64 {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl From<std::fs::Metadata> for Metadata {
    fn from(metadata: std::fs::Metadata) -> Self {
        Metadata {
            created: metadata
                .created()
                .unwrap()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            modified: metadata
                .modified()
                .unwrap()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            accessed: metadata
                .accessed()
                .unwrap()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            len: metadata.len(),
        }
    }
}

impl TryFrom<JsValue> for Metadata {
    type Error = Error;
    fn try_from(metadata: JsValue) -> Result<Self> {
        if metadata.is_undefined() {
            return Err(Error::Metadata);
        }
        let created = (Reflect::get(&metadata, &"birthtimeMs".into())
            .unwrap()
            .as_f64()
            .unwrap()
            / 1000.0) as u64;
        let modified = (Reflect::get(&metadata, &"mtimeMs".into())
            .unwrap()
            .as_f64()
            .unwrap()
            / 1000.0) as u64;
        let accessed = (Reflect::get(&metadata, &"atimeMs".into())
            .unwrap()
            .as_f64()
            .unwrap()
            / 1000.0) as u64;
        let size = Reflect::get(&metadata, &"size".into())
            .unwrap()
            .as_f64()
            .unwrap() as u64;

        Ok(Metadata {
            created,
            modified,
            accessed,
            len: size,
        })
    }
}

#[derive(Clone, Debug)]
pub struct DirEntry {
    file_name: String,
    metadata: Option<Metadata>,
}

impl DirEntry {
    pub fn file_name(&self) -> &str {
        &self.file_name
    }

    pub fn metadata(&self) -> Option<&Metadata> {
        self.metadata.as_ref()
    }
}

impl From<std::fs::DirEntry> for DirEntry {
    fn from(de: std::fs::DirEntry) -> Self {
        DirEntry {
            file_name: de.file_name().to_string_lossy().to_string(),
            metadata: None,
        }
    }
}

impl From<(std::fs::DirEntry, std::fs::Metadata)> for DirEntry {
    fn from((de, metadata): (std::fs::DirEntry, std::fs::Metadata)) -> Self {
        DirEntry {
            file_name: de.file_name().to_string_lossy().to_string(),
            metadata: Some(metadata.into()),
        }
    }
}

impl From<JsValue> for DirEntry {
    fn from(de: JsValue) -> Self {
        DirEntry {
            file_name: de.as_string().unwrap(),
            metadata: None,
        }
    }
}

impl From<String> for DirEntry {
    fn from(s: String) -> Self {
        DirEntry {
            file_name: s,
            metadata: None,
        }
    }
}

/// Check if a file exists
pub async fn exists<P: AsRef<Path>>(filename: P) -> Result<bool> {
    exists_with_options(filename, Options::default()).await
}

/// Check if a file exists
pub fn exists_sync<P: AsRef<Path>>(filename: P) -> Result<bool> {
    exists_with_options_sync(filename, Options::default())
}

/// Read file contents to a string. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used.
pub async fn read_to_string(filename: &Path) -> Result<String> {
    read_to_string_with_options(filename, Options::default()).await
}

/// Read file contents to a string. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used.
pub fn read_to_string_sync(filename: &Path) -> Result<String> {
    read_to_string_with_options_sync(filename, Options::default())
}

/// Read binary file contents to a `Vec<u8>`. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used and the data is assumed to be hex-encoded.
pub async fn read(filename: &Path) -> Result<Vec<u8>> {
    read_binary_with_options(filename, Options::default()).await
}

/// Read binary file contents to a `Vec<u8>`. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used and the data is assumed to be hex-encoded.
pub fn read_sync(filename: &Path) -> Result<Vec<u8>> {
    read_binary_with_options_sync(filename, Options::default())
}

/// Write a string to a text file. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used.
pub async fn write_string(filename: &Path, text: &str) -> Result<()> {
    write_string_with_options(filename, Options::default(), text).await
}

/// Write a string to a text file. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used.
pub fn write_string_sync(filename: &Path, text: &str) -> Result<()> {
    write_string_with_options_sync(filename, Options::default(), text)
}

/// Write a `Vec<u8>` to a binary file. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used and the data will be hex-encoded.
pub async fn write(filename: &Path, data: &[u8]) -> Result<()> {
    write_binary_with_options(filename, Options::default(), data).await
}

/// Write a `Vec<u8>` to a binary file. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be used and the data will be hex-encoded.
pub async fn write_sync(filename: &Path, data: &[u8]) -> Result<()> {
    write_binary_with_options_sync(filename, Options::default(), data)
}

/// Remove the file at the given path. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be removed.
pub async fn remove(filename: &Path) -> Result<()> {
    remove_with_options(filename, Options::default()).await
}

/// Remove the file at the given path. If using within the web browser
/// environment, a local storage key with the name of the file
/// will be removed.
pub fn remove_sync(filename: &Path) -> Result<()> {
    remove_with_options_sync(filename, Options::default())
}

/// Read text file and deserialized it using `serde-json`.
pub async fn read_json_with_options<T>(filename: &Path, options: Options) -> Result<T>
where
    T: DeserializeOwned,
{
    let text = read_to_string_with_options(filename, options).await?;
    Ok(serde_json::from_str(&text)?)
}

/// Read text file and deserialized it using `serde-json`.
pub fn read_json_with_options_sync<T>(filename: &Path, options: Options) -> Result<T>
where
    T: DeserializeOwned,
{
    let text = read_to_string_with_options_sync(filename, options)?;
    Ok(serde_json::from_str(&text)?)
}

/// Write a serializable value to a text file using `serde-json`.
pub async fn write_json_with_options<T>(filename: &Path, options: Options, value: &T) -> Result<()>
where
    T: Serialize,
{
    let json = serde_json::to_string(value)?;
    write_string_with_options(filename, options, &json).await?;
    Ok(())
}

/// Write a serializable value to a text file using `serde-json`.
pub fn write_json_with_options_sync<T>(filename: &Path, options: Options, value: &T) -> Result<()>
where
    T: Serialize,
{
    let json = serde_json::to_string(value)?;
    write_string_with_options_sync(filename, options, &json)?;
    Ok(())
}

/// Read text file and deserialized it using `serde-json`.
pub async fn read_json<T>(filename: &Path) -> Result<T>
where
    T: DeserializeOwned,
{
    read_json_with_options(filename, Options::default()).await
}

/// Read text file and deserialized it using `serde-json`.
pub fn read_json_sync<T>(filename: &Path) -> Result<T>
where
    T: DeserializeOwned,
{
    read_json_with_options_sync(filename, Options::default())
}

/// Write a serializable value to a text file using `serde-json`.
pub async fn write_json<T>(filename: &Path, value: &T) -> Result<()>
where
    T: Serialize,
{
    write_json_with_options(filename, Options::default(), value).await
}

/// Write a serializable value to a text file using `serde-json`.
pub fn write_json_sync<T>(filename: &Path, value: &T) -> Result<()>
where
    T: Serialize,
{
    write_json_with_options_sync(filename, Options::default(), value)
}

/// Parses the supplied path resolving `~/` to the home directory.
pub fn resolve_path(path: &str) -> Result<PathBuf> {
    if let Some(_stripped) = path.strip_prefix("~/") {
        if runtime::is_web() {
            Ok(PathBuf::from(path))
        } else if runtime::is_node() || runtime::is_nw() {
            Ok(dirs::home_dir()
                .ok_or_else(|| Error::HomeDir(path.to_string()))?
                .join(_stripped))
        } else {
            cfg_if! {
                if #[cfg(target_arch = "wasm32")] {
                    Ok(PathBuf::from(path))
                } else {
                    Ok(home::home_dir().ok_or_else(||Error::HomeDir(path.to_string()))?.join(_stripped))
                }
            }
        }
    } else {
        Ok(PathBuf::from(path))
    }
}

/// Normalizes path, dereferencing relative references `.` and `..`
/// and converting path separators to current platform separators.
/// (detects platform natively or via NodeJS if operating in WASM32
/// environment)
pub trait NormalizePath {
    fn normalize(&self) -> Result<PathBuf>;
}

impl NormalizePath for Path {
    fn normalize(&self) -> Result<PathBuf> {
        normalize(self)
    }
}

impl NormalizePath for PathBuf {
    fn normalize(&self) -> Result<PathBuf> {
        normalize(self)
    }
}

/// Convert path separators to unix or to current platform.
/// Detects platform natively or using NodeJS if operating
/// under WASM32 environment. Since in WASM32 paths default
/// to forward slashes, when running WASM32 in Windows paths
/// needs to be converted back and forth for various path-related
/// functions to work.
pub trait ToPlatform {
    fn to_platform(&self) -> PathBuf;
    fn to_platform_string(&self) -> String;
    fn to_unix(&self) -> PathBuf;
}

impl ToPlatform for Path {
    fn to_platform(&self) -> PathBuf {
        if runtime::is_windows() {
            convert_path_separators(self, "/", "\\")
        } else {
            self.to_path_buf()
        }
    }

    fn to_platform_string(&self) -> String {
        self.to_platform().to_string_lossy().to_string()
    }

    fn to_unix(&self) -> PathBuf {
        if runtime::is_windows() {
            convert_path_separators(self, "\\", "/")
        } else {
            self.to_path_buf()
        }
    }
}

/// Normalizes path, dereferencing relative references `.` and `..`
/// and converting path separators to current platform separators.
/// (detects platform natively or via NodeJS if operating in WASM32
/// environment). Uses [`ToPlatform`] to perform path conversion.
pub fn normalize<P>(path: P) -> Result<PathBuf>
where
    P: AsRef<Path>,
{
    let path = path.as_ref().to_unix();
    let mut result = PathBuf::new();

    for component in path.components() {
        if let Some(c) = component.as_os_str().to_str() {
            if c == "." {
                continue;
            } else if c == ".." {
                result.pop();
            } else {
                result.push(c);
            }
        } else {
            return Err(Error::InvalidPath(path.to_string_lossy().to_string()));
        }
    }

    Ok(result.to_platform())
}

fn convert_path_separators<P>(path: P, from: &str, to: &str) -> PathBuf
where
    P: AsRef<Path>,
{
    let path = path.as_ref().to_string_lossy();
    let path = path.replace(from, to);
    PathBuf::from(path)
}