radicle: generic Transaction::initial

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`.
This commit is contained in:
Fintan Halpenny 2025-02-13 16:35:56 +00:00
parent 170915ffcf
commit dd5f739630
1 changed files with 6 additions and 3 deletions

View File

@ -314,20 +314,23 @@ where
T: Cob + cob::Evaluate<R>, T: Cob + cob::Evaluate<R>,
{ {
/// Create a new transaction to be used as the initial set of operations for a COB. /// 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, message: &str,
store: &mut Store<T, R>, store: &mut Store<T, R>,
signer: &G, signer: &G,
operations: F, operations: F,
) -> Result<(ObjectId, T), Error> ) -> Result<(ObjectId, T), Error>
where where
Tx: From<Self>,
Self: From<Tx>,
G: Signer, G: Signer,
F: FnOnce(&mut Self, &R) -> Result<(), Error>, F: FnOnce(&mut Tx, &R) -> Result<(), Error>,
R: ReadRepository + SignRepository + cob::Store, R: ReadRepository + SignRepository + cob::Store,
T::Action: Serialize + Clone, T::Action: Serialize + Clone,
{ {
let mut tx = Transaction::default(); let mut tx = Tx::from(Transaction::default());
operations(&mut tx, store.as_ref())?; operations(&mut tx, store.as_ref())?;
let tx = Self::from(tx);
let actions = NonEmpty::from_vec(tx.actions) let actions = NonEmpty::from_vec(tx.actions)
.expect("Transaction::initial: transaction must contain at least one action"); .expect("Transaction::initial: transaction must contain at least one action");