// Copyright © 2021 The Radicle Link Contributors // // This file is part of radicle-link, distributed under the GPLv3 with Radicle // Linking Exception. For full terms see the included LICENSE file. use git_ext::Oid; use crate::pruning_fold; use super::HistoryType; #[derive(Clone, Debug, PartialEq, Hash, Eq)] pub enum Contents { Automerge(Vec), } impl Contents { pub fn automerge(bytes: &[u8]) -> Self { Self::Automerge(bytes.to_vec()) } } impl From<&Contents> for HistoryType { fn from(c: &Contents) -> Self { match c { Contents::Automerge(..) => HistoryType::Automerge, } } } impl AsRef<[u8]> for Contents { fn as_ref(&self) -> &[u8] { match self { Self::Automerge(bytes) => bytes, } } } /// A unique identifier for a history entry. #[derive(Clone, Copy, Debug, PartialEq, Hash, Eq, PartialOrd, Ord)] pub struct EntryId(Oid); impl From for EntryId { fn from(id: git2::Oid) -> Self { Self(id.into()) } } impl From for EntryId { fn from(id: Oid) -> Self { Self(id) } } impl From for Oid { fn from(EntryId(id): EntryId) -> Self { id } } /// One entry in the dependency graph for a change #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Entry { /// The identifier for this entry pub(super) id: EntryId, /// The content-address for this entry's author. pub(super) author: Option, /// The content-address for the resource this entry lives under. pub(super) resource: Oid, /// The child entries for this entry. pub(super) children: Vec, /// The contents of this entry. pub(super) contents: Contents, } impl Entry { pub fn new( id: Id1, author: Option, resource: Oid, children: ChildIds, contents: Contents, ) -> Self where Id1: Into, Id2: Into, ChildIds: IntoIterator, { Self { id: id.into(), author, resource, children: children.into_iter().map(|id| id.into()).collect(), contents, } } /// The ids of the changes this change depends on pub fn children(&self) -> impl Iterator { self.children.iter() } /// The `Oid` of the resource this change lives under. pub fn resource(&self) -> Oid { self.resource } /// The `Oid` of the author that made this change. pub fn author(&self) -> &Option { &self.author } /// The contents of this change pub fn contents(&self) -> &Contents { &self.contents } pub fn id(&self) -> &EntryId { &self.id } } impl pruning_fold::GraphNode for Entry { type Id = EntryId; fn id(&self) -> &Self::Id { &self.id } fn child_ids(&self) -> &[Self::Id] { &self.children } }