any_storage/
noop.rs

1use std::io::{Error, ErrorKind, Result};
2use std::ops::RangeBounds;
3use std::path::PathBuf;
4use std::pin::Pin;
5use std::task::Poll;
6
7use futures::Stream;
8
9use crate::{Entry, Store, StoreDirectory, StoreFile, StoreFileReader};
10
11/// Configuration for [`NoopStore`].
12#[derive(Clone, Debug)]
13#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
14pub struct NoopStoreConfig;
15
16impl NoopStoreConfig {
17    pub fn build(&self) -> Result<NoopStore> {
18        Ok(NoopStore)
19    }
20}
21
22/// Noop store, does nothing, can't do anything
23#[derive(Clone, Debug, Default)]
24pub struct NoopStore;
25
26impl Store for NoopStore {
27    type Directory = NoopStoreDirectory;
28    type File = NoopStoreFile;
29
30    async fn get_dir<P: Into<PathBuf>>(&self, path: P) -> Result<Self::Directory> {
31        let path = path.into();
32        Ok(NoopStoreDirectory { path })
33    }
34
35    async fn get_file<P: Into<PathBuf>>(&self, path: P) -> Result<Self::File> {
36        let path = path.into();
37        Ok(NoopStoreFile { path })
38    }
39}
40
41pub type NoopStoreEntry = Entry<NoopStoreFile, NoopStoreDirectory>;
42
43/// Representation of a directory in the noop store.
44#[derive(Debug)]
45pub struct NoopStoreDirectory {
46    path: PathBuf,
47}
48
49impl StoreDirectory for NoopStoreDirectory {
50    type Entry = NoopStoreEntry;
51    type Reader = NoopStoreDirectoryReader;
52
53    fn path(&self) -> &std::path::Path {
54        &self.path
55    }
56
57    async fn exists(&self) -> Result<bool> {
58        Ok(false)
59    }
60
61    async fn read(&self) -> Result<Self::Reader> {
62        Err(Error::new(ErrorKind::NotFound, "directory not found"))
63    }
64
65    async fn delete(&self) -> Result<()> {
66        Err(Error::new(ErrorKind::NotFound, "directory not found"))
67    }
68
69    async fn delete_recursive(&self) -> Result<()> {
70        Err(Error::new(ErrorKind::NotFound, "directory not found"))
71    }
72}
73
74/// Reader for streaming entries from a noop store directory.
75///
76/// Should never be built.
77#[derive(Debug)]
78pub struct NoopStoreDirectoryReader;
79
80impl Stream for NoopStoreDirectoryReader {
81    type Item = Result<NoopStoreEntry>;
82
83    fn poll_next(
84        self: Pin<&mut Self>,
85        _cx: &mut std::task::Context<'_>,
86    ) -> Poll<Option<Self::Item>> {
87        Poll::Ready(None)
88    }
89}
90
91impl crate::StoreDirectoryReader<NoopStoreEntry> for NoopStoreDirectoryReader {}
92
93/// Representation of a file in the noop store.
94#[derive(Debug)]
95pub struct NoopStoreFile {
96    path: PathBuf,
97}
98
99impl StoreFile for NoopStoreFile {
100    type FileReader = NoopStoreFileReader;
101    type FileWriter = NoopStoreFileWriter;
102    type Metadata = NoopStoreFileMetadata;
103
104    fn path(&self) -> &std::path::Path {
105        &self.path
106    }
107
108    async fn exists(&self) -> Result<bool> {
109        Ok(false)
110    }
111
112    async fn metadata(&self) -> Result<Self::Metadata> {
113        Err(Error::new(ErrorKind::NotFound, "file not found"))
114    }
115
116    async fn read<R: RangeBounds<u64>>(&self, _range: R) -> Result<Self::FileReader> {
117        Err(Error::new(ErrorKind::NotFound, "file not found"))
118    }
119
120    async fn write(&self, _options: crate::WriteOptions) -> Result<Self::FileWriter> {
121        Err(Error::new(ErrorKind::Unsupported, "unable to write"))
122    }
123
124    async fn delete(&self) -> Result<()> {
125        Err(Error::new(ErrorKind::NotFound, "file not found"))
126    }
127}
128
129/// Metadata associated with a file in the store store.
130///
131/// Should never be built.
132#[derive(Clone, Debug)]
133pub struct NoopStoreFileMetadata;
134
135impl super::StoreMetadata for NoopStoreFileMetadata {
136    fn size(&self) -> u64 {
137        0
138    }
139
140    fn created(&self) -> u64 {
141        0
142    }
143
144    fn modified(&self) -> u64 {
145        0
146    }
147
148    fn content_type(&self) -> Option<&str> {
149        None
150    }
151}
152
153/// Reader for asynchronously reading the contents of a file in the noop store.
154///
155/// Should never be built.
156#[derive(Debug)]
157pub struct NoopStoreFileReader;
158
159impl tokio::io::AsyncRead for NoopStoreFileReader {
160    fn poll_read(
161        self: std::pin::Pin<&mut Self>,
162        _cx: &mut std::task::Context<'_>,
163        _buf: &mut tokio::io::ReadBuf<'_>,
164    ) -> Poll<std::io::Result<()>> {
165        Poll::Ready(Err(Error::new(ErrorKind::NotFound, "file not found")))
166    }
167}
168
169impl StoreFileReader for NoopStoreFileReader {}
170
171/// Writer for files in the noop store.
172///
173/// Should never be built.
174#[derive(Debug)]
175pub struct NoopStoreFileWriter;
176
177impl tokio::io::AsyncWrite for NoopStoreFileWriter {
178    fn poll_write(
179        self: Pin<&mut Self>,
180        _cx: &mut std::task::Context<'_>,
181        _buf: &[u8],
182    ) -> Poll<Result<usize>> {
183        Poll::Ready(Err(Error::new(ErrorKind::Unsupported, "unable to write")))
184    }
185
186    fn poll_flush(self: Pin<&mut Self>, _cx: &mut std::task::Context<'_>) -> Poll<Result<()>> {
187        Poll::Ready(Err(Error::new(ErrorKind::Unsupported, "unable to write")))
188    }
189
190    fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut std::task::Context<'_>) -> Poll<Result<()>> {
191        Poll::Ready(Err(Error::new(ErrorKind::Unsupported, "unable to write")))
192    }
193}
194
195impl crate::StoreFileWriter for NoopStoreFileWriter {}