iepub 1.2.0

epub、mobi电子书读写
Documentation
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
use std::collections::HashMap;
use std::fmt::{Debug, Display};
use std::io::Write;
use std::sync::{Arc, Mutex};

use super::common::{self};
use super::html::{get_html_info, to_html};
use crate::cache_struct;
use crate::common::{escape_xml, IError, IResult};
use crate::epub::common::LinkRel;
crate::cache_enum!{
    #[derive(Clone)]
    pub enum Direction {
        RTL,
        LTR,
        CUS(String),
    }
}


impl Display for Direction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Direction::RTL => f.write_str("rtl"),
            Direction::LTR => f.write_str("ltr"),
            Direction::CUS(v) => f.write_fmt(format_args!("{}", escape_xml(v))),
        }
    }
}

impl From<String> for Direction {
    fn from(value: String) -> Self {
        if value.eq_ignore_ascii_case("rtl") {
            Direction::RTL
        } else if value.eq_ignore_ascii_case("ltr") {
            Direction::LTR
        } else {
            Direction::CUS(value)
        }
    }
}

pub(crate) mod info {
    include!(concat!(env!("OUT_DIR"), "/version.rs"));
}

macro_rules! epub_base_field{
    (
     // meta data about struct
     $(#[$meta:meta])*
     $vis:vis struct $struct_name:ident {
        $(
        // meta data about field
        $(#[$field_meta:meta])*
        $field_vis:vis $field_name:ident : $field_type:ty
        ),*$(,)?
    }
    ) => {

            crate::cache_struct!{
                $(#[$meta])*
                pub struct $struct_name{

                    id:String,
                    _file_name:String,
                    media_type:String,
                    _data: Option<Vec<u8>>,
                    #[cfg(not(feature="cache"))]
                    reader:Option<std::sync::Arc<std::sync::Mutex< Box<dyn EpubReaderTrait+Send+Sync>>>>,
                    #[cfg(feature="cache")]
                    #[serde(skip)]
                    reader:Option<std::sync::Arc<std::sync::Mutex< Box<dyn EpubReaderTrait+Send+Sync>>>>,
                    $(
                        $(#[$field_meta])*
                        $field_vis $field_name : $field_type,
                    )*

                }
            }

            impl $struct_name {
                ///
                /// 文件路径
                ///
                /// 注意,如果是 EPUB 目录下的文件,返回的时候不会带有EPUB路径
                ///
                pub fn file_name(&self)->&str{
                    self._file_name.as_str()
                }
                ///
                /// 设置文件路径
                ///
                pub fn set_file_name<T:Into<String>>(&mut self,value: T){
                    self._file_name = value.into();
                }

                pub fn id(&self)->&str{
                    self.id.as_str()
                }
                pub fn set_id<T:Into<String>>(&mut self,id: T){
                    self.id = id.into();
                }

                pub fn set_data(&mut self, data: Vec<u8>) {
                    // if let Some(d) = &mut self._data {
                    //     d.clear();
                    //     d.append(data);
                    // }else{
                        self._data = Some(data);
                    // }
                }
                pub fn with_file_name<T: Into<String>>(mut self,value: T)->Self{
                    self.set_file_name(value);
                    self
                }

                pub fn with_data(mut self, value:Vec<u8>)->Self{
                    self.set_data(value);
                    self
                }

            }


    }
}

crate::cache_struct! {
    /**
     * 链接文件,可能是css
     */
    #[derive(Debug)]
    pub struct EpubLink {
        pub rel: LinkRel,
        pub file_type: String,
        pub href: String,
    }
}

epub_base_field! {
    #[derive(Default)]
    pub struct EpubHtml {
        pub(crate) lang: String,
        links: Option<Vec<EpubLink>>,
        /// 章节名称
        title: String,
        /// 自定义的css,会被添加到link下
        css: Option<String>,
        /// 文件初始内容
        raw_data:Option<String>,
        /// 方向
       pub(crate) direction:Option<Direction>,
    }
}

impl Debug for EpubHtml {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EpubHtml")
            .field("id", &self.id)
            .field("_file_name", &self._file_name)
            .field("media_type", &self.media_type)
            .field("_data", &self._data)
            .field("lang", &self.lang)
            .field("links", &self.links)
            .field("title", &self.title)
            .field("css", &self.css)
            .finish()
    }
}

impl EpubHtml {
    pub fn string_data(&self) -> String {
        if let Some(data) = &self._data {
            String::from_utf8(data.clone()).unwrap_or_else(|_e| String::new())
        } else {
            String::new()
        }
    }

    pub fn data(&self) -> Option<&[u8]> {
        self._data.as_ref().map(|f| f.as_slice())
    }
    ///
    /// 获取数据
    ///
    /// 支持延迟读取
    ///
    pub fn data_mut(&mut self) -> Option<&[u8]> {
        let (id, origin) = if let Some(index) = self._file_name.find(|f| f == '#') {
            (
                Some(&self._file_name[(index + 1)..]),
                self._file_name[0..index].to_string(),
            )
        } else {
            (None, self.file_name().to_string())
        };
        let mut f = String::from(self._file_name.as_str());
        let prefixs = vec!["", common::EPUB, common::EPUB3];
        if self._data.is_none() && self.reader.is_some() && !f.is_empty() {
            for prefix in prefixs.iter() {
                // 添加 前缀再次读取
                f = format!("{prefix}{origin}");
                let s = self.reader.as_mut().unwrap();
                let d = s.lock().unwrap().read_string(f.as_str());
                match d {
                    Ok(v) => {
                        if let Ok((title, data, lang, direction)) = get_html_info(v.as_str(), id) {
                            if !title.is_empty() {
                                self.set_title(&title);
                            }
                            self.set_data(data);
                            if let Some(lang) = lang {
                                self.set_language(lang);
                            }
                            self.direction = direction;
                        }
                        break;
                    }
                    Err(IError::FileNotFound) => {}
                    Err(e) => {
                        break;
                    }
                }
            }
        }
        self._data.as_deref()
    }

    pub fn release_data(&mut self) {
        if let Some(data) = &mut self._data {
            data.clear();
        }
        self._data = None;
    }

    pub fn format(&mut self) -> Option<String> {
        self.data_mut();
        Some(to_html(self, false, &None))
    }

    pub fn raw_data(&mut self) -> Option<&str> {
        let (id, origin) = if let Some(index) = self._file_name.find(|f| f == '#') {
            (
                Some(&self._file_name[(index + 1)..]),
                self._file_name[0..index].to_string(),
            )
        } else {
            (None, self.file_name().to_string())
        };
        let mut f = String::from(self._file_name.as_str());
        let prefixs = vec!["", common::EPUB, common::EPUB3];
        if self.raw_data.is_none() && self.reader.is_some() && !f.is_empty() {
            for prefix in prefixs.iter() {
                // 添加 前缀再次读取
                f = format!("{prefix}{origin}");
                let s = self.reader.as_mut().unwrap();
                let d = s.lock().unwrap().read_string(f.as_str());
                if let Ok(data) = d {
                    self.raw_data = Some(data);
                }
            }
        }
        self.raw_data.as_deref()
    }

    pub fn release_raw_data(&mut self) {
        if let Some(data) = &mut self.raw_data {
            data.clear();
        }
        self.raw_data = None;
    }

    pub fn set_title<T: Into<String>>(&mut self, title: T) {
        self.title = title.into();
    }

    pub fn with_title<T: Into<String>>(mut self, title: T) -> Self {
        self.set_title(title);

        self
    }

    pub fn title(&self) -> &str {
        &self.title
    }

    pub fn set_css<T: Into<String>>(&mut self, css: T) {
        self.css = Some(css.into());
    }
    pub fn with_css<T: Into<String>>(mut self, css: T) -> Self {
        self.set_css(css);
        self
    }
    pub fn css(&self) -> Option<&str> {
        self.css.as_deref()
    }

    pub fn set_language<T: Into<String>>(&mut self, lang: T) {
        self.lang = lang.into();
    }

    pub fn with_language<T: Into<String>>(mut self, lang: T) -> Self {
        self.set_language(lang);
        self
    }

    pub fn links(&self) -> Option<std::slice::Iter<EpubLink>> {
        self.links.as_ref().map(|f| f.iter())
    }

    pub fn add_link(&mut self, link: EpubLink) {
        if let Some(links) = &mut self.links {
            links.push(link);
        } else {
            self.links = Some(vec![link]);
        }
    }

    fn get_links(&mut self) -> Option<&mut Vec<EpubLink>> {
        self.links.as_mut()
    }

    pub fn set_direction(&mut self, dir: Direction) {
        self.direction = Some(dir);
    }

    pub fn with_direction(mut self, dir: Direction) -> Self {
        self.set_direction(dir);
        self
    }
}

epub_base_field! {
///
/// 非章节资源
///
/// 例如css,字体,图片等
///
#[derive(Default,Clone)]
pub struct EpubAssets {
   pub(crate) version:String,
}
}

impl EpubAssets {
    pub fn with_version<T: Into<String>>(&mut self, version: T) {
        self.version = version.into();
    }

    pub fn data(&self) -> Option<&[u8]> {
        self._data.as_deref()
    }

    pub fn data_mut(&mut self) -> Option<&[u8]> {
        let mut f = String::from(self._file_name.as_str());
        if self._data.is_none() && self.reader.is_some() && !f.is_empty() {
            let prefixs = vec!["", common::EPUB, common::EPUB3];
            if self._data.is_none() && self.reader.is_some() && !f.is_empty() {
                for prefix in prefixs.iter() {
                    let s = self.reader.as_mut().unwrap();
                    // 添加 前缀再次读取
                    f = format!("{prefix}{}", self._file_name);
                    let d = s.lock().unwrap().read_file(f.as_str());
                    if let Ok(v) = d {
                        self.set_data(v);
                        break;
                    }
                }
            }
        }
        self._data.as_deref()
    }

    pub fn write_to<W: Write>(&mut self, writer: &mut W) -> IResult<()> {
        if let Some(data) = self.data_mut() {
            writer.write_all(&data)?;
            writer.flush()?;
        }
        Ok(())
    }

    pub fn save_to<T: AsRef<str>>(&mut self, file_path: T) -> IResult<()> {
        let mut f: String = self._file_name.clone();
        if self.reader.is_some() && !f.is_empty() {
            let prefixs = vec!["", common::EPUB, common::EPUB3];
            for prefix in prefixs.iter() {
                let s = self.reader.as_mut().unwrap();
                f = format!("{prefix}{}", self._file_name);
                let d: Result<(), IError> = s
                    .lock()
                    .unwrap()
                    .read_to_path(f.as_str(), file_path.as_ref());
                if d.is_ok() {
                    break;
                }
            }
        }
        Ok(())
    }

    pub fn release_data(&mut self) {
        if let Some(data) = &mut self._data {
            data.clear();
        }
        self._data = None;
    }
}

impl Debug for EpubAssets {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EpubAssets")
            .field("id", &self.id)
            .field("_file_name", &self._file_name)
            .field("media_type", &self.media_type)
            .field("_data", &self._data)
            .field("reader_mode", &self.reader.is_some())
            .finish()
    }
}

// impl Clone for EpubAssets {
//     fn clone(&self) -> Self {
//         Self {
//             id: self.id.clone(),
//             _file_name: self._file_name.clone(),
//             media_type: self.media_type.clone(),
//             _data: self._data.clone(),
//             reader: self.reader.clone(),
//         }
//     }
// }
epub_base_field! {
///
/// 目录信息
///
/// 支持嵌套
///
#[derive(Default)]
pub struct EpubNav {
    /// 章节目录
    /// 如果需要序号需要调用方自行处理
    title: String,
    child: Vec<EpubNav>,
}
}
impl Debug for EpubNav {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EpubNav")
            .field("id", &self.id)
            .field("_file_name", &self._file_name)
            .field("media_type", &self.media_type)
            .field("_data", &self._data)
            .field("title", &self.title)
            .field("child", &self.child)
            .finish()
    }
}

impl Clone for EpubNav {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            _file_name: self._file_name.clone(),
            media_type: self.media_type.clone(),
            _data: self._data.clone(),
            reader: None,
            title: self.title.clone(),
            child: self.child.clone(),
        }
    }
}

