coqui_access_kit/file/source.rs
1//! Where a table is kept, as a provider's configuration names it.
2
3use std::sync::Arc;
4
5use coqui_access::guatiao::{FromWalk, Walk};
6use coqui_builder::Describe;
7
8use super::table::{Codec, FileTable};
9use crate::table::{Keyed, SourceError, Table};
10
11/// Where a table is kept: an object holding exactly one source kind, open to more kinds (a
12/// SQL database, an HTTP API) without changing what sits beside it.
13///
14/// `F` is the caller's format choice, a [`Codec`] that is also configuration (an enum of the
15/// formats a plugin offers). In a configuration:
16///
17/// ```json
18/// { "file": { "path": "accounts.toml", "format": "toml" } }
19/// ```
20#[derive(Debug, Clone, PartialEq, Eq, Describe)]
21pub struct Source<F> {
22 /// Records in one private file.
23 pub file: Option<FileSource<F>>,
24}
25
26/// A table kept in one private file.
27#[derive(Debug, Clone, PartialEq, Eq, Describe)]
28pub struct FileSource<F> {
29 /// The file. Created, readable by its owner alone, when the first record is put; a
30 /// missing file is an empty table.
31 pub path: String,
32 /// How the file is written.
33 pub format: F,
34 /// The most the file may hold, in bytes, read or written; absent is the provider's
35 /// default, which is [`DEFAULT_MAX_BYTES`](super::DEFAULT_MAX_BYTES) unless it sizes one
36 /// from its record limit.
37 pub max_bytes: Option<u32>,
38}
39
40impl<F> Source<F> {
41 /// The file at `path`, written in `format`.
42 pub fn file(path: impl Into<String>, format: F) -> Self {
43 Self {
44 file: Some(FileSource {
45 path: path.into(),
46 format,
47 max_bytes: None,
48 }),
49 }
50 }
51
52 /// This source with a size bound, in bytes, for the file.
53 #[must_use]
54 pub fn max_bytes(mut self, max: u32) -> Self {
55 if let Some(file) = &mut self.file {
56 file.max_bytes = Some(max);
57 }
58 self
59 }
60
61 /// This source with `max` as the size bound, unless one is set: a provider sizes the
62 /// default from the number of records it allows.
63 #[must_use]
64 pub fn max_bytes_unless_set(mut self, max: u32) -> Self {
65 if let Some(file) = &mut self.file {
66 file.max_bytes.get_or_insert(max);
67 }
68 self
69 }
70
71 /// The same kind of source beside this one, for a second table: a file source becomes
72 /// the file named `<path>.<suffix>` in the same format.
73 #[must_use]
74 pub fn sibling(&self, suffix: &str) -> Self
75 where
76 F: Clone,
77 {
78 Self {
79 file: self.file.as_ref().map(|file| FileSource {
80 path: format!("{}.{suffix}", file.path),
81 format: file.format.clone(),
82 max_bytes: file.max_bytes,
83 }),
84 }
85 }
86
87 /// The table of `T` under `section` this source names. Nothing is read yet.
88 ///
89 /// # Errors
90 ///
91 /// [`SourceError::Invalid`] when no source kind is set or the file's path is blank.
92 pub fn open<T>(&self, section: &str) -> Result<Arc<dyn Table<T>>, SourceError>
93 where
94 F: Codec + Clone + 'static,
95 T: Keyed + Clone + Send + Walk + FromWalk + 'static,
96 {
97 match &self.file {
98 Some(file) if !file.path.trim().is_empty() => {
99 let table = FileTable::new(&file.path, section, file.format.clone());
100 Ok(Arc::new(match file.max_bytes {
101 Some(max) => table.max_bytes(max as usize),
102 None => table,
103 }))
104 }
105 Some(_) => Err(SourceError::invalid("source.file.path must name the file")),
106 None => Err(SourceError::invalid("source must name a source kind: file")),
107 }
108 }
109}