changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / obj/src/lib.rs

changeset 11: d8f806f1d327
parent: 315fedf35bc7
author: ellis <ellis@rwest.io>
date: Sun, 14 May 2023 21:27:04 -0400
permissions: -rw-r--r--
description: obj updates
1 //! obj/src/lib.rs --- Objective type library
2 #![feature(associated_type_bounds)]
3 mod err;
4 pub use err::{Error, Result};
5 mod types;
6 pub use types::*;
7 pub mod id;
8 pub mod auth;
9 pub mod hash;
10 pub mod network;
11 pub mod database;
12 pub use bincode;
13 pub use ron;
14 use ron::extensions::Extensions;
15 use serde::{de::DeserializeOwned, Deserialize, Serialize};
16 pub use serde_json;
17 use std::collections::{BTreeMap, HashMap};
18 use std::io;
19 
20 /// Objective trait
21 /// Define Object behaviors, implemented by Objects
22 pub trait Objective {
23  fn encode(&self) -> Result<Vec<u8>>
24  where
25  Self: Serialize,
26  {
27  Ok(bincode::serialize(self)?)
28  }
29 
30  fn encode_into<W>(&self, writer: W) -> Result<()>
31  where
32  W: io::Write,
33  Self: Serialize,
34  {
35  Ok(bincode::serialize_into(writer, self)?)
36  }
37 
38  fn decode<'a>(bytes: &'a [u8]) -> Result<Self>
39  where
40  Self: Deserialize<'a>,
41  {
42  Ok(bincode::deserialize(bytes)?)
43  }
44 
45  fn decode_from<R>(&self, rdr: R) -> Result<Self>
46  where
47  R: io::Read,
48  Self: DeserializeOwned,
49  {
50  Ok(bincode::deserialize_from(rdr)?)
51  }
52 
53  fn to_ron_writer<W>(&self, writer: W) -> Result<()>
54  where
55  W: io::Write,
56  Self: Serialize,
57  {
58  Ok(ron::ser::to_writer_pretty(
59  writer,
60  &self,
61  ron::ser::PrettyConfig::new()
62  .indentor(" ".to_owned())
63  .extensions(Extensions::all()),
64  )?)
65  }
66 
67  fn to_ron_string(&self) -> Result<String>
68  where
69  Self: Serialize,
70  {
71  Ok(ron::ser::to_string_pretty(
72  &self,
73  ron::ser::PrettyConfig::new().indentor(" ".to_owned()),
74  )?)
75  }
76 
77  fn from_ron_reader<R>(&self, mut rdr: R) -> Result<Self>
78  where
79  R: io::Read,
80  Self: DeserializeOwned,
81  {
82  let mut bytes = Vec::new();
83  rdr.read_to_end(&mut bytes)?;
84  Ok(ron::de::from_bytes(&bytes)?)
85  }
86 
87  fn from_ron_str<'a>(s: &'a str) -> Result<Self>
88  where
89  Self: Deserialize<'a>,
90  {
91  Ok(ron::de::from_bytes(s.as_bytes())?)
92  }
93 
94  fn to_json_writer<W>(&self, writer: W) -> Result<()>
95  where
96  W: io::Write,
97  Self: Serialize,
98  {
99  // let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
100  Ok(serde_json::ser::to_writer_pretty(writer, &self)?)
101  }
102 
103  fn to_json_string(&self) -> Result<String>
104  where
105  Self: Serialize,
106  {
107  Ok(serde_json::ser::to_string_pretty(&self)?)
108  }
109 
110  fn from_json_reader<R>(&self, mut rdr: R) -> Result<Self>
111  where
112  R: io::Read,
113  Self: DeserializeOwned,
114  {
115  let mut bytes = Vec::new();
116  rdr.read_to_end(&mut bytes)?;
117  Ok(serde_json::de::from_slice(&bytes)?)
118  }
119 
120  fn from_json_str<'a>(s: &'a str) -> Result<Self>
121  where
122  Self: Deserialize<'a>,
123  {
124  Ok(serde_json::de::from_slice(s.as_bytes())?)
125  }
126 }
127 
128 impl<T> Objective for Vec<T> {}
129 impl<K, V> Objective for HashMap<K, V> {}
130 impl<K, V> Objective for BTreeMap<K, V> {}
131 impl Objective for std::path::PathBuf {}
132 impl Objective for std::path::Path {}
133 impl Objective for std::string::String {}
134 impl Objective for std::any::TypeId {}
135 impl Objective for u8 {}
136 impl Objective for u16 {}
137 impl Objective for u32 {}
138 impl Objective for u64 {}
139 impl Objective for u128 {}
140 impl Objective for i8 {}
141 impl Objective for i16 {}
142 impl Objective for i32 {}
143 impl Objective for i64 {}
144 impl Objective for i128 {}
145 impl Objective for isize {}
146 impl Objective for usize {}
147 impl Objective for f32 {}
148 impl Objective for f64 {}
149