Writer

Struct Writer 

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

Writes an iCalendar or vCard file.

Folds all content lines to lines that are no longer than 75 bytes (not including line breaks). A CRLF line break is appended to each content line.

Writer will always output valid UTF-8 to the underlying Write.

§Performance

It can be excessively inefficient to work directly with something that implements Write. For example, every call to write on std::net::TcpStream results in a system call. Wrapping writer in a io::BufWriter may improve performance significantly.

§Example

The following example illustrates how to write a simple vCard file:

use ical_vcard::{Contentline, Identifier, Param, ParamValue, Value, Writer};

let contentlines = [
    Contentline::try_new("BEGIN", "VCARD")?,
    Contentline::try_new("FN", "Mr. John Example")?,
    Contentline::try_new("BDAY", "20230326")?
        .try_add_param("VALUE", ["date-and-or-time"])?,
    Contentline::try_new("END", "VCARD")?,
];

// For the sake of simplicity and testability, the vCard is written to a Vec. However, in a
// real application, one would probably write it to disk or do some further processing (e.g.
// compression)
let vcard = {
    let mut buffer = Vec::new();
    let mut writer = Writer::new(&mut buffer);
    writer.write_all(contentlines)?;
    buffer
};

let expected = "\
BEGIN:VCARD\r
FN:Mr. John Example\r
BDAY;VALUE=date-and-or-time:20230326\r
END:VCARD\r
".as_bytes();

assert_eq!(vcard, expected);

Implementations§

Source§

impl<W: Write> Writer<W>

Source

pub fn new(writer: W) -> Self

Creates a new Writer.

Note that you can also pass &mut W instead of W for any W that implements Write.

Source

pub fn write(&mut self, contentline: &Contentline) -> Result<()>

Writes a single content line.

Flushes the underlying Write afterwards.

§Errors

Fails in case of an io::Error.

Source

pub fn write_all<C, I>(&mut self, contentlines: I) -> Result<()>
where C: Borrow<Contentline>, I: IntoIterator<Item = C>,

Writes a sequence of contentlines.

Flushes the underlying Write afterwards.

§Errors

Fails in case of an io::Error.

Source

pub fn inner(&self) -> &W

Returns a reference to the inner Write.

Source

pub fn inner_mut(&mut self) -> &mut W

Returns a mutable reference to the inner Write.

Source

pub fn into_inner(self) -> W

Returns the inner Write

Trait Implementations§

Source§

impl<W: Debug + Write> Debug for Writer<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.