Struct Writer

Source
pub struct Writer<W: Write> {
    pub wtr: BufWriter<W>,
    /* private fields */
}
Expand description

A writer for a refer file.

It’s a simple wrapper of a io::BufWriter, along with a line number tracker to help track errors when parsing the input.

Fields§

§wtr: BufWriter<W>

Implementations§

Source§

impl Writer<File>

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Writer<File>, Error>

Build a refer writer that writes data to the given file path. The file is truncated if it already exists.

If there was a problem opening the file at the given path, then this returns the corresponding error.

§Example
use std::error::Error;
use refer::Writer;

fn example() -> Result<(), Box<dyn Error>> {
    let mut wtr = Writer::from_path("foo.refer")?;
    wtr.write_record(vec!["%A Brown, M", "%J PNAS"])?;
    wtr.write_record(vec!["%A Twyford, A. D.", "%J PNAS"])?;
    wtr.flush()?;
    Ok(())
}
Source§

impl<W: Write> Writer<W>

Source

pub fn new(wtr: W) -> Writer<W>

Create a new refer writer.

§Example
use std::error::Error;
use refer::Writer;

fn example() -> Result<(), Box<dyn Error>> {
    let mut wtr = Writer::new(vec![]);
    wtr.write_record(vec!["%A Brown, M", "%J PNAS"])?;
    wtr.write_record(vec!["%A Twyford, A. D.", "%J PNAS"])?;

    let data = String::from_utf8(wtr.into_inner()?)?;
    assert_eq!(data, "%A Brown, M\n%J PNAS\n\n%A Twyford, A. D.\n%J PNAS\n\n");
    Ok(())
}
Source

pub fn write_record<I, T>(&mut self, record: I) -> Result<(), Error>
where I: IntoIterator<Item = T>, T: AsRef<[u8]> + Into<Vec<u8>>,

The main function used to write a record to an output buffer.

Source

pub fn into_inner(self) -> StdResult<W, IntoInnerError<BufWriter<W>>>

Flush the contents of the current buffer and return the underlying writer.

Source

pub fn flush(&mut self) -> StdResult<(), Error>

Flush the writer at the end of usage.

Auto Trait Implementations§

§

impl<W> Freeze for Writer<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Writer<W>
where W: RefUnwindSafe,

§

impl<W> Send for Writer<W>
where W: Send,

§

impl<W> Sync for Writer<W>
where W: Sync,

§

impl<W> Unpin for Writer<W>
where W: Unpin,

§

impl<W> UnwindSafe for Writer<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.