Skip to content

Commit

Permalink
make strip() no longer return errors (#9)
Browse files Browse the repository at this point in the history
Writing to a `Cursor<Vec<u8>>` can never fail.
  • Loading branch information
sunshowers committed Dec 7, 2021
1 parent f42c49e commit 5e24faf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::io::{self, Write};

fn work() -> io::Result<()> {
let bytes_with_colors = b"\x1b[32mfoo\x1b[m bar";
let plain_bytes = strip_ansi_escapes::strip(&bytes_with_colors)?;
let plain_bytes = strip_ansi_escapes::strip(&bytes_with_colors);
io::stdout().write_all(&plain_bytes)?;
Ok(())
}
Expand Down
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//!
//! # fn foo() -> io::Result<()> {
//! let bytes_with_colors = b"\x1b[32mfoo\x1b[m bar";
//! let plain_bytes = strip_ansi_escapes::strip(&bytes_with_colors)?;
//! let plain_bytes = strip_ansi_escapes::strip(&bytes_with_colors);
//! io::stdout().write_all(&plain_bytes)?;
//! # Ok(())
//! # }
Expand Down Expand Up @@ -59,14 +59,18 @@ where
/// See [the module documentation][mod] for an example.
///
/// [mod]: index.html
pub fn strip<T>(data: T) -> io::Result<Vec<u8>>
pub fn strip<T>(data: T) -> Vec<u8>
where
T: AsRef<[u8]>,
{
let c = Cursor::new(Vec::new());
let mut writer = Writer::new(c);
writer.write_all(data.as_ref())?;
Ok(writer.into_inner()?.into_inner())
fn strip_impl(data: &[u8]) -> io::Result<Vec<u8>> {
let c = Cursor::new(Vec::new());
let mut writer = Writer::new(c);
writer.write_all(data.as_ref())?;
Ok(writer.into_inner()?.into_inner())
}

strip_impl(data.as_ref()).expect("writing to a Cursor<Vec<u8>> cannot fail")
}

struct Performer<W>
Expand Down Expand Up @@ -162,7 +166,7 @@ mod tests {
use super::*;

fn assert_parsed(input: &[u8], expected: &[u8]) {
let bytes = strip(input).expect("Failed to strip escapes");
let bytes = strip(input);
assert_eq!(bytes, expected);
}

Expand Down

0 comments on commit 5e24faf

Please sign in to comment.