Function decancer::cure_reader

source ·
pub fn cure_reader<R>(reader: R) -> Result<CuredString>where
    R: Read,
Available on crate feature std only.
Expand description

Cures bytes from a reader. This can be a File, BufReader, Cursor, or any data type that implements Read.

Safety

This function assumes that the stream of bytes coming are already valid UTF-8. Therefore, UTF-8 validity will NOT be checked unless the reader EOFs prematurely (see UnexpectedEof).

Errors

Errors only if the reader ends prematurely or fails.

Examples

From an in-memory buffer with a Cursor:

use std::io::Cursor;

let text = "vEⓡ𝔂 𝔽𝕌Ňℕy ţ乇𝕏𝓣";
let reader = Cursor::new(text.as_bytes());
let cured = decancer::cure_reader(reader).unwrap();

// cured here is a decancer::CuredString struct wrapping over the cured string
// for comparison purposes, it's more recommended to use the methods provided by the decancer::CuredString struct.
assert_eq!(cured, "very funny text");
assert!(cured.starts_with("very"));
assert!(cured.contains("funny"));
assert!(cured.ends_with("text"));

// retrieve the String inside and consume the struct.
let _output_str = cured.into_str();

From a File through a BufReader:

use std::{fs::File, io::BufReader};

// assume cancer.txt is a UTF-8 encoded file containing the string "vEⓡ𝔂 𝔽𝕌Ňℕy ţ乇𝕏𝓣"
let reader = BufReader::new(File::open("cancer.txt").unwrap());
let cured = decancer::cure_reader(reader).unwrap();

// cured here is a decancer::CuredString struct wrapping over the cured string
// for comparison purposes, it's more recommended to use the methods provided by the decancer::CuredString struct.
assert_eq!(cured, "very funny text");
assert!(cured.starts_with("very"));
assert!(cured.contains("funny"));
assert!(cured.ends_with("text"));

// retrieve the String inside and consume the struct.
let _output_str = cured.into_str();