changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/obj/src/lib.rs

changeset 698: 96958d3eb5b0
parent: 4f49127c9048
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 //! lib.rs --- Objective types
2 pub use ron;
3 
4 mod err;
5 mod id;
6 
7 mod config;
8 mod object;
9 
10 pub use err::{Error, Result};
11 pub use id::{Domain, Id, Identity, NameSpace};
12 
13 pub use object::{
14  color::Color,
15  direction::{CardinalDirection, EdgeDirection, RelativeDirection},
16  doc::{Doc, DocExtension},
17  location::{City, Point},
18  media::{Media, MediaExtension},
19  meta::{Meta, Note, Property, Summary},
20  temperature::Temperature,
21 };
22 
23 #[cfg(feature = "oauth")]
24 pub use config::auth::Oauth2Config;
25 #[cfg(feature = "git")]
26 pub use config::repo::git::GitRepository;
27 #[cfg(feature = "hg")]
28 pub use config::repo::hg::{
29  export_hg_git, HgSubFile, HgwebConfig, MercurialConfig,
30 };
31 
32 pub use config::{
33  auth::{AuthConfig, SshConfig},
34  database::DatabaseConfig,
35  display::DisplayConfig,
36  library::LibraryConfig,
37  meta::MetaConfig,
38  network::NetworkConfig,
39  package::PackageConfig,
40  program::ProgramConfig,
41  project::ProjectConfig,
42  registry::RegistryConfig,
43  repo::RepoConfig,
44  user::{
45  ShellConfig, TmuxPaneConfig, TmuxSessionConfig, TmuxWindowConfig,
46  UserConfig,
47  },
48 };
49 
50 use ron::extensions::Extensions;
51 use serde::{de::DeserializeOwned, Deserialize, Serialize};
52 
53 use std::io;
54 
55 #[macro_export]
56 macro_rules! impl_config {
57  ($($t:ident),*) => {
58  $(
59  impl Objective for $t {}
60  impl Configure for $t {}
61  )*
62  };
63 }
64 
65 impl_config!(
66  MetaConfig,
67  RepoConfig,
68  DatabaseConfig,
69  DisplayConfig,
70  UserConfig,
71  ShellConfig,
72  TmuxSessionConfig,
73  TmuxWindowConfig,
74  TmuxPaneConfig,
75  LibraryConfig,
76  ProgramConfig,
77  ProjectConfig,
78  NetworkConfig,
79  AuthConfig,
80  SshConfig
81 );
82 
83 #[cfg(feature = "oauth")]
84 impl_config!(Oauth2Config);
85 
86 /// common trait for all config modules. This trait provides functions
87 /// for de/serializing to/from RON, updating fields, and formatting.
88 pub trait Configure: Objective {
89  fn update(&self) -> Result<()> {
90  Ok(())
91  }
92 }
93 
94 /// Objective trait
95 ///
96 /// Defines Object behaviors, implemented by Objects
97 pub trait Objective {
98  fn encode(&self) -> Result<Vec<u8>>
99  where
100  Self: Serialize,
101  {
102  Ok(bincode::serialize(self)?)
103  }
104 
105  fn encode_into<W>(&self, writer: W) -> Result<()>
106  where
107  W: io::Write,
108  Self: Serialize,
109  {
110  Ok(bincode::serialize_into(writer, self)?)
111  }
112 
113  fn decode<'a>(&self, bytes: &'a [u8]) -> Result<Self>
114  where
115  Self: Deserialize<'a>,
116  {
117  Ok(bincode::deserialize(bytes)?)
118  }
119 
120  fn decode_from<R>(&self, rdr: R) -> Result<Self>
121  where
122  R: io::Read,
123  Self: DeserializeOwned,
124  {
125  Ok(bincode::deserialize_from(rdr)?)
126  }
127 
128  fn to_ron_writer<W>(&self, writer: W) -> Result<()>
129  where
130  W: io::Write,
131  Self: Serialize,
132  {
133  Ok(ron::ser::to_writer_pretty(
134  writer,
135  &self,
136  ron::ser::PrettyConfig::new()
137  .indentor(" ".to_owned())
138  .extensions(Extensions::all()),
139  )?)
140  }
141 
142  fn to_ron_string(&self) -> Result<String>
143  where
144  Self: Serialize,
145  {
146  Ok(ron::ser::to_string_pretty(
147  &self,
148  ron::ser::PrettyConfig::new().indentor(" ".to_owned()),
149  )?)
150  }
151 
152  fn from_ron_reader<R>(&self, mut rdr: R) -> Result<Self>
153  where
154  R: io::Read,
155  Self: DeserializeOwned,
156  {
157  let mut bytes = Vec::new();
158  rdr.read_to_end(&mut bytes)?;
159  Ok(ron::de::from_bytes(&bytes)?)
160  }
161 
162  fn from_ron_str<'a>(s: &'a str) -> Result<Self>
163  where
164  Self: Deserialize<'a>,
165  {
166  Ok(ron::de::from_bytes(s.as_bytes())?)
167  }
168 
169  fn to_json_writer<W>(&self, writer: W) -> Result<()>
170  where
171  W: io::Write,
172  Self: Serialize,
173  {
174  // let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
175  Ok(serde_json::ser::to_writer_pretty(writer, &self)?)
176  }
177 
178  fn to_json_string(&self) -> Result<String>
179  where
180  Self: Serialize,
181  {
182  Ok(serde_json::ser::to_string_pretty(&self)?)
183  }
184 
185  fn from_json_reader<R>(&self, mut rdr: R) -> Result<Self>
186  where
187  R: io::Read,
188  Self: DeserializeOwned,
189  {
190  let mut bytes = Vec::new();
191  rdr.read_to_end(&mut bytes)?;
192  Ok(serde_json::de::from_slice(&bytes)?)
193  }
194 
195  fn from_json_str<'a>(s: &'a str) -> Result<Self>
196  where
197  Self: Deserialize<'a>,
198  {
199  Ok(serde_json::de::from_slice(s.as_bytes())?)
200  }
201 }