atomic_ops/process/file/
remove.rs1use std::{
2 path::{Path, PathBuf},
3 sync::Arc,
4};
5
6use serde::{Deserialize, Serialize};
7use serde_flow::encoder::{bincode, FlowEncoder};
8
9use crate::{
10 error::{OpsError, OpsResult},
11 operation::LoaderFn,
12 process::{constants::DELETE_FILE_ID, Process},
13};
14
15#[derive(Serialize, Deserialize, Default)]
16pub struct Op {
17 from: PathBuf,
18
19 #[serde(skip)]
20 bytes: Vec<u8>,
21}
22
23impl Op {
24 #[must_use]
25 pub fn new(from: &Path) -> Self {
26 let mut object = Self {
27 from: from.to_path_buf(),
28 bytes: Vec::new(),
29 };
30 object.bytes = bincode::Encoder::serialize(&object).unwrap();
31 object
32 }
33
34 pub fn from_bytes(bytes: &[u8]) -> OpsResult<Self> {
35 let mut decoded: Op =
36 bincode::Encoder::deserialize(bytes).map_err(|_| OpsError::SerializeFailed)?;
37 decoded.bytes = bytes.to_vec();
38 Ok(decoded)
39 }
40}
41
42pub fn load(bytes: &[u8]) -> OpsResult<Box<dyn Process>> {
43 let value = Op::from_bytes(bytes)?;
44 Ok(Box::new(value))
45}
46
47pub fn loader() -> (u8, LoaderFn) {
48 (DELETE_FILE_ID, Arc::new(load))
49}
50
51impl Process for Op {
52 #[inline]
53 fn prepare(&self) -> OpsResult<()> {
54 Ok(())
55 }
56
57 #[inline]
58 fn run(&self) -> OpsResult<()> {
59 Ok(())
60 }
61
62 #[inline]
63 fn clean(&self) -> OpsResult<()> {
64 std::fs::remove_file(self.from.as_path())?;
65 Ok(())
66 }
67
68 #[inline]
69 fn revert_prepare(&self) -> OpsResult<()> {
70 Ok(())
71 }
72
73 #[inline]
74 fn revert_run(&self) -> OpsResult<()> {
75 Ok(())
76 }
77
78 fn id() -> u8 {
79 DELETE_FILE_ID
80 }
81
82 fn as_bytes(&self) -> &[u8] {
83 &self.bytes
84 }
85}
86
87#[cfg(test)]
88pub mod tests {
89
90 #[test]
91 fn remove_file() {}
92}