impl EpubNav {
    pub fn title(&self) -> &str {
        &self.title
    }
    pub fn set_title<T: Into<String>>(&mut self, title: T) {
        self.title = title.into();
    }
    pub fn with_title<T: Into<String>>(mut self, title: T) -> Self {
        self.set_title(title);
        self
    }
    ///
    ///
    /// 添加下级目录
    ///
    pub fn push(&mut self, child: EpubNav) {
        self.child.push(child);
    }

    pub fn child(&self) -> std::slice::Iter<EpubNav> {
        self.child.iter()
    }
}

cache_struct! {
///
/// 书籍元数据
///
/// 自定义的数据,不在规范内
///
#[derive(Debug, Default)]
pub struct EpubMetaData {
    /// 属性
    attr: HashMap<String, String>,
    /// 文本
    text: Option<String>,
}
}

impl EpubMetaData {
    pub fn with_attr<K: Into<String>>(mut self, key: K, value: K) -> Self {
        self.push_attr(key, value);
        self
    }

    pub fn push_attr<T: Into<String>>(&mut self, key: T, value: T) {
        self.attr.insert(key.into(), value.into());
    }
    pub fn with_text<T: Into<String>>(mut self, text: T) -> Self {
        self.set_text(text);
        self
    }

    pub fn set_text<T: Into<String>>(&mut self, text: T) {
        self.text = Some(text.into());
    }

