Fixes for breaking changes in the toml crate

This commit is contained in:
D. Scott Boggs 2023-02-01 07:05:18 -05:00 committed by Scott Boggs
parent 765ab75ea8
commit 2e8f8a4885
2 changed files with 10 additions and 19 deletions

View File

@ -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() {

View File

@ -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