nmd_core/
resource.rs

1pub mod disk_resource;
2pub mod cached_disk_resource;
3pub mod image_resource;
4pub mod remote_resource;
5pub mod dynamic_resource;
6pub mod resource_reference;
7pub mod text_reference;
8pub mod table;
9pub mod source;
10pub mod bucket;
11
12
13use std::{str::FromStr, io::{self}};
14use resource_reference::ResourceReferenceError;
15use thiserror::Error;
16
17
18#[derive(Error, Debug)]
19pub enum ResourceError {
20
21    #[error("resource '{0}' not found")]
22    ResourceNotFound(String),
23
24    #[error("wrong elaboration: '{0}'")]
25    WrongElaboration(String),
26
27    #[error("resource is invalid")]
28    InvalidResource,
29
30    #[error("resource is invalid because: {0}")]
31    InvalidResourceVerbose(String),
32
33    #[error("resource cannot be created: {0}")]
34    Creation(String),
35
36    #[error("resource '{0}' cannot be read")]
37    ReadError(String),
38
39    #[error(transparent)]
40    IoError(#[from] io::Error),
41    
42    #[error("elaboration error: {0}")]
43    ElaborationError(String),
44
45    #[error(transparent)]
46    ResourceReferenceError(#[from] ResourceReferenceError),
47}
48
49impl Clone for ResourceError {
50    fn clone(&self) -> Self {
51        match self {
52            Self::IoError(e) => Self::ElaborationError(e.to_string()),
53            other => other.clone()
54        }
55    }
56}
57
58
59/// General physical or virtual resource
60pub trait Resource: FromStr {
61
62    type LocationType;
63
64    /// write resource content
65    fn write(&mut self, content: &str) -> Result<(), ResourceError>;
66
67    /// erase content resource
68    fn erase(&mut self) -> Result<(), ResourceError>;
69
70    /// append resource content
71    fn append(&mut self, content: &str) -> Result<(), ResourceError>;
72
73    /// read resource content
74    fn read(&self) -> Result<String, ResourceError>;
75
76    /// return resource content
77    fn content(&self) -> Result<String, ResourceError> {
78        self.read()        
79    }
80
81    /// return resource name
82    fn name(&self) -> &String;
83
84    /// return embedded location type (e.g. PathBuf for files)
85    fn location(&self) -> &Self::LocationType;
86}