    pub fn text(&self) -> Option<&str> {
        self.text.as_deref()
    }

    pub fn attrs(&self) -> std::collections::hash_map::Iter<'_, String, String> {
        self.attr.iter()
    }

    pub fn get_attr<T: AsRef<str>>(&self, key: T) -> Option<&String> {
        self.attr.get(key.as_ref())
    }
}
crate::cache_struct! {
/// 书本
#[derive(Default)]
pub struct EpubBook {
    /// 上次修改时间
    last_modify: Option<String>,
    /// epub电子书创建者信息
    generator: Option<String>,
    /// 书本信息
    info: crate::common::BookInfo,
    /// 元数据
    meta: Vec<EpubMetaData>,
    /// 目录信息
    nav: Vec<EpubNav>,
    /// 资源
    assets: Vec<EpubAssets>,
    /// 章节
    chapters: Vec<EpubHtml>,
    /// 封面
    cover: Option<EpubAssets>,
    /// 版本号
    version: String,
    /// 处于读模式
    #[cfg(not(feature="cache"))]
    reader:Option<std::sync::Arc<std::sync::Mutex< Box<dyn EpubReaderTrait+Send+Sync>>>>,
    #[cfg(feature="cache")]
    #[serde(skip)]
    reader:Option<std::sync::Arc<std::sync::Mutex< Box<dyn EpubReaderTrait+Send+Sync>>>>,
    /// PREFIX
    pub(crate) prefix: String,
    /// 方向
   pub(crate) direction: Option<Direction>,
   /// 语言
   language: Option<String>,
}
}

