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
use std::{path::PathBuf, str::FromStr};

use super::{disk_resource::DiskResource, ResourceError, Resource};



/// Resource which uses filesystem to store information. It use simple cache logics avoid duplicating read and write operations
#[derive(Debug, Clone)]
pub struct CachedDiskResource {
    name: String, 
    location: PathBuf,
    shadow_resource: DiskResource,
    cached_content: Option<String>
}

impl FromStr for CachedDiskResource {
    type Err = ResourceError;

    fn from_str(path: &str) -> Result<Self, Self::Err> {

        if path.is_empty() {
            return Err(ResourceError::Creation("resource cannot be an empty string".to_string()));
        }

        Self::try_from(PathBuf::from_str(path).unwrap())
    }
}


impl ToString for CachedDiskResource {
    fn to_string(&self) -> String {
        self.location().to_string_lossy().to_string()
    }
}

impl TryFrom<PathBuf> for CachedDiskResource {
    type Error = ResourceError;

    fn try_from(location: PathBuf) -> Result<Self, Self::Error> {
        if location.is_dir() {
            return Err(ResourceError::InvalidResourceVerbose(format!("{} is a directory", location.to_string_lossy())))
        }

        if let Some(name) = location.file_name() {

            let l = location.clone();

            Ok(Self {
                name: name.to_string_lossy().to_string(),
                location: l.clone(),
                shadow_resource: DiskResource::try_from(l)?,
                cached_content: Option::None
            })
        } else {
            Err(ResourceError::InvalidResource)
        }
    }
}


#[allow(dead_code)]
impl CachedDiskResource {
    fn new(location: PathBuf) -> Result<Self, ResourceError> {

        Self::try_from(location)
    }

    pub fn cached_content(&self) -> &Option<String> {
        &self.cached_content
    }

    pub fn set_cached_content(&mut self, content: &str) -> () {
        self.cached_content = Some(content.to_string())
    }

    pub fn append_to_cached_content(&mut self, content: &str) -> () {
        if self.cached_content.is_some() {
            self.cached_content = Option::Some(self.cached_content.clone().unwrap() + content);
            
        } else {
            self.cached_content = Option::Some(content.to_string())
        }
    }

    pub fn dump_cached_content(&mut self) -> Result<(), ResourceError> {
        self.shadow_resource.write(self.cached_content.as_ref().unwrap().as_str())
    }

    pub fn clear_cached_content(&mut self) -> () {
        self.cached_content = Option::None
    }

    pub fn refresh_cached_content(&mut self) -> Result<(), ResourceError> {
        self.cached_content = Option::Some(self.shadow_resource.read()?);

        Ok(())   
    }
}

impl Resource for CachedDiskResource {
    type LocationType = PathBuf;

    fn write(&mut self, content: &str) -> Result<(), ResourceError> {

        self.set_cached_content(content);

        self.shadow_resource.write(self.cached_content.as_ref().unwrap().as_str())
    }

    fn append(&mut self, content: &str) -> Result<(), ResourceError> {

        self.append_to_cached_content(content);

        self.shadow_resource.append(self.cached_content.as_ref().unwrap().as_str())
    }

    fn read(&self) -> Result<String, ResourceError> {

        match &self.cached_content {
            Some(content) => Ok(content.clone()),
            None => self.shadow_resource.read()
        }
    }

    fn name(&self) -> &String {
        &self.name
    }

    fn location(&self) -> &Self::LocationType {
        &self.location
    }

    fn erase(&mut self) -> Result<(), ResourceError> {
        self.shadow_resource.erase()
    }
}