Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Move `thread` COB to test module
Alexis Sellier committed 3 years ago
commit c68a5a88578e99db539da28301bfef1c3247472e
parent f26674a5a9745b3e11fd297a5a2fbfdbb1d49904
1 file changed +33 -33
modified radicle/src/cob/thread.rs
@@ -1,16 +1,13 @@
use std::cmp::Ordering;
use std::collections::BTreeMap;
-
use std::ops::{ControlFlow, Deref, DerefMut};
-
use std::str::FromStr;
+
use std::ops::{Deref, DerefMut};

-
use once_cell::sync::Lazy;
use radicle_crdt as crdt;
use serde::{Deserialize, Serialize};

use crate::cob;
use crate::cob::common::{Reaction, Timestamp};
-
use crate::cob::store;
-
use crate::cob::{ActorId, History, Op, OpId, TypeName};
+
use crate::cob::{ActorId, Op, OpId};
use crate::crypto::Signer;

use crdt::clock::Lamport;
@@ -19,12 +16,6 @@ use crdt::lwwset::LWWSet;
use crdt::redactable::Redactable;
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.
pub type CommentId = OpId;

@@ -99,26 +90,6 @@ pub struct Thread {
    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 {
    fn merge(&mut self, other: Self) {
        self.comments.merge(other.comments);
@@ -278,16 +249,45 @@ impl<G> DerefMut for Actor<G> {

#[cfg(test)]
mod tests {
+
    use std::ops::ControlFlow;
+
    use std::str::FromStr;
    use std::{array, iter};

-
    use crate::crypto::test::signer::MockSigner;
+
    use cob::op::Ops;
    use nonempty::NonEmpty;
+
    use once_cell::sync::Lazy;
    use pretty_assertions::assert_eq;
    use qcheck::{Arbitrary, TestResult};

+
    use crdt::test::{assert_laws, WeightedGenerator};
+

    use super::*;
    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)]
    struct Changes<const N: usize> {