impl Display for EpubBook {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f,"last_modify={:?},info={:?},meta={:?},nav={:?},assets={:?},chapters={:?},cover={:?},is in read mode={}",
        self.last_modify,
        self.info,
        self.meta,
        self.nav,
        self.assets,
        self.chapters,
        self.cover,
        self.reader.is_some()
    )
    }
}

impl EpubBook {
    iepub_derive::option_string_method!(info, creator);
    iepub_derive::option_string_method!(info, description);
    iepub_derive::option_string_method!(info, contributor);
    iepub_derive::option_string_method!(info, date);
    iepub_derive::option_string_method!(info, format);
    iepub_derive::option_string_method!(info, publisher);
    iepub_derive::option_string_method!(info, subject);
    // /
    // / 设置epub最后修改时间
    // /
    // / # Examples
    // /
    // / ```
    // / let mut epub = EpubBook::default();
    // / epub.set_last_modify("2024-06-28T08:07:07UTC");
    // / ```
    // /
    iepub_derive::option_string_method!(last_modify);
    iepub_derive::option_string_method!(generator);
    iepub_derive::option_string_method!(language);
}

// 元数据
impl EpubBook {
    pub fn set_direction(&mut self, dir: Direction) {
        self.direction = Some(dir);
    }

    pub fn with_direction(mut self, dir: Direction) -> Self {
        self.set_direction(dir);
        self
    }

