1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use futures::TryStreamExt;

use tonic::Status;

use crate::source::DataSource;
use crate::target::DataTarget;

/// Copies the rows got from the source to the target.
pub async fn copy<S, T>(src: &S, tgt: &T) -> Result<u64, Status>
where
    S: DataSource,
    T: DataTarget<Row = S::Row>,
{
    let row_strm = src.get_rows().await?;
    row_strm
        .try_fold(0, |state, next| async move {
            let row: &S::Row = &next;
            tgt.save(row).await?;
            Ok(state + 1)
        })
        .await
}