unity_asset_yaml/
python_like_api.rs1use crate::YamlDocument;
7use std::path::Path;
8use unity_asset_core::{
9 DynamicAccess, DynamicValue, Result, UnityAssetError, UnityClass,
10 UnityDocument as UnityDocumentTrait,
11};
12
13pub struct PythonLikeUnityDocument {
23 inner: YamlDocument,
25}
26
27impl PythonLikeUnityDocument {
28 pub fn load_yaml<P: AsRef<Path>>(file_path: P, try_preserve_types: bool) -> Result<Self> {
42 let inner = YamlDocument::load_yaml(file_path, try_preserve_types)?;
43 Ok(Self { inner })
44 }
45
46 pub fn entry(&self) -> Option<PythonLikeUnityClass<'_>> {
48 self.inner.entries().first().map(PythonLikeUnityClass::new)
49 }
50
51 pub fn entries(&self) -> Vec<PythonLikeUnityClass<'_>> {
53 self.inner
54 .entries()
55 .iter()
56 .map(PythonLikeUnityClass::new)
57 .collect()
58 }
59
60 pub fn filter(
76 &self,
77 class_names: Option<&[&str]>,
78 attributes: Option<&[&str]>,
79 ) -> Vec<PythonLikeUnityClass<'_>> {
80 self.inner
81 .filter(class_names, attributes)
82 .iter()
83 .map(|class| PythonLikeUnityClass::new(class))
84 .collect()
85 }
86
87 pub fn get(
89 &self,
90 class_name: Option<&str>,
91 attributes: Option<&[&str]>,
92 ) -> Result<PythonLikeUnityClass<'_>> {
93 let class = self.inner.get(class_name, attributes)?;
94 Ok(PythonLikeUnityClass::new(class))
95 }
96
97 pub fn dump_yaml(&self) -> Result<()> {
99 self.inner.save()
100 }
101
102 pub fn dump_yaml_to<P: AsRef<Path>>(&self, file_path: P) -> Result<()> {
104 self.inner.save_to(file_path)
105 }
106
107 pub fn inner(&self) -> &YamlDocument {
109 &self.inner
110 }
111
112 pub fn inner_mut(&mut self) -> &mut YamlDocument {
114 &mut self.inner
115 }
116}
117
118pub struct PythonLikeUnityClass<'a> {
127 class: &'a UnityClass,
129}
130
131impl<'a> PythonLikeUnityClass<'a> {
132 fn new(class: &'a UnityClass) -> Self {
134 Self { class }
135 }
136
137 pub fn get_string(&self, key: &str) -> Option<String> {
156 self.class
157 .get_dynamic(key)?
158 .as_string()
159 .map(|s| s.to_string())
160 }
161
162 pub fn get_integer(&self, key: &str) -> Option<i64> {
164 self.class.get_dynamic(key)?.as_integer()
165 }
166
167 pub fn get_float(&self, key: &str) -> Option<f64> {
169 self.class.get_dynamic(key)?.as_float()
170 }
171
172 pub fn get_bool(&self, key: &str) -> Option<bool> {
174 self.class.get_dynamic(key)?.as_bool()
175 }
176
177 pub fn get_array(&self, key: &str) -> Option<Vec<DynamicValue>> {
179 self.class.get_dynamic(key)?.as_array().cloned()
180 }
181
182 pub fn get_dynamic(&self, key: &str) -> Option<DynamicValue> {
184 self.class.get_dynamic(key)
185 }
186
187 pub fn has_property(&self, key: &str) -> bool {
189 self.class.has_dynamic(key)
190 }
191
192 pub fn property_names(&self) -> Vec<String> {
194 self.class.keys_dynamic()
195 }
196
197 pub fn class_name(&self) -> &str {
199 &self.class.class_name
200 }
201
202 pub fn class_id(&self) -> i32 {
204 self.class.class_id
205 }
206
207 pub fn anchor(&self) -> &str {
209 &self.class.anchor
210 }
211
212 pub fn inner(&self) -> &UnityClass {
214 self.class
215 }
216}
217
218pub struct PythonLikeUnityClassMut<'a> {
220 class: &'a mut UnityClass,
222}
223
224impl<'a> PythonLikeUnityClassMut<'a> {
225 pub fn new(class: &'a mut UnityClass) -> Self {
227 Self { class }
228 }
229
230 pub fn set_string(&mut self, key: &str, value: &str) -> Result<()> {
232 let dynamic_value = DynamicValue::String(value.to_string());
233 self.class.set_dynamic(key, dynamic_value)
234 }
235
236 pub fn set_integer(&mut self, key: &str, value: i64) -> Result<()> {
238 let dynamic_value = DynamicValue::Integer(value);
239 self.class.set_dynamic(key, dynamic_value)
240 }
241
242 pub fn set_float(&mut self, key: &str, value: f64) -> Result<()> {
244 let dynamic_value = DynamicValue::Float(value);
245 self.class.set_dynamic(key, dynamic_value)
246 }
247
248 pub fn set_bool(&mut self, key: &str, value: bool) -> Result<()> {
250 let dynamic_value = DynamicValue::Bool(value);
251 self.class.set_dynamic(key, dynamic_value)
252 }
253
254 pub fn set_dynamic(&mut self, key: &str, value: DynamicValue) -> Result<()> {
256 self.class.set_dynamic(key, value)
257 }
258
259 pub fn add_to_numeric(&mut self, key: &str, value: f64) -> Result<()> {
261 if let Some(mut current) = self.class.get_dynamic(key) {
262 current.add_numeric(value)?;
263 self.class.set_dynamic(key, current)?;
264 } else {
265 return Err(UnityAssetError::format(format!(
266 "Property '{}' not found",
267 key
268 )));
269 }
270 Ok(())
271 }
272
273 pub fn concat_to_string(&mut self, key: &str, value: &str) -> Result<()> {
275 if let Some(mut current) = self.class.get_dynamic(key) {
276 current.concat_string(value)?;
277 self.class.set_dynamic(key, current)?;
278 } else {
279 return Err(UnityAssetError::format(format!(
280 "Property '{}' not found",
281 key
282 )));
283 }
284 Ok(())
285 }
286
287 pub fn get_string(&self, key: &str) -> Option<String> {
289 self.class
290 .get_dynamic(key)?
291 .as_string()
292 .map(|s| s.to_string())
293 }
294
295 pub fn get_integer(&self, key: &str) -> Option<i64> {
297 self.class.get_dynamic(key)?.as_integer()
298 }
299
300 pub fn get_float(&self, key: &str) -> Option<f64> {
302 self.class.get_dynamic(key)?.as_float()
303 }
304
305 pub fn get_bool(&self, key: &str) -> Option<bool> {
307 self.class.get_dynamic(key)?.as_bool()
308 }
309
310 pub fn inner(&self) -> &UnityClass {
312 self.class
313 }
314
315 pub fn inner_mut(&mut self) -> &mut UnityClass {
317 self.class
318 }
319}
320
321#[cfg(test)]
322mod tests {
323 #[test]
324 fn test_python_like_api() {
325 assert_eq!(2 + 2, 4);
328 }
329}