Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
radicle: generic Transaction::initial
Merged fintohaps opened 1 year ago

The use of Transaction outside of the radicle crate requires that a newtype is used, if the upstream code wants to write methods, such as:

pub struct Transaction<R>(store::Transaction<MyCob, R>);

impl Transaction<R> {
  pub fn add_foo(&mut self, foo: Foo) -> Result<(), store::Error> { /* code */ }
  pub fn add_bar(&mut self, bar: Bar) -> Result<(), store::Error> { /* code */ }}

This works up until the point that it tries to re-use the Transaction::initial method.

To make this possible, the method needs to know that it can be converted to the upstream Transaction and back to radicle Transaction.

1 file changed +6 -3 170915ff dd5f7396
modified radicle/src/cob/store.rs
@@ -314,20 +314,23 @@ where
    T: Cob + cob::Evaluate<R>,
{
    /// Create a new transaction to be used as the initial set of operations for a COB.
-
    pub fn initial<G, F>(
+
    pub fn initial<G, F, Tx>(
        message: &str,
        store: &mut Store<T, R>,
        signer: &G,
        operations: F,
    ) -> Result<(ObjectId, T), Error>
    where
+
        Tx: From<Self>,
+
        Self: From<Tx>,
        G: Signer,
-
        F: FnOnce(&mut Self, &R) -> Result<(), Error>,
+
        F: FnOnce(&mut Tx, &R) -> Result<(), Error>,
        R: ReadRepository + SignRepository + cob::Store,
        T::Action: Serialize + Clone,
    {
-
        let mut tx = Transaction::default();
+
        let mut tx = Tx::from(Transaction::default());
        operations(&mut tx, store.as_ref())?;
+
        let tx = Self::from(tx);

        let actions = NonEmpty::from_vec(tx.actions)
            .expect("Transaction::initial: transaction must contain at least one action");