summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorD. Scott Boggs <scott@tams.tech>2023-02-01 07:05:18 -0500
committerScott Boggs <dscottboggs@gmail.com>2023-02-01 08:52:50 -0500
commit2e8f8a48854e8ae5bc4eeeed0ef2667d475305b6 (patch)
tree25794fe29487dbc8f32e85f04044f2f2c0a3b5f1
parent765ab75ea8ee0b6272aa168e55991017cc663a01 (diff)
Fixes for breaking changes in the toml crate
-rw-r--r--src/errors.rs12
-rw-r--r--src/helpers/toml.rs17
2 files changed, 10 insertions, 19 deletions
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),
@@ -181,14 +185,6 @@ mod tests {
#[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() {
use tomlcrate;
let err: TomlDeError = tomlcrate::from_str::<()>("not valid toml").unwrap_err();
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<Data> {
/// Attempts to deserialize a Data struct from a slice of bytes
pub fn from_slice(s: &[u8]) -> Result<Data> {
- 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<String> {
/// Attempts to serialize a Data struct to a Vec of bytes
pub fn to_vec(data: &Data) -> Result<Vec<u8>> {
- 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<W: Write>(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<W: Write>(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