Skip to content

Commit 5a79bdb

Browse files
committed
refactor: add lossy_reader mod
1 parent 855b92f commit 5a79bdb

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

src/bin/code-minimap/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
};
66

77
use cli::{CompletionOpt, Opt, StructOpt, Subcommand};
8-
use code_minimap::LossyReader;
8+
use code_minimap::lossy_reader::LossyReader;
99

1010
fn main() {
1111
if let Err(e) = try_main() {

src/core.rs

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,10 @@
11
use std::{
2-
fs::File,
3-
io::{self, BufRead, BufReader, Read, Write},
2+
io::{self, BufRead, Write},
43
ops::Range,
5-
path::Path,
64
};
75

86
use itertools::Itertools;
97

10-
pub struct LossyReader {
11-
reader: BufReader<File>,
12-
}
13-
14-
impl LossyReader {
15-
pub fn open(path: impl AsRef<Path>) -> io::Result<Self> {
16-
let file = File::open(path)?;
17-
let reader = BufReader::new(file);
18-
19-
Ok(Self { reader })
20-
}
21-
}
22-
23-
impl Read for LossyReader {
24-
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
25-
self.reader.read(buf)
26-
}
27-
}
28-
29-
impl BufRead for LossyReader {
30-
fn fill_buf(&mut self) -> io::Result<&[u8]> {
31-
self.reader.fill_buf()
32-
}
33-
34-
fn consume(&mut self, amt: usize) {
35-
self.reader.consume(amt)
36-
}
37-
38-
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
39-
let mut append_buf = Vec::new();
40-
let res = self.read_until(0x0a, &mut append_buf);
41-
if let Err(err) = res {
42-
return Err(err);
43-
}
44-
buf.push_str(&String::from_utf8_lossy(&append_buf));
45-
Ok(buf.len())
46-
}
47-
}
48-
498
/// Write minimap to the writer.
509
pub fn write(
5110
mut writer: impl Write,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub(crate) mod core;
2+
pub mod lossy_reader;
23
pub use crate::core::*;

src/lossy_reader.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::{
2+
fs::File,
3+
io::{self, BufRead, BufReader, Read},
4+
path::Path,
5+
};
6+
7+
pub struct LossyReader {
8+
reader: BufReader<File>,
9+
}
10+
11+
impl LossyReader {
12+
pub fn open(path: impl AsRef<Path>) -> io::Result<Self> {
13+
let file = File::open(path)?;
14+
let reader = BufReader::new(file);
15+
16+
Ok(Self { reader })
17+
}
18+
}
19+
20+
impl Read for LossyReader {
21+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
22+
self.reader.read(buf)
23+
}
24+
}
25+
26+
impl BufRead for LossyReader {
27+
fn fill_buf(&mut self) -> io::Result<&[u8]> {
28+
self.reader.fill_buf()
29+
}
30+
31+
fn consume(&mut self, amt: usize) {
32+
self.reader.consume(amt)
33+
}
34+
35+
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
36+
let mut append_buf = Vec::new();
37+
let res = self.read_until(0x0a, &mut append_buf);
38+
if let Err(err) = res {
39+
return Err(err);
40+
}
41+
buf.push_str(&String::from_utf8_lossy(&append_buf));
42+
Ok(buf.len())
43+
}
44+
}

0 commit comments

Comments
 (0)