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 10 pub use err::{Error, Result}; 11 pub use id::{Domain, Id, Identity, NameSpace}; 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, 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, 33 auth::{AuthConfig, SshConfig}, 34 database::DatabaseConfig, 35 display::DisplayConfig, 36 library::LibraryConfig, 38 network::NetworkConfig, 39 package::PackageConfig, 40 program::ProgramConfig, 41 project::ProjectConfig, 42 registry::RegistryConfig, 45 ShellConfig, TmuxPaneConfig, TmuxSessionConfig, TmuxWindowConfig, 50 use ron::extensions::Extensions; 51 use serde::{de::DeserializeOwned, Deserialize, Serialize}; 56 macro_rules! impl_config { 59 impl Objective for $t {} 60 impl Configure for $t {} 83 #[cfg(feature = "oauth")] 84 impl_config!(Oauth2Config); 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<()> { 96 /// Defines Object behaviors, implemented by Objects 98 fn encode(&self) -> Result<Vec<u8>> 102 Ok(bincode::serialize(self)?) 105 fn encode_into<W>(&self, writer: W) -> Result<()> 110 Ok(bincode::serialize_into(writer, self)?) 113 fn decode<'a>(&self, bytes: &'a [u8]) -> Result<Self> 115 Self: Deserialize<'a>, 117 Ok(bincode::deserialize(bytes)?) 120 fn decode_from<R>(&self, rdr: R) -> Result<Self> 123 Self: DeserializeOwned, 125 Ok(bincode::deserialize_from(rdr)?) 128 fn to_ron_writer<W>(&self, writer: W) -> Result<()> 133 Ok(ron::ser::to_writer_pretty( 136 ron::ser::PrettyConfig::new() 137 .indentor(" ".to_owned()) 138 .extensions(Extensions::all()), 142 fn to_ron_string(&self) -> Result<String> 146 Ok(ron::ser::to_string_pretty( 148 ron::ser::PrettyConfig::new().indentor(" ".to_owned()), 152 fn from_ron_reader<R>(&self, mut rdr: R) -> Result<Self> 155 Self: DeserializeOwned, 157 let mut bytes = Vec::new(); 158 rdr.read_to_end(&mut bytes)?; 159 Ok(ron::de::from_bytes(&bytes)?) 162 fn from_ron_str<'a>(s: &'a str) -> Result<Self> 164 Self: Deserialize<'a>, 166 Ok(ron::de::from_bytes(s.as_bytes())?) 169 fn to_json_writer<W>(&self, writer: W) -> Result<()> 174 // let formatter = serde_json::ser::PrettyFormatter::with_indent(b" "); 175 Ok(serde_json::ser::to_writer_pretty(writer, &self)?) 178 fn to_json_string(&self) -> Result<String> 182 Ok(serde_json::ser::to_string_pretty(&self)?) 185 fn from_json_reader<R>(&self, mut rdr: R) -> Result<Self> 188 Self: DeserializeOwned, 190 let mut bytes = Vec::new(); 191 rdr.read_to_end(&mut bytes)?; 192 Ok(serde_json::de::from_slice(&bytes)?) 195 fn from_json_str<'a>(s: &'a str) -> Result<Self> 197 Self: Deserialize<'a>, 199 Ok(serde_json::de::from_slice(s.as_bytes())?)