use {
crate::mm::{Buffer, BufferId, Position},
std::time::SystemTime,
};
#[derive(Debug, Clone)]
pub struct Snapshot {
lines: Vec<String>,
cursor: Position,
buffer_id: BufferId,
timestamp: SystemTime,
}
impl Snapshot {
#[must_use]
pub fn capture(buffer: &Buffer, cursor: Position) -> Self {
Self {
lines: buffer.lines().to_vec(),
cursor,
buffer_id: buffer.id(),
timestamp: SystemTime::now(),
}
}
#[must_use]
pub const fn from_parts(
lines: Vec<String>,
cursor: Position,
buffer_id: BufferId,
timestamp: SystemTime,
) -> Self {
Self {
lines,
cursor,
buffer_id,
timestamp,
}
}
pub fn restore(&self, buffer: &mut Buffer) -> Position {
let content = self.lines.join("\n");
buffer.set_content(&content);
self.cursor
}
#[must_use]
pub const fn cursor(&self) -> Position {
self.cursor
}
#[must_use]
pub fn lines(&self) -> &[String] {
&self.lines
}
#[must_use]
pub const fn buffer_id(&self) -> BufferId {
self.buffer_id
}
#[must_use]
pub const fn timestamp(&self) -> SystemTime {
self.timestamp
}
#[must_use]
pub fn matches_buffer(&self, buffer: &Buffer) -> bool {
self.buffer_id == buffer.id()
}
#[must_use]
pub fn char_count(&self) -> usize {
self.lines.iter().map(|l| l.chars().count()).sum::<usize>()
+ self.lines.len().saturating_sub(1) }
#[must_use]
pub const fn line_count(&self) -> usize {
self.lines.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.lines.is_empty()
}
}