Refactoring a large struct method


#1

I try to avoid large object and method definitions.

Say for instance I have the following:

struct Parser {

}

impl Parser {
    // many other methods (not shown)
 
    fn process(&self, lines: &[Lines], query: Query) {
       match query {
           .. => handle_lines_1(lines, arg),
           .. => handle_lines_2(lines),
           .. => handle_lines_3(lines, arg, arg),
       }
    }

    fn handle_lines_1(&self, &[Lines], arg2: ..) { ... }

    fn handle_lines_2(&self, &[Lines]) { ... }

    fn handle_lines_3(&self, &[Lines], arg2: .., arg3: ..) { ... }

}

The method process is too large so I break it down into some other methods.
But I’m not too found of passing the Lines collection over and over, so I’ve created a LinesParser struct as follows:

fn process(&self, ..) {
    let lp = LinesParser { parser: self, lines: lines};
    lp.handle_all();
}

struct LinesParser<'a> {
    lines: &'a [Lines],
    parser: &'a Parser
}

impl<'a> LinesParser<'a> {
    fn handle_all(&self) { ... }

    fn handle_1(&self) { ... }

    fn handle_2(&self) { ... }

    fn handle_3(&self) { ... }
}

This works, but somehow it doesn’t feel elegant. I would appreciate suggestions or improvements.

Thanks!