    pub fn set_title<T: AsRef<str>>(&mut self, title: T) {
        self.info.title.clear();
        self.info.title.push_str(title.as_ref());
    }
    pub fn title(&self) -> &str {
        self.info.title.as_str()
    }
    pub fn with_title<T: AsRef<str>>(mut self, title: T) -> Self {
        self.set_title(title.as_ref());
        self
    }
    pub fn identifier(&self) -> &str {
        self.info.identifier.as_str()
    }
    pub fn set_identifier<T: AsRef<str>>(&mut self, identifier: T) {
        self.info.identifier.clear();
        self.info.identifier.push_str(identifier.as_ref());
    }
    pub fn with_identifier<T: AsRef<str>>(mut self, identifier: T) -> Self {
        self.set_identifier(identifier.as_ref());
        self
    }

    ///
    /// 添加元数据
    ///
    /// # Examples
    ///
    /// ```
    /// use iepub::prelude::*;
    /// let mut epub = EpubBook::default();
    /// epub.add_meta(EpubMetaData::default().with_attr("k", "v").with_text("text"));
    /// ```
    ///
    pub fn add_meta(&mut self, meta: EpubMetaData) {
        self.meta.push(meta);
    }
    pub fn meta(&self) -> &[EpubMetaData] {
        &self.meta
    }

    pub fn get_meta_mut(&mut self, index: usize) -> Option<&mut EpubMetaData> {
        self.meta.get_mut(index)
    }
    pub fn get_meta(&self, index: usize) -> Option<&EpubMetaData> {
        self.meta.get(index)
    }

    pub fn meta_len(&self) -> usize {
        self.meta.len()
    }

    pub(crate) fn set_reader(
        &mut self,
        reader: Arc<Mutex<Box<dyn EpubReaderTrait + Send + Sync>>>,
    ) {
        self.reader = Some(reader)
    }

    ///
    /// 添加目录
    ///
    #[inline]
    pub fn add_nav(&mut self, nav: EpubNav) {
        self.nav.push(nav);
    }

    pub fn add_assets(&mut self, mut assets: EpubAssets) {
        if let Some(r) = &self.reader {
            assets.reader = Some(Arc::clone(r));
        }
        self.assets.push(assets);
    }

    ///
    /// 查找章节
    ///
    /// [file_name] 不需要带有 EPUB 目录
    ///
    pub fn get_assets<T: AsRef<str>>(&self, file_name: T) -> Option<&EpubAssets> {
        self.assets
            .iter()
            .find(|s| s.file_name() == file_name.as_ref())
    }

    ///
    /// 查找章节
    ///
    /// [file_name] 不需要带有 EPUB 目录
    ///
    pub fn get_assets_mut<T: AsRef<str>>(&mut self, file_name: T) -> Option<&mut EpubAssets> {
        self.assets
            .iter_mut()
            .find(|s| s.file_name() == file_name.as_ref())
    }

    pub fn assets(&self) -> std::slice::Iter<EpubAssets> {
        self.assets.iter()
    }

    pub fn assets_mut(&mut self) -> std::slice::IterMut<EpubAssets> {
        self.assets.iter_mut()
    }

    pub fn add_chapter(&mut self, mut chap: EpubHtml) {
        if let Some(r) = &self.reader {
            chap.reader = Some(Arc::clone(r));
        }
        self.chapters.push(chap);
    }

