Convinience functions in crate root
This commit is contained in:
@@ -5,6 +5,8 @@ use thiserror::Error;
|
|||||||
pub enum CoronaError {
|
pub enum CoronaError {
|
||||||
#[error("IO error while writing code: {0}")]
|
#[error("IO error while writing code: {0}")]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[from] std::io::Error),
|
||||||
|
#[error("Serialization process yielded invalid UTF-8 sequence: {0}")]
|
||||||
|
Utf8(#[from] std::string::FromUtf8Error),
|
||||||
#[error("Unknown error: {0}")]
|
#[error("Unknown error: {0}")]
|
||||||
Custom(String),
|
Custom(String),
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-1
@@ -1,2 +1,19 @@
|
|||||||
|
use crate::ser::SerResult;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
pub mod ser;
|
pub mod ser;
|
||||||
pub mod error;
|
|
||||||
|
pub fn write(value: impl Serialize, target: impl std::io::Write) -> SerResult {
|
||||||
|
value.serialize(&mut ser::Corona::new(target))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_file(value: impl Serialize, target: impl AsRef<std::path::Path>) -> SerResult {
|
||||||
|
value.serialize(&mut ser::Corona::new(std::fs::File::create(target)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_string(value: impl Serialize) -> Result<String, error::CoronaError> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
value.serialize(&mut ser::Corona::new(&mut out))?;
|
||||||
|
Ok(String::from_utf8(out)?)
|
||||||
|
}
|
||||||
+8
-1
@@ -2,7 +2,7 @@ use crate::error::CoronaError;
|
|||||||
use serde::ser;
|
use serde::ser;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
type SerResult = Result<(), CoronaError>;
|
pub(crate) type SerResult = Result<(), CoronaError>;
|
||||||
|
|
||||||
pub struct Corona<W: Write> {
|
pub struct Corona<W: Write> {
|
||||||
writer: W,
|
writer: W,
|
||||||
@@ -10,6 +10,13 @@ pub struct Corona<W: Write> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Write> Corona<W> {
|
impl<W: Write> Corona<W> {
|
||||||
|
pub(crate) fn new(target: W) -> Self {
|
||||||
|
Self {
|
||||||
|
writer: target,
|
||||||
|
inside: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn start_sub(&mut self) -> &mut Self {
|
fn start_sub(&mut self) -> &mut Self {
|
||||||
self.inside = true;
|
self.inside = true;
|
||||||
self
|
self
|
||||||
|
|||||||
Reference in New Issue
Block a user