starlane_core/resource/
file.rs1use std::convert::{TryFrom, TryInto};
2use std::sync::Arc;
3
4use serde::{Deserialize, Serialize};
5
6use starlane_resources::{AssignResourceStateSrc, LocalStateSetSrc, Resource};
7use starlane_resources::data::{BinSrc, DataSet};
8
9use crate::error::Error;
10use crate::resource::{
11 FileKey, ResourceAddress, ResourceType,
12};
13
14#[derive(Clone)]
15pub struct File {
16 key: FileKey,
17 address: ResourceAddress,
18 state_src: DataSet<BinSrc>,
19}
20
21#[derive(Clone, Serialize, Deserialize)]
22pub struct FileState {
23 content: Arc<Vec<u8>>,
24}
25
26impl FileState {
27 pub fn new(content: Arc<Vec<u8>>) -> Self {
28 FileState { content: content }
29 }
30}
31
32impl TryInto<Vec<u8>> for FileState {
33 type Error = Error;
34
35 fn try_into(self) -> Result<Vec<u8>, Self::Error> {
36 Ok(bincode::serialize(&self)?)
37 }
38}
39
40impl TryInto<Arc<Vec<u8>>> for FileState {
41 type Error = Error;
42
43 fn try_into(self) -> Result<Arc<Vec<u8>>, Self::Error> {
44 Ok(Arc::new(bincode::serialize(&self)?))
45 }
46}
47
48impl TryFrom<Arc<Vec<u8>>> for FileState {
49 type Error = Error;
50
51 fn try_from(value: Arc<Vec<u8>>) -> Result<Self, Self::Error> {
52 Ok(bincode::deserialize::<FileState>(value.as_slice())?)
53 }
54}
55
56impl TryFrom<Vec<u8>> for FileState {
57 type Error = Error;
58
59 fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
60 Ok(bincode::deserialize::<FileState>(value.as_slice())?)
61 }
62}