Skip to content

Commit

Permalink
format source code with cargo-fmt
Browse files Browse the repository at this point in the history
rustup component add rustfmt-preview
cargo fmt
  • Loading branch information
Jens Getreu committed Nov 8, 2018
1 parent dff02fc commit 781828b
Show file tree
Hide file tree
Showing 8 changed files with 1,742 additions and 1,245 deletions.
96 changes: 66 additions & 30 deletions src/codec/ascii.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Custom 7-bit ASCII graphic only encoding.

use std::mem;
use encoding::types::{ByteWriter, CodecError, Encoding, RawDecoder, RawEncoder, StringWriter};
use std::convert::Into;
use encoding::types::{Encoding, CodecError, StringWriter, RawDecoder, RawEncoder, ByteWriter};

use std::mem;

/// A static castable reference to `AsciiGraphicEncoding`.
/// Usage: `let enc = ASCII_GRAPHIC as encoding::EncodingRef`.
Expand All @@ -16,38 +15,61 @@ pub const ASCII_GRAPHIC: &'static self::AsciiGraphicEncoding = &self::AsciiGraph
pub struct AsciiGraphicEncoding;

impl Encoding for AsciiGraphicEncoding {
fn name(&self) -> &'static str { "ascii" }
fn whatwg_name(&self) -> Option<&'static str> { None }
fn raw_encoder(&self) -> Box<dyn RawEncoder> { AsciiGraphicEncoder::new() }
fn raw_decoder(&self) -> Box<dyn RawDecoder> { AsciiGraphicDecoder::new() }
fn name(&self) -> &'static str {
"ascii"
}
fn whatwg_name(&self) -> Option<&'static str> {
None
}
fn raw_encoder(&self) -> Box<dyn RawEncoder> {
AsciiGraphicEncoder::new()
}
fn raw_decoder(&self) -> Box<dyn RawDecoder> {
AsciiGraphicDecoder::new()
}
}


/// An encoder for ASCII.
#[derive(Clone, Copy)]
pub struct AsciiGraphicEncoder;


impl AsciiGraphicEncoder {
pub fn new() -> Box<dyn RawEncoder> { Box::new(AsciiGraphicEncoder) }
pub fn new() -> Box<dyn RawEncoder> {
Box::new(AsciiGraphicEncoder)
}
}


impl RawEncoder for AsciiGraphicEncoder {
fn from_self(&self) -> Box<dyn RawEncoder> { AsciiGraphicEncoder::new() }
fn is_ascii_compatible(&self) -> bool { true }
fn from_self(&self) -> Box<dyn RawEncoder> {
AsciiGraphicEncoder::new()
}
fn is_ascii_compatible(&self) -> bool {
true
}

fn raw_feed(&mut self, input: &str, output: &mut dyn ByteWriter) -> (usize, Option<CodecError>) {
fn raw_feed(
&mut self,
input: &str,
output: &mut dyn ByteWriter,
) -> (usize, Option<CodecError>) {
output.writer_hint(input.len());

// all non graphic is unrepresentable
match input.as_bytes().iter().position(|&ch| ch >= 0x7F || (ch < 0x20) && (ch != 0x09) ) {
match input
.as_bytes()
.iter()
.position(|&ch| ch >= 0x7F || (ch < 0x20) && (ch != 0x09))
{
Some(first_error) => {
output.write_bytes(&input.as_bytes()[..first_error]);
let len = input[first_error..].chars().next().unwrap().len_utf8();
(first_error, Some(CodecError {
upto: (first_error + len) as isize, cause: "non-graphic character".into()
}))
(
first_error,
Some(CodecError {
upto: (first_error + len) as isize,
cause: "non-graphic character".into(),
}),
)
}
None => {
output.write_bytes(input.as_bytes());
Expand All @@ -61,35 +83,49 @@ impl RawEncoder for AsciiGraphicEncoder {
}
}




/// A decoder for ASCII.
#[derive(Clone, Copy)]
pub struct AsciiGraphicDecoder;

impl AsciiGraphicDecoder {
pub fn new() -> Box<dyn RawDecoder> { Box::new(AsciiGraphicDecoder) }
pub fn new() -> Box<dyn RawDecoder> {
Box::new(AsciiGraphicDecoder)
}
}

impl RawDecoder for AsciiGraphicDecoder {
fn from_self(&self) -> Box<dyn RawDecoder> { AsciiGraphicDecoder::new() }
fn is_ascii_compatible(&self) -> bool { true }
fn from_self(&self) -> Box<dyn RawDecoder> {
AsciiGraphicDecoder::new()
}
fn is_ascii_compatible(&self) -> bool {
true
}

fn raw_feed(&mut self, input: &[u8], output: &mut dyn StringWriter) -> (usize, Option<CodecError>) {
fn raw_feed(
&mut self,
input: &[u8],
output: &mut dyn StringWriter,
) -> (usize, Option<CodecError>) {
output.writer_hint(input.len());

fn write_ascii_bytes(output: &mut dyn StringWriter, buf: &[u8]) {
output.write_str(unsafe {mem::transmute(buf)});
output.write_str(unsafe { mem::transmute(buf) });
}

// all non graphic is error
match input.iter().position(|&ch| ch >= 0x7F || (ch < 0x20) && (ch != 0x09) ) {
match input
.iter()
.position(|&ch| ch >= 0x7F || (ch < 0x20) && (ch != 0x09))
{
Some(first_error) => {
write_ascii_bytes(output, &input[..first_error]);
(first_error, Some(CodecError {
upto: first_error as isize + 1, cause: "non graphic character".into()
}))
(
first_error,
Some(CodecError {
upto: first_error as isize + 1,
cause: "non graphic character".into(),
}),
)
}
None => {
write_ascii_bytes(output, input);
Expand Down
Loading

0 comments on commit 781828b

Please sign in to comment.