use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct FileText {
pub path: PathBuf,
pub full_text: String,
pub lines: Vec<String>,
}
impl FileText {
pub fn new(path: PathBuf, content: String) -> Self {
let lines: Vec<String> = content.lines().map(|s| s.to_string()).collect();
Self {
path,
full_text: content,
lines,
}
}
pub fn get_full_text(&self) -> &str {
&self.full_text
}
pub fn get_line(&self, line_no: usize) -> Option<&str> {
if line_no > 0 && line_no <= self.lines.len() {
Some(&self.lines[line_no - 1])
} else {
None
}
}
pub fn line_count(&self) -> usize {
self.lines.len()
}
}
#[derive(Debug, Clone)]
pub struct FileContents {
pub file_text: FileText,
pub file_name: String,
}
impl FileContents {
pub fn new(file_text: FileText) -> Self {
let file_name = file_text
.path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
.to_string();
Self {
file_text,
file_name,
}
}
pub fn get_text(&self) -> &FileText {
&self.file_text
}
}