Skip to content

Commit

Permalink
csplit: return UResult instead of io::Result from iterator to handle …
Browse files Browse the repository at this point in the history
…error message more uniformly.
  • Loading branch information
fuad1502 committed Dec 12, 2024
1 parent f98e6d3 commit 8a54b84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ pub fn csplit<T>(options: &CsplitOptions, patterns: &[String], input: T) -> Resu
where
T: BufRead,
{
let mut input_iter = InputSplitter::new(input.lines().enumerate());
let enumerated_input_lines = input
.lines()
.enumerate()
.map(|(idx, result)| (idx, result.map_err_context(|| "read error".to_string())));
let mut input_iter = InputSplitter::new(enumerated_input_lines);
let mut split_writer = SplitWriter::new(options);
let patterns: Vec<patterns::Pattern> = patterns::get_patterns(patterns)?;
let ret = do_csplit(&mut split_writer, patterns, &mut input_iter);
Expand Down Expand Up @@ -120,7 +124,7 @@ fn do_csplit<I>(
input_iter: &mut InputSplitter<I>,
) -> Result<(), CsplitError>
where
I: Iterator<Item = (usize, io::Result<String>)>,
I: Iterator<Item = (usize, UResult<String>)>,
{
// split the file based on patterns
for pattern in patterns {
Expand Down Expand Up @@ -308,7 +312,7 @@ impl SplitWriter<'_> {
input_iter: &mut InputSplitter<I>,
) -> Result<(), CsplitError>
where
I: Iterator<Item = (usize, io::Result<String>)>,
I: Iterator<Item = (usize, UResult<String>)>,
{
input_iter.rewind_buffer();
input_iter.set_size_of_buffer(1);
Expand Down Expand Up @@ -361,7 +365,7 @@ impl SplitWriter<'_> {
input_iter: &mut InputSplitter<I>,
) -> Result<(), CsplitError>
where
I: Iterator<Item = (usize, io::Result<String>)>,
I: Iterator<Item = (usize, UResult<String>)>,
{
if offset >= 0 {
// The offset is zero or positive, no need for a buffer on the lines read.
Expand Down Expand Up @@ -456,7 +460,7 @@ impl SplitWriter<'_> {
/// This is used to pass matching lines to the next split and to support patterns with a negative offset.
struct InputSplitter<I>
where
I: Iterator<Item = (usize, io::Result<String>)>,
I: Iterator<Item = (usize, UResult<String>)>,
{
iter: I,
buffer: Vec<<I as Iterator>::Item>,
Expand All @@ -469,7 +473,7 @@ where

impl<I> InputSplitter<I>
where
I: Iterator<Item = (usize, io::Result<String>)>,
I: Iterator<Item = (usize, UResult<String>)>,
{
fn new(iter: I) -> Self {
Self {
Expand Down Expand Up @@ -533,7 +537,7 @@ where

impl<I> Iterator for InputSplitter<I>
where
I: Iterator<Item = (usize, io::Result<String>)>,
I: Iterator<Item = (usize, UResult<String>)>,
{
type Item = <I as Iterator>::Item;

Expand Down
13 changes: 12 additions & 1 deletion src/uu/csplit/src/csplit_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum CsplitError {
SuffixFormatTooManyPercents,
#[error("{} is not a regular file", ._0.quote())]
NotRegularFile(String),
#[error("{}", _0)]
UError(Box<dyn UError>),
}

impl From<io::Error> for CsplitError {
Expand All @@ -43,8 +45,17 @@ impl From<io::Error> for CsplitError {
}
}

impl From<Box<dyn UError>> for CsplitError {
fn from(error: Box<dyn UError>) -> Self {
Self::UError(error)
}
}

impl UError for CsplitError {
fn code(&self) -> i32 {
1
match self {
Self::UError(e) => e.code(),
_ => 1,
}
}
}

0 comments on commit 8a54b84

Please sign in to comment.