use std::sync::Arc;
use crate::commit::Commit;
use crate::entry::Entry;
use crate::id::CommitId;
use crate::store::Store;
#[derive(Debug, Clone)]
pub struct ReadView {
commit: Arc<Commit>,
}
impl ReadView {
pub(crate) fn new(commit: Arc<Commit>) -> Self {
Self { commit }
}
pub fn commit(&self) -> &Arc<Commit> {
&self.commit
}
pub fn commit_id(&self) -> CommitId {
self.commit.id
}
pub fn seq(&self) -> u64 {
self.commit.seq
}
pub fn get(&self, key: &str) -> Option<Arc<Entry>> {
self.commit.root.get(key).cloned()
}
pub fn contains(&self, key: &str) -> bool {
self.commit.root.get(key).is_some()
}
pub fn len(&self) -> usize {
self.commit.root.len()
}
pub fn is_empty(&self) -> bool {
self.commit.root.is_empty()
}
pub fn list(&self, prefix: &str, limit: Option<usize>) -> Vec<(String, Arc<Entry>)> {
let it = self
.commit
.root
.range_prefix(prefix)
.map(|(k, v)| (k.clone(), Arc::clone(v)));
match limit {
Some(n) => it.take(n).collect(),
None => it.collect(),
}
}
pub fn keys(&self) -> Vec<String> {
self.commit.root.iter().map(|(k, _)| k.clone()).collect()
}
}
#[derive(Debug, Clone)]
pub struct BranchInfo {
pub name: String,
pub head: CommitId,
pub seq: u64,
pub key_count: usize,
pub is_default: bool,
pub forked_at: CommitId,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeKind {
Added,
Removed,
Modified,
}
impl ChangeKind {
pub fn marker(self) -> char {
match self {
ChangeKind::Added => '+',
ChangeKind::Removed => '-',
ChangeKind::Modified => '~',
}
}
}
#[derive(Debug, Clone)]
pub struct Change {
pub key: String,
pub kind: ChangeKind,
}