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
use crate::{ utils, FileHolder, Resource, SerializedData, SerializedDataError, YyResource, YyResourceHandler, YypBoss, }; use std::path::Path; use yy_typings::{utils::TrailingCommaUtility, Note, ViewPath}; impl YyResource for Note { type AssociatedData = String; const SUBPATH_NAME: &'static str = "notes"; const RESOURCE: Resource = Resource::Note; fn name(&self) -> &str { &self.name } fn set_name(&mut self, name: String) { self.name = name; } fn set_parent_view_path(&mut self, vp: ViewPath) { self.parent = vp; } fn parent_view_path(&self) -> ViewPath { self.parent.clone() } fn get_handler(yyp_boss: &YypBoss) -> &YyResourceHandler<Self> { &yyp_boss.notes } fn get_handler_mut(yyp_boss: &mut YypBoss) -> &mut YyResourceHandler<Self> { &mut yyp_boss.notes } fn serialize_associated_data( &self, directory_path: &std::path::Path, data: &Self::AssociatedData, ) -> anyhow::Result<()> { let file = directory_path.join(&self.name).with_extension("txt"); std::fs::write(file, data)?; Ok(()) } fn deserialize_associated_data( &self, directory_path: &Path, _: &TrailingCommaUtility, ) -> Result<Self::AssociatedData, SerializedDataError> { let path = directory_path.join(format!("{}.txt", self.name)); std::fs::read_to_string(path).map_err(|e| { SerializedDataError::CouldNotDeserializeFile(crate::FileSerializationError::Io( e.to_string(), )) }) } fn serialize_associated_data_into_data( _: &std::path::Path, associated_data: &Self::AssociatedData, ) -> Result<SerializedData, SerializedDataError> { Ok(SerializedData::Value { data: associated_data.clone(), }) } fn deserialize_associated_data_from_data( &self, incoming_data: &SerializedData, tcu: &TrailingCommaUtility, ) -> Result<Self::AssociatedData, SerializedDataError> { match incoming_data { SerializedData::Value { data: v } => Ok(v.to_string()), SerializedData::Filepath { data: v } => { utils::deserialize_json_tc(v, tcu).map_err(|e| e.into()) } SerializedData::DefaultValue => Ok(Self::AssociatedData::default()), } } fn cleanup_on_replace(&self, _: impl FileHolder) {} }