encoding_rs_transcode

Crates.ioencoding_rs_transcode
lib.rsencoding_rs_transcode
version0.8.3
sourcesrc
created_at2023-04-05 19:37:40.932584
updated_at2023-04-17 15:19:53.468505
descriptionTranscode text within writers using encoding_rs
homepage
repositoryhttps://github.com/VincentFoulon80/encoding_rs_transcode
max_upload_size
id831351
size10,507
Vincent Foulon (VincentFoulon80)

documentation

README

encode_rs_transcode

Crates.io docs.rs dependency status Crates.io Rust

This library allows you to easily transcode text within writers.

The transcoding is performed by encoding_rs, this library just provides a simple builder to ease the use with external writers.

Use cases

  • Writing files in a particular format (e.g. writing a CSV file in WINDOWS_1252, using the csv crate)

Example

extern crate csv;
use std::fs::File;
use encoding_rs_transcode::{encoding_rs::WINDOWS_1252, TranscoderBuilder};

fn main() {
    // Create a file writer
    let file = File::create("test.csv").unwrap();

    // Create a transcoder that'll transcode the input to WINDOWS_1252
    let transcoder_writer = TranscoderBuilder::new()
    //  .from_encoding(UTF_8) // implied by new()
        .to_encoding(WINDOWS_1252)
        .build_writer(file);

    // Create a CSV writer
    let mut csv_writer = csv::Writer::from_writer(transcoder_writer);
    // Write to the CSV file
    csv_writer.write_record(["foo", "bar"]).unwrap();
    csv_writer.write_record(["aceio", "àcéîö"]).unwrap();

    // The CSV file will now be encoded in WINDOWS_1252, without the CSV crate ever
    // aknowledging the final encoding.
    // This can be applied to any writer implementing the `Write` trait.
}
Commit count: 10

cargo fmt