Skip to main content

io_vdir/item/
delete.rs

1//! I/O-free coroutine deleting a Vdir item by its ID.
2//!
3//! Locates the item file via [`VdirItemLocate`], then removes it.
4//!
5//! # Example
6//!
7//! ```rust,no_run
8//! use std::fs;
9//!
10//! use io_vdir::{coroutine::*, item::delete::*};
11//!
12//! let opts = VdirItemDeleteOptions::default();
13//! let mut coroutine = VdirItemDelete::new("/tmp/vdir/contacts", "alice", opts);
14//! let mut arg = None;
15//!
16//! loop {
17//!     match coroutine.resume(arg.take()) {
18//!         VdirCoroutineState::Yielded(VdirYield::WantsFileExists(paths)) => {
19//!             let map = paths
20//!                 .into_iter()
21//!                 .map(|p| {
22//!                     let ok = fs::metadata(p.as_str()).map(|m| m.is_file()).unwrap_or(false);
23//!                     (p, ok)
24//!                 })
25//!                 .collect();
26//!             arg = Some(VdirReply::FileExists(map));
27//!         }
28//!         VdirCoroutineState::Yielded(VdirYield::WantsFileRemove(paths)) => {
29//!             for path in paths {
30//!                 fs::remove_file(path.as_str()).unwrap();
31//!             }
32//!             arg = Some(VdirReply::FileRemove);
33//!         }
34//!         VdirCoroutineState::Complete(Ok(())) => break,
35//!         VdirCoroutineState::Complete(Err(err)) => panic!("{err}"),
36//!         state => panic!("unexpected state {state:?}"),
37//!     }
38//! }
39//! ```
40
41use core::fmt;
42
43use alloc::{collections::BTreeSet, string::ToString};
44
45use thiserror::Error;
46
47use crate::{coroutine::*, item::locate::*, path::VdirPath, vdir_try};
48
49/// Failure causes during a [`VdirItemDelete`] step.
50#[derive(Clone, Debug, Error)]
51pub enum VdirItemDeleteError {
52    /// The driver fed back a reply that does not match the pending
53    /// request.
54    #[error("Vdir item delete failed: unexpected arg {0:?}")]
55    UnexpectedArg(Option<VdirReply>),
56
57    /// The inner locate coroutine failed.
58    #[error(transparent)]
59    Locate(#[from] VdirItemLocateError),
60}
61
62/// Options for [`VdirItemDelete::new`].
63#[derive(Clone, Debug, Default, Eq, PartialEq)]
64pub struct VdirItemDeleteOptions {}
65
66/// Locates a Vdir item by its ID and removes it.
67#[derive(Debug)]
68pub struct VdirItemDelete {
69    state: State,
70    #[allow(dead_code)]
71    opts: VdirItemDeleteOptions,
72}
73
74impl VdirItemDelete {
75    /// Creates a new coroutine that will delete item `id` from
76    /// `collection`.
77    pub fn new(
78        collection: impl Into<VdirPath>,
79        id: impl ToString,
80        opts: VdirItemDeleteOptions,
81    ) -> Self {
82        Self {
83            opts,
84            state: State::Locate(VdirItemLocate::new(
85                collection,
86                id,
87                VdirItemLocateOptions::default(),
88            )),
89        }
90    }
91}
92
93impl VdirCoroutine for VdirItemDelete {
94    type Yield = VdirYield;
95    type Return = Result<(), VdirItemDeleteError>;
96
97    fn resume(&mut self, arg: Option<VdirReply>) -> VdirCoroutineState<Self::Yield, Self::Return> {
98        match (&mut self.state, arg) {
99            (State::Locate(c), arg) => {
100                let out = vdir_try!(c, arg);
101                let paths = BTreeSet::from_iter([out.path]);
102                self.state = State::Remove;
103                VdirCoroutineState::Yielded(VdirYield::WantsFileRemove(paths))
104            }
105            (State::Remove, Some(VdirReply::FileRemove)) => VdirCoroutineState::Complete(Ok(())),
106            (_, arg) => {
107                let err = VdirItemDeleteError::UnexpectedArg(arg);
108                VdirCoroutineState::Complete(Err(err))
109            }
110        }
111    }
112}
113
114#[derive(Debug)]
115enum State {
116    Locate(VdirItemLocate),
117    Remove,
118}
119
120impl fmt::Display for State {
121    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122        match self {
123            Self::Locate(_) => f.write_str("locate item"),
124            Self::Remove => f.write_str("remove item"),
125        }
126    }
127}
128
129#[cfg(test)]
130mod tests {
131    use alloc::collections::BTreeMap;
132
133    use super::*;
134
135    #[test]
136    fn removes_located_item() {
137        let mut cor =
138            VdirItemDelete::new("root/contacts", "alice", VdirItemDeleteOptions::default());
139
140        match cor.resume(None) {
141            VdirCoroutineState::Yielded(VdirYield::WantsFileExists(_)) => {}
142            state => panic!("expected WantsFileExists, got {state:?}"),
143        }
144
145        let vcf = VdirPath::from("root/contacts/alice.vcf");
146        let mut exists = BTreeMap::new();
147        exists.insert(vcf.clone(), true);
148        exists.insert(VdirPath::from("root/contacts/alice.ics"), false);
149
150        let paths = match cor.resume(Some(VdirReply::FileExists(exists))) {
151            VdirCoroutineState::Yielded(VdirYield::WantsFileRemove(paths)) => paths,
152            state => panic!("expected WantsFileRemove, got {state:?}"),
153        };
154        assert!(paths.contains(&vcf));
155
156        match cor.resume(Some(VdirReply::FileRemove)) {
157            VdirCoroutineState::Complete(Ok(())) => {}
158            state => panic!("expected Complete(Ok), got {state:?}"),
159        }
160    }
161
162    #[test]
163    fn locate_error_is_forwarded() {
164        let mut cor =
165            VdirItemDelete::new("root/contacts", "alice", VdirItemDeleteOptions::default());
166        let _ = cor.resume(None);
167
168        let err = match cor.resume(Some(VdirReply::DirCreate)) {
169            VdirCoroutineState::Complete(Err(err)) => err,
170            state => panic!("expected Complete(Err), got {state:?}"),
171        };
172        assert!(matches!(err, VdirItemDeleteError::Locate(_)));
173    }
174}