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
use super::{dir_node::DirNode, node::Node};
use std::{cell::RefCell, rc::Rc};

pub struct FileNode {
    name: String,
    parent_node: Option<Rc<RefCell<DirNode>>>,
    cache_offset: i64,
    timestamp: i64,
    comp_len: i32,
    len: i32,
    toc_offset: i64,
}

impl Node for FileNode {
    fn name(&self) -> &String {
        &self.name
    }

    fn parent(&self) -> Option<Rc<RefCell<dyn Node>>> {
        match &self.parent_node {
            Some(parent) => Some(parent.to_owned()),
            None => None,
        }
    }

    fn toc_offset(&self) -> i64 {
        self.toc_offset
    }

    fn path(&self) -> String {
        let mut path = self.parent_node.clone().unwrap().borrow().path();
        path = format!("{}/{}", path, self.name);
        path
    }
}

impl FileNode {
    pub fn new(
        name: Option<String>,
        parent_node: Option<Rc<RefCell<DirNode>>>,
        cache_offset: Option<i64>,
        timestamp: Option<i64>,
        comp_len: Option<i32>,
        len: Option<i32>,
        toc_offset: Option<i64>,
    ) -> Self {
        Self {
            name: name.unwrap_or(String::new()),
            parent_node,
            cache_offset: cache_offset.unwrap_or(i64::MAX),
            timestamp: timestamp.unwrap_or(0),
            comp_len: comp_len.unwrap_or(0),
            len: len.unwrap_or(0),
            toc_offset: toc_offset.unwrap_or(0),
        }
    }

    pub fn set_data(
        &mut self,
        name: Option<String>,
        parent_node: Option<Rc<RefCell<DirNode>>>,
        cache_offset: Option<i64>,
        timestamp: Option<i64>,
        comp_len: Option<i32>,
        len: Option<i32>,
        toc_offset: Option<i64>,
    ) {
        if name.is_some() {
            self.name = name.unwrap();
        }
        if parent_node.is_some() {
            self.parent_node = parent_node;
        }
        if cache_offset.is_some() {
            self.cache_offset = cache_offset.unwrap();
        }
        if timestamp.is_some() {
            self.timestamp = timestamp.unwrap();
        }
        if comp_len.is_some() {
            self.comp_len = comp_len.unwrap();
        }
        if len.is_some() {
            self.len = len.unwrap();
        }
        if toc_offset.is_some() {
            self.toc_offset = toc_offset.unwrap();
        }
    }

    pub fn cache_offset(&self) -> i64 {
        self.cache_offset
    }

    pub fn timestamp(&self) -> i64 {
        self.timestamp
    }

    pub fn comp_len(&self) -> i32 {
        self.comp_len
    }

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