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
use crate::{FileSerializationError, Resource, YyResourceHandler, YypBoss};
use serde::{Deserialize, Serialize};
use std::{
fmt::Debug,
path::{Path, PathBuf},
};
use yy_typings::{utils::TrailingCommaUtility, FilesystemPath, ViewPath};
pub trait YyResource: Serialize + for<'de> Deserialize<'de> + Clone + Default + PartialEq {
type AssociatedData: Debug + Clone + PartialEq;
const SUBPATH_NAME: &'static str;
const RESOURCE: Resource;
fn relative_yy_directory(&self) -> PathBuf {
self.relative_yy_filepath().parent().unwrap().to_owned()
}
fn relative_yy_filepath(&self) -> PathBuf {
FilesystemPath::new_path(Self::SUBPATH_NAME, self.name())
}
fn name(&self) -> &str;
fn set_name(&mut self, name: String);
fn set_parent_view_path(&mut self, vp: ViewPath);
fn parent_view_path(&self) -> ViewPath;
fn relative_path(&self) -> PathBuf {
Path::new(&format!("{}/{}", Self::SUBPATH_NAME, self.name())).to_owned()
}
fn get_handler(yyp_boss: &YypBoss) -> &YyResourceHandler<Self>;
fn get_handler_mut(yyp_boss: &mut YypBoss) -> &mut YyResourceHandler<Self>;
fn deserialize_associated_data(
&self,
incoming_data: AssocDataLocation<'_>,
tcu: &TrailingCommaUtility,
) -> Result<Self::AssociatedData, SerializedDataError>;
fn serialize_associated_data(
&self,
directory_path: &Path,
data: &Self::AssociatedData,
) -> anyhow::Result<()>;
fn serialize_associated_data_into_data(
working_directory: &Path,
associated_data: &Self::AssociatedData,
) -> Result<SerializedData, SerializedDataError>;
fn cleanup_on_replace(&self, paths_to_delete: impl FileHolder);
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "dataType")]
pub enum SerializedData {
Value { data: String },
Filepath { data: PathBuf },
DefaultValue,
}
#[derive(Debug, thiserror::Error)]
pub enum SerializedDataError {
#[error("given a `SerializedData::File` tag, but path didn't exist, wasn't a file, or couldn't be read. path was {}", .0.to_string_lossy())]
BadDataFile(std::path::PathBuf),
#[error(transparent)]
CouldNotDeserializeFile(#[from] FileSerializationError),
#[error(transparent)]
CouldNotWriteImage(#[from] image::ImageError),
#[error(
"cannot be represented with utf8 encoding; must use `SerializedData::File` or `SerializedData::DefaultValue`"
)]
CannotUseValue,
#[error("internal error -- yyp is unstable...{}", .0)]
InnerError(String),
}
impl From<serde_json::Error> for SerializedDataError {
fn from(e: serde_json::Error) -> Self {
SerializedDataError::CouldNotDeserializeFile(FileSerializationError::Serde(e.to_string()))
}
}
impl SerializedData {
pub fn as_assoc_data_location(&self) -> AssocDataLocation<'_> {
match self {
SerializedData::Value { data } => AssocDataLocation::Value(data),
SerializedData::Filepath { data } => AssocDataLocation::Path(data.as_ref()),
SerializedData::DefaultValue => AssocDataLocation::Default,
}
}
}
#[derive(Debug)]
pub enum AssocDataLocation<'a> {
Value(&'a str),
Path(&'a Path),
Default,
}
pub trait FileHolder {
fn push(&mut self, f: PathBuf);
}