    pub fn chapters_mut(&mut self) -> std::slice::IterMut<EpubHtml> {
        self.chapters.iter_mut()
    }

    pub fn chapters(&self) -> std::slice::Iter<EpubHtml> {
        self.chapters.iter()
    }

    ///
    /// 查找章节
    ///
    /// [file_name] 不需要带有 EPUB 目录
    ///
    pub fn get_chapter<T: AsRef<str>>(&self, file_name: T) -> Option<&EpubHtml> {
        self.chapters.iter().find(|s| {
            return s.file_name() == file_name.as_ref();
        })
    }

    ///
    /// 查找章节
    ///
    /// [file_name] 不需要带有 EPUB 目录
    ///
    pub fn get_chapter_mut<T: AsRef<str>>(&mut self, file_name: T) -> Option<&mut EpubHtml> {
        self.chapters.iter_mut().find(|s| {
            return s.file_name() == file_name.as_ref();
        })
    }

    pub fn set_version<T: AsRef<str>>(&mut self, version: T) {
        self.version.clear();
        self.version.push_str(version.as_ref());
    }

    pub fn version(&self) -> &str {
        self.version.as_ref()
    }

    /// 获取目录
    pub fn nav(&self) -> std::slice::Iter<EpubNav> {
        self.nav.iter()
    }

    pub fn set_cover(&mut self, cover: EpubAssets) {
        self.cover = Some(cover);
    }

    pub fn cover(&self) -> Option<&EpubAssets> {
        self.cover.as_ref()
    }

    pub fn cover_mut(&mut self) -> Option<&mut EpubAssets> {
        self.cover.as_mut()
    }

    /// 读取完成后更新文章
    pub(crate) fn update_chapter(&mut self) {
        let f = flatten_nav(&self.nav);

        let mut map = HashMap::new();
        for (index, ele) in self.chapters.iter_mut().enumerate() {
            if let Some(v) = self.nav.iter().find(|f| f.file_name() == ele.file_name()) {
                ele.set_title(v.title());
            } else {
                // 如果 chapter 在 nav中不存在,有两种情况,一是cover之类的本身就不存在,二是epub3,在一个文件里使用id分章节
                let id_nav: Vec<&&EpubNav> = f
                    .iter()
                    .filter(|f| {
                        f.file_name().contains("#") && f.file_name().starts_with(ele.file_name())
                    })
                    .collect();
                if !id_nav.is_empty() {
                    // epub3,去除该 chap,重新填入
                    map.insert(index, id_nav);
                }
            }
        }
        // 修正章节
        let mut offset = 0;
        for (index, nav) in map {
            for ele in nav {
                let mut chap = EpubHtml::default()
                    .with_title(ele.title())
                    .with_file_name(ele.file_name());
                if let Some(r) = &self.reader {
                    chap.reader = Some(Arc::clone(r));
                }
                self.chapters.insert(index + offset, chap);
                offset = offset + 1;
            }
        }
    }

    pub(crate) fn update_assets(&mut self) {
        let version = self.version().to_string();
        for assets in self.assets_mut() {
            assets.with_version(&version);
        }
    }

    #[cfg(feature = "cache")]
    pub fn cache<T: AsRef<std::path::Path>>(&self, file: T) -> IResult<()> {
        std::fs::write(file, serde_json::to_string(self).unwrap())?;
        Ok(())
    }

    /// 加载缓存
    #[cfg(feature = "cache")]
    pub fn load_from_cache<T: AsRef<std::path::Path>>(file: T) -> IResult<EpubBook> {
        let file = std::fs::File::open(file)?;
        let reader = std::io::BufReader::new(file);

        // Read the JSON contents of the file as an instance of `User`.
        let u: EpubBook = serde_json::from_reader(reader)?;

        // Return the `User`.
        Ok(u)
    }
}

