From 6f95c04eb657c34c67de4cfde9c9ce12c5857940 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 1 Nov 2024 20:50:20 +0000 Subject: [PATCH] feat(fs): add lines to reader --- src/fs/real/reader.rs | 6 +++++- tests/fs.rs | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/fs/real/reader.rs b/src/fs/real/reader.rs index a524edd..c601159 100644 --- a/src/fs/real/reader.rs +++ b/src/fs/real/reader.rs @@ -1,5 +1,5 @@ // -use std::{fmt::Display, path::Path}; +use std::{fmt::Display, path::Path, str::Lines}; use crate::fs::Result; @@ -15,6 +15,10 @@ impl ReaderReal { pub fn as_str(&self) -> &str { &self.contents } + + pub fn lines(&self) -> Lines<'_> { + self.contents.lines() + } } impl Display for ReaderReal { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/tests/fs.rs b/tests/fs.rs index 2c32f7d..15dae46 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -114,6 +114,22 @@ mod file { Ok(()) } + + #[test] + fn read_file_lines() -> TestResult { + let fs = fs::temp().expect("temp fs"); + + let path = fs.base().join("foo"); + let mut file = fs.file(&path); + file.write("line 1\nline 2").expect("write"); + + let reader = file.reader().expect("reader"); + let lines = reader.lines().collect::>(); + + assert_eq!(lines, vec!["line 1", "line 2"]); + + Ok(()) + } } mod dir_create {