1use 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#[derive(Clone, Debug, Error)]
51pub enum VdirItemDeleteError {
52 #[error("Vdir item delete failed: unexpected arg {0:?}")]
55 UnexpectedArg(Option<VdirReply>),
56
57 #[error(transparent)]
59 Locate(#[from] VdirItemLocateError),
60}
61
62#[derive(Clone, Debug, Default, Eq, PartialEq)]
64pub struct VdirItemDeleteOptions {}
65
66#[derive(Debug)]
68pub struct VdirItemDelete {
69 state: State,
70 #[allow(dead_code)]
71 opts: VdirItemDeleteOptions,
72}
73
74impl VdirItemDelete {
75 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}