hecto/src/document.rs

31 lines
575 B
Rust
Raw Normal View History

2021-03-08 10:21:06 -05:00
use crate::Row;
2021-03-08 13:34:25 -05:00
use std::fs;
2021-03-08 10:21:06 -05:00
#[derive(Default)]
pub struct Document {
rows: Vec<Row>,
2021-03-08 10:43:40 -05:00
}
impl Document {
2021-03-08 13:34:25 -05:00
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
let contents = fs::read_to_string(filename)?;
2021-03-08 10:43:40 -05:00
let mut rows = Vec::new();
2021-03-08 13:34:25 -05:00
for value in contents.lines() {
rows.push(Row::from(value));
}
Ok(Self {
rows
})
2021-03-08 10:43:40 -05:00
}
pub fn row(&self, index: usize) -> Option<&Row> {
self.rows.get(index)
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
2021-03-08 10:21:06 -05:00
}