ic-query 0.2.10

Internet Computer query CLI for NNS, SNS, and related public network metadata
Documentation
//! Module: sns::report::proposals_cache::attempt::write
//!
//! Responsibility: write proposal refresh-attempt status metadata.
//! Does not own: live proposal paging, cache publication, or status report rendering.
//! Boundary: persists running and complete refresh-attempt states.

use super::super::errors::sns_cache_file_error;
use super::super::model::SnsProposalsRefreshAttempt;
use super::model::{
    SnsProposalsAttemptContext, SnsProposalsAttemptParts, SnsProposalsAttemptProgress,
    attempt_from_parts,
};
use crate::{snapshot_cache::write_snapshot_refresh_attempt, sns::report::SnsHostError};
use std::path::Path;

/// Write the initial running proposal refresh-attempt file.
pub(in crate::sns::report::proposals_cache) fn write_starting_attempt(
    context: SnsProposalsAttemptContext<'_>,
) -> Result<(), SnsHostError> {
    write_attempt_status(
        context,
        "running",
        SnsProposalsAttemptProgress::starting(),
        None,
    )
}

/// Write updated running proposal refresh progress.
pub(in crate::sns::report::proposals_cache) fn write_running_attempt(
    context: SnsProposalsAttemptContext<'_>,
    progress: SnsProposalsAttemptProgress,
) -> Result<(), SnsHostError> {
    write_attempt_status(context, "running", progress, None)
}

/// Write successful proposal refresh completion metadata.
pub(in crate::sns::report::proposals_cache) fn write_complete_attempt(
    context: SnsProposalsAttemptContext<'_>,
    progress: SnsProposalsAttemptProgress,
) -> Result<(), SnsHostError> {
    write_attempt_status(context, "complete", progress, None)
}

pub(in crate::sns::report::proposals_cache::attempt) fn write_attempt_status(
    context: SnsProposalsAttemptContext<'_>,
    status: &'static str,
    progress: SnsProposalsAttemptProgress,
    last_error: Option<String>,
) -> Result<(), SnsHostError> {
    write_proposals_attempt(
        context.path,
        &attempt_from_parts(SnsProposalsAttemptParts {
            context,
            status,
            progress,
            last_error,
        }),
    )
}

fn write_proposals_attempt(
    path: &Path,
    attempt: &SnsProposalsRefreshAttempt,
) -> Result<(), SnsHostError> {
    write_snapshot_refresh_attempt(
        path,
        attempt,
        |path, source| SnsHostError::SerializeCache { path, source },
        sns_cache_file_error,
    )
}