compmd_host/store.rs
1//! The pack store's storage ([hosting], decisions 4 and 5): the releases a host fetched or keeps, by their place, each
2//! checked against its integrity before it was kept. A host gives the storage: a folder ([`crate::FolderStore`]),
3//! its own store of published versions, or none ([`NoStore`]).
4//!
5//! [hosting]: https://github.com/comp-markdown/compmd/blob/main/docs/hosting.md
6
7use compmd_pack::{Integrity, PackName, Place, ReleaseId};
8
9/// A release a store holds.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct StoredRelease {
12 /// The release.
13 pub release: ReleaseId,
14 /// The integrity the registry publishes for it (`sha512-…`), which its files were checked against.
15 pub integrity: Integrity,
16}
17
18/// Where a host keeps packs beyond the bundle.
19pub trait Store: Send + Sync {
20 /// The releases of `pack` it holds, in any order.
21 fn releases(&self, pack: &PackName) -> Vec<StoredRelease>;
22
23 /// The bytes of the file at `place`, when it holds them.
24 fn file(&self, place: &Place) -> Option<Vec<u8>>;
25}
26
27/// No store: the host serves the bundle's packs alone.
28#[derive(Clone, Copy, Debug, Default)]
29pub struct NoStore;
30
31impl Store for NoStore {
32 fn releases(&self, _pack: &PackName) -> Vec<StoredRelease> {
33 Vec::new()
34 }
35
36 fn file(&self, _place: &Place) -> Option<Vec<u8>> {
37 None
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn no_store_holds_nothing() {
47 assert_eq!(
48 NoStore.releases(&"@kit/mermaid".parse().expect("a name")),
49 []
50 );
51 assert_eq!(
52 NoStore.file(&"@kit/mermaid@1.0.0/a.js".parse().expect("a place")),
53 None
54 );
55 }
56}