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
59pub trait Resource: FromStr {
61
62 type LocationType;
63
64 fn write(&mut self, content: &str) -> Result<(), ResourceError>;
66
67 fn erase(&mut self) -> Result<(), ResourceError>;
69
70 fn append(&mut self, content: &str) -> Result<(), ResourceError>;
72
73 fn read(&self) -> Result<String, ResourceError>;
75
76 fn content(&self) -> Result<String, ResourceError> {
78 self.read()
79 }
80
81 fn name(&self) -> &String;
83
84 fn location(&self) -> &Self::LocationType;
86}