Skip to content

Commit ae0beb4

Browse files
committed
unify read functions
1 parent 8456d6a commit ae0beb4

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

lib/src/lib.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,23 @@ pub struct Project<'a> {
2828
indent_string: Option<&'a str>,
2929
}
3030
impl<'a> Project<'a> {
31-
pub fn open_with_indent(file: impl Read, indent_string: &'a str) -> Result<Self, Error> {
31+
fn read_file(file: impl Read) -> Result<String, Error> {
3232
let mut file = BufReader::new(file);
33-
let content = {
34-
let mut buf = String::new();
35-
file.read_to_string(&mut buf)?;
36-
buf
37-
};
33+
let mut buf = String::new();
34+
file.read_to_string(&mut buf)?;
35+
Ok(buf)
36+
}
3837

38+
pub fn open_with_indent(file: impl Read, indent_string: &'a str) -> Result<Self, Error> {
3939
Ok(Self {
40-
content,
40+
content: Self::read_file(file)?,
4141
indent_string: Some(indent_string),
4242
})
4343
}
4444

4545
pub fn open(file: impl Read) -> Result<Self, Error> {
46-
let mut file = BufReader::new(file);
47-
let content = {
48-
let mut buf = String::new();
49-
file.read_to_string(&mut buf)?;
50-
buf
51-
};
52-
5346
Ok(Self {
54-
content,
47+
content: Self::read_file(file)?,
5548
indent_string: None,
5649
})
5750
}
@@ -369,8 +362,8 @@ fn module(lua: &Lua) -> LuaResult<LuaTable> {
369362
(0..size).map(|_| ' ').collect()
370363
}
371364

372-
let indent =
373-
project.derive_indent()
365+
let indent = project
366+
.derive_indent()
374367
.or(indent.map(build_indent_string))
375368
.unwrap_or(build_indent_string(2));
376369

lib/src/tests/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,14 @@ fn ignore_empty_lines() -> AnyResult<()> {
142142
"#
143143
.as_bytes();
144144

145-
let project =
146-
Project::open_with_indent(input, " ")?.with_files(&["a", "b", "", " ", " ", "c"])?;
145+
let project = Project::open_with_indent(input, " ")?.with_files(&[
146+
"a",
147+
"b",
148+
"",
149+
" ",
150+
" ",
151+
"c",
152+
])?;
147153

148154
let files = {
149155
let mut buf = Cursor::new(vec![]);

0 commit comments

Comments
 (0)