use std::convert::Infallible;
use std::fmt::Debug;
use git_ext::Oid;
use nonempty::NonEmpty;
use crate::change::store::{Manifest, Version};
use crate::{change, Entry, History, ObjectId, TypeName};
pub mod error;
mod create;
pub use create::{create, Create};
mod get;
pub use get::get;
pub mod info;
mod list;
pub use list::list;
mod remove;
pub use remove::remove;
mod update;
pub use update::{update, Update, Updated};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CollaborativeObject<T> {
pub manifest: Manifest,
pub object: T,
pub history: History,
pub id: ObjectId,
}
impl<T> CollaborativeObject<T> {
pub fn object(&self) -> &T {
&self.object
}
pub fn history(&self) -> &History {
&self.history
}
pub fn id(&self) -> &ObjectId {
&self.id
}
pub fn typename(&self) -> &TypeName {
&self.manifest.type_name
}
pub fn manifest(&self) -> &Manifest {
&self.manifest
}
}
pub trait Evaluate<R>: Sized + Debug + 'static {
type Error: std::error::Error + Send + Sync + 'static;
fn init(entry: &Entry, store: &R) -> Result<Self, Self::Error>;
fn apply<'a, I: Iterator<Item = (&'a Oid, &'a Entry)>>(
&mut self,
entry: &Entry,
concurrent: I,
store: &R,
) -> Result<(), Self::Error>;
}
impl<R> Evaluate<R> for NonEmpty<Entry> {
type Error = Infallible;
fn init(entry: &Entry, _store: &R) -> Result<Self, Self::Error> {
Ok(Self::new(entry.clone()))
}
fn apply<'a, I: Iterator<Item = (&'a Oid, &'a Entry)>>(
&mut self,
entry: &Entry,
_concurrent: I,
_store: &R,
) -> Result<(), Self::Error> {
self.push(entry.clone());
Ok(())
}
}
pub fn parse_refstr<R>(name: &R) -> Option<(TypeName, ObjectId)>
where
R: AsRef<git_ext::ref_format::RefStr>,
{
use git_ext::ref_format::Qualified;
let name = name.as_ref();
let refs_cobs = match name.to_namespaced() {
None => Qualified::from_refstr(name)?,
Some(ns) => ns.strip_namespace_recursive(),
};
let (_refs, _cobs, typename, mut object_id) = refs_cobs.non_empty_components();
let object = object_id
.next()
.and_then(|oid| oid.parse::<ObjectId>().ok())?;
let name = typename.parse::<TypeName>().ok()?;
Some((name, object))
}