Move `thread` COB to test module
Threads are only intended to be used as part of other COBs. Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
f26674a5a9
commit
c68a5a8857
|
|
@ -1,16 +1,13 @@
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::ops::{ControlFlow, Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use radicle_crdt as crdt;
|
use radicle_crdt as crdt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::cob;
|
use crate::cob;
|
||||||
use crate::cob::common::{Reaction, Timestamp};
|
use crate::cob::common::{Reaction, Timestamp};
|
||||||
use crate::cob::store;
|
use crate::cob::{ActorId, Op, OpId};
|
||||||
use crate::cob::{ActorId, History, Op, OpId, TypeName};
|
|
||||||
use crate::crypto::Signer;
|
use crate::crypto::Signer;
|
||||||
|
|
||||||
use crdt::clock::Lamport;
|
use crdt::clock::Lamport;
|
||||||
|
|
@ -19,12 +16,6 @@ use crdt::lwwset::LWWSet;
|
||||||
use crdt::redactable::Redactable;
|
use crdt::redactable::Redactable;
|
||||||
use crdt::Semilattice;
|
use crdt::Semilattice;
|
||||||
|
|
||||||
use super::op::Ops;
|
|
||||||
|
|
||||||
/// Type name of a thread.
|
|
||||||
pub static TYPENAME: Lazy<TypeName> =
|
|
||||||
Lazy::new(|| FromStr::from_str("xyz.radicle.thread").expect("type name is valid"));
|
|
||||||
|
|
||||||
/// Identifies a comment.
|
/// Identifies a comment.
|
||||||
pub type CommentId = OpId;
|
pub type CommentId = OpId;
|
||||||
|
|
||||||
|
|
@ -99,26 +90,6 @@ pub struct Thread {
|
||||||
reactions: GMap<CommentId, LWWSet<(ActorId, Reaction), Lamport>>,
|
reactions: GMap<CommentId, LWWSet<(ActorId, Reaction), Lamport>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl store::FromHistory for Thread {
|
|
||||||
type Action = Action;
|
|
||||||
|
|
||||||
fn type_name() -> &'static radicle_cob::TypeName {
|
|
||||||
&*TYPENAME
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_history(history: &History) -> Result<(Self, Lamport), store::Error> {
|
|
||||||
let obj = history.traverse(Thread::default(), |mut acc, entry| {
|
|
||||||
if let Ok(Ops(ops)) = Ops::try_from(entry) {
|
|
||||||
acc.apply(ops);
|
|
||||||
ControlFlow::Continue(acc)
|
|
||||||
} else {
|
|
||||||
ControlFlow::Break(acc)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Ok((obj, history.clock().into()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Semilattice for Thread {
|
impl Semilattice for Thread {
|
||||||
fn merge(&mut self, other: Self) {
|
fn merge(&mut self, other: Self) {
|
||||||
self.comments.merge(other.comments);
|
self.comments.merge(other.comments);
|
||||||
|
|
@ -278,16 +249,45 @@ impl<G> DerefMut for Actor<G> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::ops::ControlFlow;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::{array, iter};
|
use std::{array, iter};
|
||||||
|
|
||||||
use crate::crypto::test::signer::MockSigner;
|
use cob::op::Ops;
|
||||||
use nonempty::NonEmpty;
|
use nonempty::NonEmpty;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use qcheck::{Arbitrary, TestResult};
|
use qcheck::{Arbitrary, TestResult};
|
||||||
|
|
||||||
|
use crdt::test::{assert_laws, WeightedGenerator};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate as radicle;
|
use crate as radicle;
|
||||||
use crdt::test::{assert_laws, WeightedGenerator};
|
use crate::crypto::test::signer::MockSigner;
|
||||||
|
|
||||||
|
/// Type name of a thread.
|
||||||
|
pub static TYPENAME: Lazy<cob::TypeName> =
|
||||||
|
Lazy::new(|| FromStr::from_str("xyz.radicle.thread").expect("type name is valid"));
|
||||||
|
|
||||||
|
impl cob::store::FromHistory for Thread {
|
||||||
|
type Action = Action;
|
||||||
|
|
||||||
|
fn type_name() -> &'static radicle_cob::TypeName {
|
||||||
|
&*TYPENAME
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_history(history: &cob::History) -> Result<(Self, Lamport), cob::store::Error> {
|
||||||
|
let obj = history.traverse(Thread::default(), |mut acc, entry| {
|
||||||
|
if let Ok(Ops(ops)) = Ops::try_from(entry) {
|
||||||
|
acc.apply(ops);
|
||||||
|
ControlFlow::Continue(acc)
|
||||||
|
} else {
|
||||||
|
ControlFlow::Break(acc)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Ok((obj, history.clock().into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Changes<const N: usize> {
|
struct Changes<const N: usize> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue