Cotton code
139 lines · 4.8 KB · RustRaw
//! **The one way a photo is handed to the phone** (rule 2; the coordinator's//! decision M-b, ALBUMS CORE 1f): the compiler is the lint.//!//! A [`Mark::Handed`](super::bin::Mark::Handed) carries a [`Seal`], and a//! [`PlatformList`] has private fields: outside this crate neither can be//! made, and inside it only [`free_plan`] makes them (a stored Handed mark is//! decoded, which is the only other way one exists). The app's index supplies//! the facts through [`Store`]; every check at the moment of handing is the//! store's [`Store::verdict`], and only a `Go` is handed.//!//! ```compile_fail//! use cotton_core::albums::{bin::Mark, handing::Seal};//! let _ = Mark::Handed { at_ms: 0, seal: Seal(()) };//! ```//!//! ```compile_fail//! use cotton_core::albums::handing::PlatformList;//! let _ = PlatformList { act: "delete", parts: vec![], act_id: String::new() };//! ```use super::bin::{self, Mark};use super::Platform;/// The proof a Handed mark was made by [`free_plan`]. Its field is private.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pub struct Seal(());impl Seal {    /// Only for decoding a mark the index stored (serde's default).    pub(crate) fn decoded() -> Seal {        Seal(())    }}/// What the phone is asked to delete (iPhone) or trash (Android), batch by/// batch. Nothing outside [`free_plan`] can make one.#[derive(Debug)]pub struct PlatformList {    act: &'static str,    parts: Vec<Vec<String>>,    act_id: String,}impl PlatformList {    /// `delete` (iPhone) or `trash` (Android).    pub fn act(&self) -> &'static str {        self.act    }    /// The ids, one system request each.    pub fn parts(&self) -> &[Vec<String>] {        &self.parts    }    /// The Delete this list belongs to.    pub fn act_id(&self) -> &str {        &self.act_id    }}/// A photo a Delete planned to hand, not yet decided.#[derive(Debug, Clone)]pub struct Waiting {    pub pos: i64,    pub id: String,    pub why: String,    /// A main item (not a RAW or other part of one).    pub main: bool,}/// What the store's checks say of a waiting photo at this moment.#[derive(Debug, Clone, PartialEq, Eq)]pub enum Verdict {    /// Every check holds: hand it.    Go,    /// No longer binned, or not present, or a part whose main did not go.    Skip,    /// Stays in the Bin, not handed, with the reason (edited since he binned    /// it; the library not yet read again).    Hold(String),    /// Kept, with the reason (a copy rule).    KeepBack(String),}/// How far a Delete's handing has come.#[derive(Debug)]pub enum Listed {    /// Handed this many so far; more slices to come.    More(usize),    /// All decided: the list to ask the phone, or none when nothing went.    Done(Option<PlatformList>),}/// The index the handing reads and writes.pub trait Store {    type Error;    /// Up to `budget` photos of this Delete still to decide, in order.    fn waiting(&mut self, budget: usize) -> Result<Vec<Waiting>, Self::Error>;    /// Every check, at this moment.    fn verdict(&mut self, w: &Waiting) -> Result<Verdict, Self::Error>;    /// Write the Handed mark (made here, nowhere else).    fn hand(&mut self, w: &Waiting, mark: Mark) -> Result<(), Self::Error>;    /// Record a photo that was not handed, and why.    fn decided(&mut self, w: &Waiting, verdict: &Verdict) -> Result<(), Self::Error>;    /// After each slice: the ids handed in it.    fn slice_done(&mut self, handed: &[String]) -> Result<(), Self::Error>;    /// How many are still to decide, and every id this Delete handed so far.    fn progress(&mut self) -> Result<(usize, Vec<String>), Self::Error>;    /// The Delete is decided: asking the phone (`true`) or over with nothing to ask.    fn finish(&mut self, asking: bool) -> Result<(), Self::Error>;}/// Decide up to `budget` photos of a Delete, handing each whose every check/// holds; once all are decided, the one list the phone is asked.pub fn free_plan<S: Store>(store: &mut S, act_id: &str, platform: Platform, now: i64, budget: usize) -> Result<Listed, S::Error> {    let mut handed = Vec::new();    for w in store.waiting(budget)? {        match store.verdict(&w)? {            Verdict::Go => {                store.hand(&w, Mark::Handed { at_ms: now, seal: Seal(()) })?;                handed.push(w.id.clone());            }            other => store.decided(&w, &other)?,        }    }    store.slice_done(&handed)?;    let (left, all) = store.progress()?;    if left > 0 {        return Ok(Listed::More(all.len()));    }    if all.is_empty() {        store.finish(false)?;        return Ok(Listed::Done(None));    }    store.finish(true)?;    let act = match platform {        Platform::Ios => "delete",        Platform::Android => "trash",    };    Ok(Listed::Done(Some(PlatformList { act, parts: bin::free_batches(platform, &all), act_id: act_id.to_string() })))}