From 45f4cbbbfe4777613359972e73c30235a0b37f2c Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Sat, 25 Aug 2018 08:07:12 -0400 Subject: [PATCH] Move the `toml` helpers to `elefren::helpers::toml` --- README.md | 2 +- examples/register.rs | 2 +- src/data.rs | 90 -------------------------------------------- src/helpers/mod.rs | 11 ++++++ src/helpers/toml.rs | 78 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 +- 6 files changed, 94 insertions(+), 93 deletions(-) create mode 100644 src/helpers/mod.rs create mode 100644 src/helpers/toml.rs diff --git a/README.md b/README.md index 59f061d..c16743d 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ use std::error::Error; use elefren::prelude::*; use elefren::apps::prelude::*; -use elefren::data::toml; // requires `features = ["toml"]` +use elefren::helpers::toml; // requires `features = ["toml"]` fn main() -> Result<(), Box> { let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") { diff --git a/examples/register.rs b/examples/register.rs index 1a1524f..c6455df 100644 --- a/examples/register.rs +++ b/examples/register.rs @@ -7,7 +7,7 @@ pub use self::elefren::{apps::prelude::*, prelude::*}; use std::{error::Error, io}; #[cfg(feature = "toml")] -use self::elefren::data::toml; +use self::elefren::helpers::toml; #[allow(dead_code)] #[cfg(feature = "toml")] diff --git a/src/data.rs b/src/data.rs index 7bee3fe..20dfd69 100644 --- a/src/data.rs +++ b/src/data.rs @@ -15,93 +15,3 @@ pub struct Data { /// The client's access token. pub token: Cow<'static, str>, } - -#[cfg(feature = "toml")] -/// Helpers for serializing to/deserializing from toml -/// -/// In order to use this module, set the "toml" feature in your Cargo.toml: -/// -/// ```toml,ignore -/// [dependencies.elefren] -/// version = "0.12" -/// features = ["toml"] -/// ``` -pub mod toml { - use super::Data; - use std::{ - fs::{File, OpenOptions}, - io::{BufWriter, Read, Write}, - path::Path, - }; - use Result; - - use tomlcrate; - - /// Attempts to deserialize a Data struct from a string - pub fn from_str(s: &str) -> Result { - Ok(tomlcrate::from_str(s)?) - } - - /// Attempts to deserialize a Data struct from a slice of bytes - pub fn from_slice(s: &[u8]) -> Result { - Ok(tomlcrate::from_slice(s)?) - } - - /// Attempts to deserialize a Data struct from something that implements - /// the std::io::Read trait - pub fn from_reader(mut r: R) -> Result { - let mut buffer = Vec::new(); - r.read_to_end(&mut buffer)?; - from_slice(&buffer) - } - - /// Attempts to deserialize a Data struct from a file - pub fn from_file>(path: P) -> Result { - let path = path.as_ref(); - let file = File::open(path)?; - Ok(from_reader(file)?) - } - - /// Attempts to serialize a Data struct to a String - pub fn to_string(data: &Data) -> Result { - Ok(tomlcrate::to_string_pretty(data)?) - } - - /// Attempts to serialize a Data struct to a Vec of bytes - pub fn to_vec(data: &Data) -> Result> { - Ok(tomlcrate::to_vec(data)?) - } - - /// Attempts to serialize a Data struct to something that implements the - /// std::io::Write trait - pub fn to_writer(data: &Data, writer: W) -> Result<()> { - let mut buf_writer = BufWriter::new(writer); - let vec = to_vec(data)?; - buf_writer.write(&vec)?; - Ok(()) - } - - /// Attempts to serialize a Data struct to a file - /// - /// When opening the file, this will set the `.write(true)` and - /// `.truncate(true)` options, use the next method for more - /// fine-grained control - pub fn to_file>(data: &Data, path: P) -> Result<()> { - let mut options = OpenOptions::new(); - options.write(true).truncate(true); - to_file_with_options(data, path, options)?; - Ok(()) - } - - /// Attempts to serialize a Data struct to a file - pub fn to_file_with_options>( - data: &Data, - path: P, - options: OpenOptions, - ) -> Result<()> { - let path = path.as_ref(); - let file = options.open(path)?; - to_writer(data, file)?; - Ok(()) - } -} diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs new file mode 100644 index 0000000..ecefb36 --- /dev/null +++ b/src/helpers/mod.rs @@ -0,0 +1,11 @@ +#[cfg(feature = "toml")] +/// Helpers for serializing to/deserializing from toml +/// +/// In order to use this module, set the "toml" feature in your Cargo.toml: +/// +/// ```toml,ignore +/// [dependencies.elefren] +/// version = "0.12" +/// features = ["toml"] +/// ``` +pub mod toml; diff --git a/src/helpers/toml.rs b/src/helpers/toml.rs new file mode 100644 index 0000000..67361f8 --- /dev/null +++ b/src/helpers/toml.rs @@ -0,0 +1,78 @@ +use std::{ + fs::{File, OpenOptions}, + io::{BufWriter, Read, Write}, + path::Path, +}; + +use tomlcrate; + +use data::Data; +use Result; + +/// Attempts to deserialize a Data struct from a string +pub fn from_str(s: &str) -> Result { + Ok(tomlcrate::from_str(s)?) +} + +/// Attempts to deserialize a Data struct from a slice of bytes +pub fn from_slice(s: &[u8]) -> Result { + Ok(tomlcrate::from_slice(s)?) +} + +/// Attempts to deserialize a Data struct from something that implements +/// the std::io::Read trait +pub fn from_reader(mut r: R) -> Result { + let mut buffer = Vec::new(); + r.read_to_end(&mut buffer)?; + from_slice(&buffer) +} + +/// Attempts to deserialize a Data struct from a file +pub fn from_file>(path: P) -> Result { + let path = path.as_ref(); + let file = File::open(path)?; + Ok(from_reader(file)?) +} + +/// Attempts to serialize a Data struct to a String +pub fn to_string(data: &Data) -> Result { + Ok(tomlcrate::to_string_pretty(data)?) +} + +/// Attempts to serialize a Data struct to a Vec of bytes +pub fn to_vec(data: &Data) -> Result> { + Ok(tomlcrate::to_vec(data)?) +} + +/// Attempts to serialize a Data struct to something that implements the +/// std::io::Write trait +pub fn to_writer(data: &Data, writer: W) -> Result<()> { + let mut buf_writer = BufWriter::new(writer); + let vec = to_vec(data)?; + buf_writer.write(&vec)?; + Ok(()) +} + +/// Attempts to serialize a Data struct to a file +/// +/// When opening the file, this will set the `.write(true)` and +/// `.truncate(true)` options, use the next method for more +/// fine-grained control +pub fn to_file>(data: &Data, path: P) -> Result<()> { + let mut options = OpenOptions::new(); + options.write(true).truncate(true); + to_file_with_options(data, path, options)?; + Ok(()) +} + +/// Attempts to serialize a Data struct to a file +pub fn to_file_with_options>( + data: &Data, + path: P, + options: OpenOptions, +) -> Result<()> { + let path = path.as_ref(); + let file = options.open(path)?; + to_writer(data, file)?; + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index 4c5ce07..78b799e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,12 +64,14 @@ pub use status_builder::StatusBuilder; /// Registering your App pub mod apps; -/// Working with client auth data +/// Contains the struct that holds the client auth data pub mod data; /// Entities returned from the API pub mod entities; /// Errors pub mod errors; +/// Collection of helpers for serializing/deserializing `Data` objects +pub mod helpers; /// Contains trait for converting `reqwest::Request`s to `reqwest::Response`s pub mod http_send; /// Handling multiple pages of entities.