use radicle::{
cob::{
self,
cache::{MigrateCallback, MigrateProgress},
},
prelude::NodeId,
profile,
storage::ReadRepository,
Profile,
};
use radicle_term as term;
use crate::terminal;
pub const MIGRATION_HINT: &str = "run `rad cob migrate` to update your database";
pub struct MigrateSpinner {
spinner: Option<term::Spinner>,
}
impl Default for MigrateSpinner {
fn default() -> Self {
Self { spinner: None }
}
}
impl MigrateCallback for MigrateSpinner {
fn progress(&mut self, progress: MigrateProgress) {
self.spinner
.get_or_insert_with(|| term::spinner("Migration in progress.."))
.message(format!(
"Migration {}/{} in progress.. ({}%)",
progress.migration.current(),
progress.migration.total(),
progress.rows.percentage()
));
if progress.is_done() {
if let Some(spinner) = self.spinner.take() {
spinner.finish()
}
}
}
}
pub mod migrate {
use super::MigrateSpinner;
#[must_use]
pub fn spinner() -> MigrateSpinner {
MigrateSpinner::default()
}
}
pub fn patches<'a, R>(
profile: &Profile,
repository: &'a R,
) -> Result<cob::patch::Cache<cob::patch::Patches<'a, R>, cob::cache::StoreReader>, anyhow::Error>
where
R: ReadRepository + cob::Store<Namespace = NodeId>,
{
profile.patches(repository).map_err(with_hint)
}
pub fn patches_mut<'a, R>(
profile: &Profile,
repository: &'a R,
) -> Result<cob::patch::Cache<cob::patch::Patches<'a, R>, cob::cache::StoreWriter>, anyhow::Error>
where
R: ReadRepository + cob::Store<Namespace = NodeId>,
{
profile.patches_mut(repository).map_err(with_hint)
}
pub fn issues<'a, R>(
profile: &Profile,
repository: &'a R,
) -> Result<cob::issue::Cache<cob::issue::Issues<'a, R>, cob::cache::StoreReader>, anyhow::Error>
where
R: ReadRepository + cob::Store<Namespace = NodeId>,
{
profile.issues(repository).map_err(with_hint)
}
pub fn issues_mut<'a, R>(
profile: &Profile,
repository: &'a R,
) -> Result<cob::issue::Cache<cob::issue::Issues<'a, R>, cob::cache::StoreWriter>, anyhow::Error>
where
R: ReadRepository + cob::Store<Namespace = NodeId>,
{
profile.issues_mut(repository).map_err(with_hint)
}
fn with_hint(e: profile::Error) -> anyhow::Error {
#[allow(clippy::wildcard_enum_match_arm)]
match e {
profile::Error::CobsCache(cob::cache::Error::OutOfDate) => {
terminal::args::Error::with_hint(e, MIGRATION_HINT).into()
}
e => anyhow::Error::from(e),
}
}