/// 获取最低层级的目录
fn flatten_nav(nav: &[EpubNav]) -> Vec<&EpubNav> {
    let mut n = Vec::new();
    for ele in nav {
        if ele.child.is_empty() {
            n.push(ele);
        } else {
            n.append(&mut flatten_nav(&ele.child));
        }
    }
    n
}
pub(crate) trait EpubReaderTrait: Send + Sync {
    fn read(&mut self, book: &mut EpubBook) -> IResult<()>;
    ///
    /// file epub中的文件目录
    ///
    fn read_file(&mut self, file_name: &str) -> IResult<Vec<u8>>;

    ///
    /// file epub中的文件目录
    ///
    fn read_string(&mut self, file_name: &str) -> IResult<String>;

    ///
    /// file epub中的文件目录
    ///
    fn read_to_path(&mut self, file_name: &str, file_path: &str) -> IResult<()>;
}

#[cfg(test)]
mod tests {

    use crate::prelude::*;

    fn book() -> EpubBook {
        let mut book = EpubBook::default();

        // 添加文本资源文件

        let mut css = EpubAssets::default();
        css.set_file_name("style/1.css");
        css.set_data(String::from("ok").as_bytes().to_vec());

        book.add_assets(css);

        // 添加目录,注意目录和章节并无直接关联关系,需要自行维护保证导航到正确位置
        let mut n = EpubNav::default();
        n.set_title("作品说明");
        n.set_file_name("chaps/0.xhtml");

        let mut n1 = EpubNav::default();
        n1.set_title("第一卷");

        let mut n2 = EpubNav::default();
        n2.set_title("第一卷 第一章");
        n2.set_file_name("chaps/1.xhtml");

        let mut n3 = EpubNav::default();
        n3.set_title("第一卷 第二章");
        n3.set_file_name("chaps/2.xhtml");
        n1.push(n2);

        book.add_nav(n);
        book.add_nav(n1);
        book.set_version("2.0");
        // 添加章节
        let mut chap = EpubHtml::default();
        chap.set_file_name("chaps/0.xhtml");
        chap.set_title("标题1");
        // 章节的数据并不需要填入完整的html,只需要片段即可,输出时会结合其他数据拼接成完整的html
        chap.set_data(String::from("<p>章节内容html片段</p>").as_bytes().to_vec());

        book.add_chapter(chap);

        chap = EpubHtml::default();
        chap.set_file_name("chaps/1.xhtml");
        chap.set_title("标题2");
        chap.set_data(String::from("第一卷 第一章content").as_bytes().to_vec());

        book.add_chapter(chap);
        chap = EpubHtml::default();
        chap.set_file_name("chaps/2.xhtml");
        chap.set_title("标题2");
        chap.set_data(String::from("第一卷 第二章content").as_bytes().to_vec());

        book.add_chapter(chap);

        book.set_title("书名");
        book.set_creator("作者");
        book.set_identifier("id");
        book.set_description("desc");
        book.set_date("29939");
        book.set_subject("subject");
        book.set_format("format");
        book.set_publisher("publisher");
        book.set_contributor("contributor");
        // epub.cover = Some(EpubAssets::default());

        let mut cover = EpubAssets::default();
        cover.set_file_name("cover.jpg");

        let data = vec![2];
        cover.set_data(data);

        book.set_cover(cover);
        book
    }

    #[test]
    fn write_assets() {
        let mut book: EpubBook = book();

        // EpubWriter::write_to_file("file", &mut book).unwrap();

        EpubWriter::write_to_file("12.epub", &mut book, true).unwrap();

        // EpubWriter::<std::fs::File>write_to_file("../target/test.epub", &mut book).expect("write error");
    }

    #[test]
    #[cfg(feature = "cache")]
    fn test_cache() {
        let mut book: EpubBook = book();
        let f = if std::path::Path::new("target").exists() {
            "target/cache.json"
        } else {
            "../target/cache.json"
        };
        book.cache(f).unwrap();

        let book2 = EpubBook::load_from_cache(f).unwrap();

        assert_eq!(book.chapters.len(), book2.chapters.len());
        assert_eq!(book.chapters[0]._data, book2.chapters[0]._data);
        assert_eq!(book.assets[0]._data, book2.assets[0]._data);
    }
}