use crate::{ArtifactEntry, ArtifactFormat, ArtifactId, Bytes};
use serde::{Deserialize, Serialize};
pub type HttpReply = (u16, Vec<(String, String)>, Vec<u8>);
pub type HttpBytesReply = (u16, Vec<(String, String)>, Bytes);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Cap {
Listing,
Archive,
Group,
StreamingReads,
StreamingUploads,
PathMapping,
Delete,
}
impl Cap {
pub const ALL: [Cap; 7] = [
Cap::Listing,
Cap::Archive,
Cap::Group,
Cap::StreamingReads,
Cap::StreamingUploads,
Cap::PathMapping,
Cap::Delete,
];
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Caps {
pub listing: bool,
pub archive: bool,
pub group: bool,
pub streaming_reads: bool,
pub streaming_uploads: bool,
pub path_mapping: bool,
pub delete: bool,
}
impl Caps {
pub const NONE: Caps = Caps {
listing: false,
archive: false,
group: false,
streaming_reads: false,
streaming_uploads: false,
path_mapping: false,
delete: false,
};
pub const ALL: Caps = Caps {
listing: true,
archive: true,
group: true,
streaming_reads: true,
streaming_uploads: true,
path_mapping: true,
delete: true,
};
pub const LISTING: Caps = Caps {
listing: true,
..Caps::NONE
};
pub const fn znippy(has_archive: bool) -> Caps {
Caps {
listing: true,
archive: has_archive,
..Caps::NONE
}
}
pub fn has(&self, cap: Cap) -> bool {
match cap {
Cap::Listing => self.listing,
Cap::Archive => self.archive,
Cap::Group => self.group,
Cap::StreamingReads => self.streaming_reads,
Cap::StreamingUploads => self.streaming_uploads,
Cap::PathMapping => self.path_mapping,
Cap::Delete => self.delete,
}
}
#[must_use]
pub fn with(mut self, cap: Cap, on: bool) -> Caps {
*match cap {
Cap::Listing => &mut self.listing,
Cap::Archive => &mut self.archive,
Cap::Group => &mut self.group,
Cap::StreamingReads => &mut self.streaming_reads,
Cap::StreamingUploads => &mut self.streaming_uploads,
Cap::PathMapping => &mut self.path_mapping,
Cap::Delete => &mut self.delete,
} = on;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unsupported {
pub backend: String,
pub cap: Cap,
}
impl std::fmt::Display for Unsupported {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"backend '{}' does not support {:?}",
self.backend, self.cap
)
}
}
impl std::error::Error for Unsupported {}
pub fn head_of((status, mut headers, body): HttpReply) -> HttpReply {
if !headers
.iter()
.any(|(k, _)| k.eq_ignore_ascii_case("content-length"))
{
headers.push(("Content-Length".to_string(), body.len().to_string()));
}
(status, headers, Vec::new())
}
pub fn head_of_bytes((status, mut headers, body): HttpBytesReply) -> HttpBytesReply {
if !headers
.iter()
.any(|(k, _)| k.eq_ignore_ascii_case("content-length"))
{
headers.push(("Content-Length".to_string(), body.len().to_string()));
}
(status, headers, Bytes::new())
}
pub fn unsupported<T>(backend: &str, cap: Cap) -> anyhow::Result<T> {
Err(Unsupported {
backend: backend.to_string(),
cap,
}
.into())
}
pub fn is_unsupported(e: &anyhow::Error) -> bool {
e.chain().any(|c| c.is::<Unsupported>())
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Archive {
pub name: String,
pub files: Vec<(String, u64)>,
}
impl Archive {
pub fn read(name: &str, reader: &dyn znippy_common::ZnippyReader) -> anyhow::Result<Archive> {
let files = reader
.list_files()?
.into_iter()
.map(|f| {
let size = reader.file_size(&f).unwrap_or(0);
(f, size)
})
.collect();
Ok(Archive {
name: name.to_string(),
files,
})
}
pub fn info(&self) -> ArchiveInfo {
ArchiveInfo {
file_count: self.files.len() as u64,
total_uncompressed_bytes: self.files.iter().map(|(_, s)| *s).sum(),
archive_path: self.name.clone(),
}
}
pub fn paths(self, prefix: Option<&str>) -> Vec<String> {
let mut seen = std::collections::HashSet::new();
self.files
.into_iter()
.map(|(f, _)| f)
.filter(|f| prefix.is_none_or(|p| f.starts_with(p)))
.filter(|f| seen.insert(f.clone()))
.collect()
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct ArchiveInfo {
pub file_count: u64,
pub total_uncompressed_bytes: u64,
pub archive_path: String,
}
pub struct ChunkIter {
pub len: Option<u64>,
pub pieces: Box<dyn Iterator<Item = anyhow::Result<Bytes>> + Send>,
}
impl Iterator for ChunkIter {
type Item = anyhow::Result<Bytes>;
fn next(&mut self) -> Option<Self::Item> {
self.pieces.next()
}
}
pub struct StreamedGet {
pub headers: Vec<(String, String)>,
pub body: ChunkIter,
}
pub fn file_chunks(file: std::fs::File, chunk_bytes: usize) -> ChunkIter {
let len = file.metadata().ok().map(|m| m.len());
ChunkIter {
len,
pieces: Box::new(FileChunks {
file,
chunk: chunk_bytes.max(1),
left: len,
done: false,
}),
}
}
struct FileChunks {
file: std::fs::File,
chunk: usize,
left: Option<u64>,
done: bool,
}
impl Iterator for FileChunks {
type Item = anyhow::Result<Bytes>;
fn next(&mut self) -> Option<Self::Item> {
use std::io::Read as _;
if self.done {
return None;
}
let size = match self.left {
Some(left) => (left.min(self.chunk as u64) as usize).max(1),
None => self.chunk,
};
let mut buf = vec![0u8; size];
let mut filled = 0usize;
while filled < buf.len() {
match self.file.read(&mut buf[filled..]) {
Ok(0) => break,
Ok(n) => filled += n,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) => {
self.done = true;
return Some(Err(e.into()));
}
}
}
if filled == 0 {
self.done = true;
return None;
}
if filled < buf.len() {
self.done = true;
buf.truncate(filled);
}
if let Some(left) = self.left.as_mut() {
*left = left.saturating_sub(filled as u64);
}
Some(Ok(Bytes::from(buf)))
}
}
pub fn chunks_of(whole: Bytes, chunk_bytes: usize) -> ChunkIter {
let step = chunk_bytes.max(1);
let len = whole.len();
ChunkIter {
len: Some(len as u64),
pieces: Box::new((0..len).step_by(step).map(move |start| {
let end = (start + step).min(len);
Ok(whole.slice(start..end))
})),
}
}
pub const READER_PREALLOC_CAP: u64 = 64 * 1024 * 1024;
pub fn collect_body(
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<Vec<u8>> {
let mut buf = Vec::with_capacity(size_hint.unwrap_or(0).min(READER_PREALLOC_CAP) as usize);
body.read_to_end(&mut buf)?;
Ok(buf)
}
pub trait RepositoryBackendTrait: Send + Sync {
fn name(&self) -> &str;
fn format(&self) -> ArtifactFormat;
fn is_writable(&self) -> bool;
fn fetch(&self, id: &ArtifactId) -> anyhow::Result<Option<Bytes>>;
fn put(&self, id: &ArtifactId, data: &[u8]) -> anyhow::Result<()>;
fn handle_http2_request(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> anyhow::Result<HttpReply>;
fn handle_http2_request_bytes(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> anyhow::Result<HttpBytesReply> {
self.handle_http2_request(method, suburl, body)
.map(|(s, h, b)| (s, h, Bytes::from(b)))
}
fn read_http2_bytes(&self, head: bool, suburl: &str) -> anyhow::Result<HttpBytesReply> {
self.handle_http2_request(if head { "HEAD" } else { "GET" }, suburl, &[])
.map(|(s, h, b)| (s, h, Bytes::from(b)))
}
fn caps(&self) -> Caps;
fn list(&self, name_filter: Option<&str>, limit: usize) -> anyhow::Result<Vec<ArtifactEntry>> {
let _ = (name_filter, limit);
unsupported(self.name(), Cap::Listing)
}
fn archive(&self) -> anyhow::Result<Archive> {
unsupported(self.name(), Cap::Archive)
}
fn group_members(&self) -> anyhow::Result<Vec<String>> {
unsupported(self.name(), Cap::Group)
}
fn fetch_chunks(
&self,
id: &ArtifactId,
chunk_bytes: usize,
) -> anyhow::Result<Option<ChunkIter>> {
let _ = (id, chunk_bytes);
unsupported(self.name(), Cap::StreamingReads)
}
fn stream_get(&self, suburl: &str, piece_bytes: usize) -> anyhow::Result<Option<StreamedGet>> {
let _ = (suburl, piece_bytes);
Ok(None)
}
fn put_from_reader(
&self,
id: &ArtifactId,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<()> {
let _ = (id, body, size_hint);
unsupported(self.name(), Cap::StreamingUploads)
}
fn handle_http2_upload(
&self,
method: &str,
suburl: &str,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<Option<HttpReply>> {
let _ = (method, suburl, body, size_hint);
unsupported(self.name(), Cap::StreamingUploads)
}
fn coordinate_for_path(&self, suburl: &str) -> anyhow::Result<Option<ArtifactId>> {
let _ = suburl;
unsupported(self.name(), Cap::PathMapping)
}
fn delete_artifact(&self, id: &ArtifactId, expected_content_id: &str) -> anyhow::Result<()> {
let _ = (id, expected_content_id);
unsupported(self.name(), Cap::Delete)
}
}
impl dyn RepositoryBackendTrait + '_ {
pub fn read_chunks(
&self,
id: &ArtifactId,
chunk_bytes: usize,
) -> anyhow::Result<Option<ChunkIter>> {
if self.caps().streaming_reads {
return self.fetch_chunks(id, chunk_bytes);
}
Ok(self.fetch(id)?.map(|whole| chunks_of(whole, chunk_bytes)))
}
pub fn write_from_reader(
&self,
id: &ArtifactId,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<()> {
if self.caps().streaming_uploads {
return self.put_from_reader(id, body, size_hint);
}
let buf = collect_body(body, size_hint)?;
self.put(id, &buf)
}
pub fn coordinate(&self, suburl: &str) -> Option<ArtifactId> {
if !self.caps().path_mapping {
return None;
}
self.coordinate_for_path(suburl).ok().flatten()
}
pub fn members(&self) -> Vec<String> {
if !self.caps().group {
return Vec::new();
}
self.group_members().unwrap_or_default()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Access {
Read,
Write,
}
impl Access {
pub fn of_method(method: &str) -> Access {
if matches!(method, "GET" | "HEAD") {
Access::Read
} else {
Access::Write
}
}
}
pub trait Decorator: Send + Sync {
fn name(&self) -> &str;
fn format(&self) -> ArtifactFormat;
fn is_writable(&self) -> bool;
fn caps(&self) -> Caps;
fn around<T>(
&self,
access: Access,
call: impl FnOnce(&dyn RepositoryBackendTrait) -> anyhow::Result<T>,
) -> anyhow::Result<T>;
fn fetch(&self, id: &ArtifactId) -> anyhow::Result<Option<Bytes>> {
self.around(Access::Read, |b| b.fetch(id))
}
fn put(&self, id: &ArtifactId, data: &[u8]) -> anyhow::Result<()> {
self.around(Access::Write, |b| b.put(id, data))
}
fn handle_http2_request(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> anyhow::Result<HttpReply> {
self.around(Access::of_method(method), |b| {
b.handle_http2_request(method, suburl, body)
})
}
fn handle_http2_request_bytes(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> anyhow::Result<HttpBytesReply> {
self.around(Access::of_method(method), |b| {
b.handle_http2_request_bytes(method, suburl, body)
})
}
fn read_http2_bytes(&self, head: bool, suburl: &str) -> anyhow::Result<HttpBytesReply> {
self.around(Access::Read, |b| b.read_http2_bytes(head, suburl))
}
fn list(&self, name_filter: Option<&str>, limit: usize) -> anyhow::Result<Vec<ArtifactEntry>> {
self.around(Access::Read, |b| b.list(name_filter, limit))
}
fn archive(&self) -> anyhow::Result<Archive> {
self.around(Access::Read, |b| b.archive())
}
fn group_members(&self) -> anyhow::Result<Vec<String>> {
self.around(Access::Read, |b| b.group_members())
}
fn fetch_chunks(
&self,
id: &ArtifactId,
chunk_bytes: usize,
) -> anyhow::Result<Option<ChunkIter>> {
self.around(Access::Read, |b| b.fetch_chunks(id, chunk_bytes))
}
fn put_from_reader(
&self,
id: &ArtifactId,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<()> {
self.around(Access::Write, |b| b.put_from_reader(id, body, size_hint))
}
fn handle_http2_upload(
&self,
method: &str,
suburl: &str,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<Option<HttpReply>> {
self.around(Access::Write, |b| {
b.handle_http2_upload(method, suburl, body, size_hint)
})
}
fn coordinate_for_path(&self, suburl: &str) -> anyhow::Result<Option<ArtifactId>> {
self.around(Access::Read, |b| b.coordinate_for_path(suburl))
}
fn stream_get(&self, suburl: &str, piece_bytes: usize) -> anyhow::Result<Option<StreamedGet>> {
let _ = (suburl, piece_bytes);
Ok(None)
}
fn delete_artifact(&self, id: &ArtifactId, expected_content_id: &str) -> anyhow::Result<()> {
self.around(Access::Write, |b| {
b.delete_artifact(id, expected_content_id)
})
}
}
impl<D: Decorator> RepositoryBackendTrait for D {
fn name(&self) -> &str {
Decorator::name(self)
}
fn format(&self) -> ArtifactFormat {
Decorator::format(self)
}
fn is_writable(&self) -> bool {
Decorator::is_writable(self)
}
fn caps(&self) -> Caps {
Decorator::caps(self)
}
fn fetch(&self, id: &ArtifactId) -> anyhow::Result<Option<Bytes>> {
Decorator::fetch(self, id)
}
fn put(&self, id: &ArtifactId, data: &[u8]) -> anyhow::Result<()> {
Decorator::put(self, id, data)
}
fn handle_http2_request(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> anyhow::Result<HttpReply> {
Decorator::handle_http2_request(self, method, suburl, body)
}
fn handle_http2_request_bytes(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> anyhow::Result<HttpBytesReply> {
Decorator::handle_http2_request_bytes(self, method, suburl, body)
}
fn read_http2_bytes(&self, head: bool, suburl: &str) -> anyhow::Result<HttpBytesReply> {
Decorator::read_http2_bytes(self, head, suburl)
}
fn list(&self, name_filter: Option<&str>, limit: usize) -> anyhow::Result<Vec<ArtifactEntry>> {
Decorator::list(self, name_filter, limit)
}
fn archive(&self) -> anyhow::Result<Archive> {
Decorator::archive(self)
}
fn group_members(&self) -> anyhow::Result<Vec<String>> {
Decorator::group_members(self)
}
fn fetch_chunks(
&self,
id: &ArtifactId,
chunk_bytes: usize,
) -> anyhow::Result<Option<ChunkIter>> {
Decorator::fetch_chunks(self, id, chunk_bytes)
}
fn put_from_reader(
&self,
id: &ArtifactId,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<()> {
Decorator::put_from_reader(self, id, body, size_hint)
}
fn handle_http2_upload(
&self,
method: &str,
suburl: &str,
body: &mut dyn std::io::Read,
size_hint: Option<u64>,
) -> anyhow::Result<Option<HttpReply>> {
Decorator::handle_http2_upload(self, method, suburl, body, size_hint)
}
fn coordinate_for_path(&self, suburl: &str) -> anyhow::Result<Option<ArtifactId>> {
Decorator::coordinate_for_path(self, suburl)
}
fn stream_get(&self, suburl: &str, piece_bytes: usize) -> anyhow::Result<Option<StreamedGet>> {
Decorator::stream_get(self, suburl, piece_bytes)
}
fn delete_artifact(&self, id: &ArtifactId, expected_content_id: &str) -> anyhow::Result<()> {
Decorator::delete_artifact(self, id, expected_content_id)
}
}
pub mod conformance {
use super::*;
pub fn probe_id() -> ArtifactId {
ArtifactId {
namespace: None,
name: "holger-caps-probe".into(),
version: "0".into(),
}
}
struct Refused;
impl std::io::Read for Refused {
fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
Err(std::io::Error::other("conformance probe: refused"))
}
}
pub fn unsupported_answers(b: &dyn RepositoryBackendTrait, cap: Cap) -> Vec<bool> {
let id = probe_id();
let suburl = format!("/{}/holger-caps-probe", b.name());
let un = |r: anyhow::Result<()>| r.err().is_some_and(|e| is_unsupported(&e));
match cap {
Cap::Listing => vec![un(b.list(None, 1).map(drop))],
Cap::Archive => vec![un(b.archive().map(drop))],
Cap::Group => vec![un(b.group_members().map(drop))],
Cap::StreamingReads => vec![un(b.fetch_chunks(&id, 1).map(drop))],
Cap::StreamingUploads => vec![
un(b.put_from_reader(&id, &mut Refused, Some(1))),
un(b.handle_http2_upload("PUT", &suburl, &mut Refused, Some(1))
.map(drop)),
],
Cap::PathMapping => vec![un(b.coordinate_for_path(&suburl).map(drop))],
Cap::Delete => vec![un(b.delete_artifact(&id, &"0".repeat(64)))],
}
}
pub fn check(b: &dyn RepositoryBackendTrait) -> Result<(), String> {
let caps = b.caps();
let bad: Vec<String> = Cap::ALL
.into_iter()
.filter_map(|cap| {
let answers = unsupported_answers(b, cap);
match (
caps.has(cap),
answers.iter().any(|u| *u),
answers.iter().all(|u| *u),
) {
(true, true, _) => Some(format!(
"{cap:?} is claimed but its method answers Unsupported"
)),
(false, _, false) => {
Some(format!("{cap:?} is not claimed but its method answers"))
}
_ => None,
}
})
.collect();
if bad.is_empty() {
Ok(())
} else {
Err(format!("backend '{}': {}", b.name(), bad.join("; ")))
}
}
pub struct Everything {
pub name: String,
pub caps: Caps,
}
impl Everything {
pub fn new(name: &str) -> Everything {
Everything {
name: name.to_string(),
caps: Caps::ALL,
}
}
pub fn core_only(name: &str) -> CoreOnly {
CoreOnly {
name: name.to_string(),
}
}
}
impl RepositoryBackendTrait for Everything {
fn name(&self) -> &str {
&self.name
}
fn format(&self) -> ArtifactFormat {
ArtifactFormat::Raw
}
fn is_writable(&self) -> bool {
true
}
fn fetch(&self, _: &ArtifactId) -> anyhow::Result<Option<Bytes>> {
Ok(None)
}
fn put(&self, _: &ArtifactId, _: &[u8]) -> anyhow::Result<()> {
Ok(())
}
fn handle_http2_request(&self, _: &str, _: &str, _: &[u8]) -> anyhow::Result<HttpReply> {
Ok((404, Vec::new(), Vec::new()))
}
fn caps(&self) -> Caps {
self.caps
}
fn list(&self, _: Option<&str>, _: usize) -> anyhow::Result<Vec<ArtifactEntry>> {
Ok(Vec::new())
}
fn archive(&self) -> anyhow::Result<Archive> {
Ok(Archive {
name: self.name.clone(),
files: Vec::new(),
})
}
fn group_members(&self) -> anyhow::Result<Vec<String>> {
Ok(Vec::new())
}
fn fetch_chunks(&self, _: &ArtifactId, _: usize) -> anyhow::Result<Option<ChunkIter>> {
Ok(None)
}
fn put_from_reader(
&self,
_: &ArtifactId,
body: &mut dyn std::io::Read,
_: Option<u64>,
) -> anyhow::Result<()> {
let mut sink = Vec::new();
body.read_to_end(&mut sink)?;
Ok(())
}
fn handle_http2_upload(
&self,
_: &str,
_: &str,
_: &mut dyn std::io::Read,
_: Option<u64>,
) -> anyhow::Result<Option<HttpReply>> {
Ok(None)
}
fn coordinate_for_path(&self, _: &str) -> anyhow::Result<Option<ArtifactId>> {
Ok(None)
}
fn delete_artifact(&self, _: &ArtifactId, _: &str) -> anyhow::Result<()> {
anyhow::bail!("not found")
}
}
pub struct CoreOnly {
pub name: String,
}
impl RepositoryBackendTrait for CoreOnly {
fn name(&self) -> &str {
&self.name
}
fn format(&self) -> ArtifactFormat {
ArtifactFormat::Raw
}
fn is_writable(&self) -> bool {
false
}
fn fetch(&self, _: &ArtifactId) -> anyhow::Result<Option<Bytes>> {
Ok(None)
}
fn put(&self, _: &ArtifactId, _: &[u8]) -> anyhow::Result<()> {
anyhow::bail!("read-only")
}
fn handle_http2_request(&self, _: &str, _: &str, _: &[u8]) -> anyhow::Result<HttpReply> {
Ok((404, Vec::new(), Vec::new()))
}
fn caps(&self) -> Caps {
Caps::NONE
}
}
}
#[cfg(test)]
mod tests {
use super::conformance::*;
use super::*;
struct Pass(Box<dyn RepositoryBackendTrait>);
impl Decorator for Pass {
fn name(&self) -> &str {
self.0.name()
}
fn format(&self) -> ArtifactFormat {
self.0.format()
}
fn is_writable(&self) -> bool {
self.0.is_writable()
}
fn caps(&self) -> Caps {
self.0.caps()
}
fn around<T>(
&self,
_: Access,
call: impl FnOnce(&dyn RepositoryBackendTrait) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
call(self.0.as_ref())
}
}
#[test]
fn the_reference_backends_are_honest() {
check(&Everything::new("all")).unwrap();
check(&Everything::core_only("core")).unwrap();
}
#[test]
fn a_decorator_forwards_every_method_by_construction() {
check(&Pass(Box::new(Everything::new("all")))).unwrap();
check(&Pass(Box::new(Everything::core_only("core")))).unwrap();
}
#[test]
fn a_cap_without_its_method_and_a_method_without_its_cap_both_fail() {
let liar = Everything::core_only("liar");
struct Claims(CoreOnly);
impl Decorator for Claims {
fn name(&self) -> &str {
&self.0.name
}
fn format(&self) -> ArtifactFormat {
ArtifactFormat::Raw
}
fn is_writable(&self) -> bool {
false
}
fn caps(&self) -> Caps {
Caps::LISTING
}
fn around<T>(
&self,
_: Access,
call: impl FnOnce(&dyn RepositoryBackendTrait) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
call(&self.0)
}
}
let e = check(&Claims(liar)).unwrap_err();
assert!(e.contains("Listing is claimed"), "{e}");
let hidden = Everything {
name: "hidden".into(),
caps: Caps::NONE,
};
let e = check(&hidden).unwrap_err();
assert!(e.contains("Delete is not claimed"), "{e}");
}
#[test]
fn the_dyn_helpers_fall_back_honestly() {
let core: Box<dyn RepositoryBackendTrait> = Box::new(Everything::core_only("core"));
assert!(core.read_chunks(&probe_id(), 4).unwrap().is_none());
assert!(
core.write_from_reader(&probe_id(), &mut &b"x"[..], None)
.is_err(),
"read-only put"
);
assert_eq!(core.coordinate("/core/x"), None);
assert!(core.members().is_empty());
assert!(is_unsupported(&core.list(None, 0).unwrap_err()));
}
#[test]
fn an_archive_is_one_answer_for_paths_and_stats() {
let a = Archive {
name: "g".into(),
files: vec![("a/1".into(), 3), ("b/2".into(), 4), ("a/1".into(), 3)],
};
assert_eq!(
a.info(),
ArchiveInfo {
file_count: 3,
total_uncompressed_bytes: 10,
archive_path: "g".into()
}
);
assert_eq!(
a.clone().paths(None),
vec!["a/1".to_string(), "b/2".to_string()]
);
assert_eq!(a.paths(Some("b")), vec!["b/2".to_string()]);
}
}