Fixes for breaking changes in the toml crate
This commit is contained in:
parent
765ab75ea8
commit
2e8f8a4885
|
@ -1,3 +1,4 @@
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
use std::{error, fmt, io::Error as IoError, num::TryFromIntError};
|
use std::{error, fmt, io::Error as IoError, num::TryFromIntError};
|
||||||
|
|
||||||
#[cfg(feature = "env")]
|
#[cfg(feature = "env")]
|
||||||
|
@ -98,6 +99,9 @@ pub enum Error {
|
||||||
/// Error from mastodon-async-entities
|
/// Error from mastodon-async-entities
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Entities(#[from] mastodon_async_entities::error::Error),
|
Entities(#[from] mastodon_async_entities::error::Error),
|
||||||
|
/// Error parsing UTF-8 string from bytes
|
||||||
|
#[error(transparent)]
|
||||||
|
FromUtf8(#[from] FromUtf8Error),
|
||||||
/// Other errors
|
/// Other errors
|
||||||
#[error("other error: {0:?}")]
|
#[error("other error: {0:?}")]
|
||||||
Other(String),
|
Other(String),
|
||||||
|
@ -179,14 +183,6 @@ mod tests {
|
||||||
assert_is!(err, Error::Url(..));
|
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")]
|
#[cfg(feature = "toml")]
|
||||||
#[test]
|
#[test]
|
||||||
fn from_toml_de_error() {
|
fn from_toml_de_error() {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
io::{BufWriter, Read, Write},
|
io::{Read, Write},
|
||||||
path::Path,
|
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
|
/// Attempts to deserialize a Data struct from a slice of bytes
|
||||||
pub fn from_slice(s: &[u8]) -> Result<Data> {
|
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
|
/// 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
|
/// Attempts to serialize a Data struct to a Vec of bytes
|
||||||
pub fn to_vec(data: &Data) -> Result<Vec<u8>> {
|
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
|
/// Attempts to serialize a Data struct to something that implements the
|
||||||
/// std::io::Write trait
|
/// std::io::Write trait
|
||||||
pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> {
|
pub fn to_writer<W: Write>(data: &Data, mut writer: W) -> Result<()> {
|
||||||
let mut buf_writer = BufWriter::new(writer);
|
writer.write_all(tomlcrate::to_string(data)?.as_bytes())?;
|
||||||
let vec = to_vec(data)?;
|
Ok(())
|
||||||
if vec.len() != buf_writer.write(&vec)? {
|
|
||||||
Err(crate::Error::NotAllBytesWritten)
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to serialize a Data struct to a file
|
/// Attempts to serialize a Data struct to a file
|
||||||
|
|
Loading…
Reference in New Issue