diff --git a/src/errors.rs b/src/errors.rs index 563555f..ac4d08d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,3 +1,4 @@ +use std::string::FromUtf8Error; use std::{error, fmt, io::Error as IoError, num::TryFromIntError}; #[cfg(feature = "env")] @@ -98,6 +99,9 @@ pub enum Error { /// Error from mastodon-async-entities #[error(transparent)] Entities(#[from] mastodon_async_entities::error::Error), + /// Error parsing UTF-8 string from bytes + #[error(transparent)] + FromUtf8(#[from] FromUtf8Error), /// Other errors #[error("other error: {0:?}")] Other(String), @@ -179,14 +183,6 @@ mod tests { assert_is!(err, Error::Url(..)); } - #[cfg(feature = "toml")] - #[test] - fn from_toml_ser_error() { - let err: TomlSerError = TomlSerError::DateInvalid; - let err: Error = Error::from(err); - assert_is!(err, Error::TomlSer(..)); - } - #[cfg(feature = "toml")] #[test] fn from_toml_de_error() { diff --git a/src/helpers/toml.rs b/src/helpers/toml.rs index 0e71639..abf2415 100644 --- a/src/helpers/toml.rs +++ b/src/helpers/toml.rs @@ -1,6 +1,6 @@ use std::{ fs::{File, OpenOptions}, - io::{BufWriter, Read, Write}, + io::{Read, Write}, path::Path, }; @@ -15,7 +15,7 @@ pub fn from_str(s: &str) -> Result { /// Attempts to deserialize a Data struct from a slice of bytes pub fn from_slice(s: &[u8]) -> Result { - Ok(tomlcrate::from_slice(s)?) + from_str(&String::from_utf8(s.into())?) } /// Attempts to deserialize a Data struct from something that implements @@ -40,19 +40,14 @@ pub fn to_string(data: &Data) -> Result { /// Attempts to serialize a Data struct to a Vec of bytes pub fn to_vec(data: &Data) -> Result> { - Ok(tomlcrate::to_vec(data)?) + Ok(tomlcrate::to_string(data)?.as_bytes().into()) } /// 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)?; - if vec.len() != buf_writer.write(&vec)? { - Err(crate::Error::NotAllBytesWritten) - } else { - Ok(()) - } +pub fn to_writer(data: &Data, mut writer: W) -> Result<()> { + writer.write_all(tomlcrate::to_string(data)?.as_bytes())?; + Ok(()) } /// Attempts to serialize a Data struct to a file