use core::fmt;
use smallvec::SmallVec;
use crate::encode::Canonical;
use crate::entry::Op;
use crate::id::CommitId;
use crate::store::Root;
#[derive(Clone)]
pub struct Commit {
pub id: CommitId,
pub parents: SmallVec<[CommitId; 2]>,
pub root: Root,
pub seq: u64,
pub message: Option<String>,
pub ops: Vec<Op>,
}
impl Commit {
pub fn compute_id(parents: &[CommitId], message: Option<&str>, ops: &[Op]) -> CommitId {
let mut c = Canonical::new();
c.u64(parents.len() as u64);
for p in parents {
c.commit_id(p);
}
c.opt_str(message);
c.u64(ops.len() as u64);
for op in ops {
op.encode(&mut c);
}
c.finalize()
}
pub fn first_parent(&self) -> Option<CommitId> {
self.parents.first().copied()
}
pub(crate) const GENESIS_MESSAGE: &'static str = "genesis";
pub(crate) fn genesis() -> Self {
let id = Commit::compute_id(&[], Some(Commit::GENESIS_MESSAGE), &[]);
Commit {
id,
parents: SmallVec::new(),
root: Root::new_sync(),
seq: 0,
message: Some(Commit::GENESIS_MESSAGE.to_owned()),
ops: Vec::new(),
}
}
}
impl fmt::Debug for Commit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Commit")
.field("id", &self.id.to_hex())
.field(
"parents",
&self.parents.iter().map(|p| p.to_hex()).collect::<Vec<_>>(),
)
.field("seq", &self.seq)
.field("message", &self.message)
.field("ops", &self.ops.len())
.field("keys", &rpds::RedBlackTreeMap::size(&self.root))
.finish()
}
}
#[derive(Debug, Clone)]
pub struct LogEntry {
pub id: CommitId,
pub seq: u64,
pub parents: Vec<CommitId>,
pub message: Option<String>,
pub op_count: usize,
pub key_count: usize,
}