Skip to main content

fold

Function fold 

Source
pub fn fold(
    name: &str,
    value: &[u8],
    out: &mut impl Sink,
) -> Result<(), WriteError>
Expand description

Writes Name: value with CRLF folds before whitespace so lines stay within 78 columns where the words allow, never creating a line of only whitespace and never one over 998 bytes. A space is added after the colon when value does not start with whitespace. No CRLF is written at the end.

§Errors

WriteError::NotToken for a name outside ftext; WriteError::Forbidden for CR, LF, or NUL in value; WriteError::OutOfRange for a word (with the whitespace that ends the value, after the last word) that cannot fit in 998 bytes; WriteError::Full (nothing written when the sink reports its room).

§Examples

use parse_net::{mail, SliceSink};

let mut buf = [0u8; 256];
let mut out = SliceSink::new(&mut buf);
let words = "word ".repeat(20);
mail::fold("Subject", words.trim_end().as_bytes(), &mut out)?;
let lines: Vec<&[u8]> = out.written().split(|&b| b == b'\n').collect();
assert_eq!(lines.len(), 2);
assert!(lines.iter().all(|l| l.len() <= 79));