mod disk;
mod git;
mod inventory;
mod memory;
mod registry;
mod sources;
pub use disk::{CapturedDirectory, CapturedFile, CapturedLink, DiskTree};
pub use git::{CommittedGitTree, PinnedGitTree};
pub use memory::MemoryTree;
pub use registry::RegistryArchiveTree;
pub use sources::SourceTrees;
use crate::error::TreeError;
use crate::meter::Meter;
use crate::path::{RelPath, SourceAuthorityId};
use std::sync::Arc;
pub(super) enum SealedMeter<'a> {
Owned(Meter),
Borrowed(&'a Meter),
}
impl std::ops::Deref for SealedMeter<'_> {
type Target = Meter;
fn deref(&self) -> &Meter {
match self {
Self::Owned(meter) => meter,
Self::Borrowed(meter) => meter,
}
}
}
fn require_first_party(path: &RelPath) -> Result<(), TreeError> {
if path.authority() == SourceAuthorityId::FIRST_PARTY {
Ok(())
} else {
Err(TreeError::WrongAuthority {
path: path.to_string(),
authority: path.authority(),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum EntryKind {
File,
Dir,
Symlink,
Other,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileId {
authority: SourceAuthorityId,
identity: Identity,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Identity {
Git { commit: [u8; 20], path: RelPath },
Device { dev: u64, ino: u64 },
Key(Arc<[u8]>),
}
impl FileId {
pub(crate) fn git(commit: [u8; 20], path: &RelPath) -> Self {
Self {
authority: SourceAuthorityId::FIRST_PARTY,
identity: Identity::Git {
commit,
path: path.clone(),
},
}
}
#[must_use]
pub fn from_device(dev: u64, ino: u64) -> Self {
Self {
authority: SourceAuthorityId::FIRST_PARTY,
identity: Identity::Device { dev, ino },
}
}
#[must_use]
pub fn from_key(key: impl AsRef<[u8]>) -> Self {
Self {
authority: SourceAuthorityId::FIRST_PARTY,
identity: Identity::Key(key.as_ref().into()),
}
}
#[must_use]
pub fn in_source(mut self, authority: SourceAuthorityId) -> Self {
self.authority = authority;
self
}
#[must_use]
pub const fn authority(&self) -> SourceAuthorityId {
self.authority
}
pub(crate) fn heap_bytes(&self) -> usize {
match &self.identity {
Identity::Git { path, .. } => crate::cost::shared_text(path.as_str().len()),
Identity::Device { .. } => 0,
Identity::Key(key) => crate::cost::shared_text(key.len()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Opened {
Seen(FileId),
Read {
id: FileId,
text: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum OpenedBytes {
Seen(FileId),
Read {
id: FileId,
bytes: Vec<u8>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub struct Entry {
pub name: String,
pub kind: EntryKind,
}
impl Entry {
#[must_use]
pub fn new(name: impl Into<String>, kind: EntryKind) -> Self {
Self {
name: name.into(),
kind,
}
}
}
pub trait Tree {
fn read(&self, path: &RelPath, max_bytes: u64) -> Result<String, TreeError>;
fn read_bytes(&self, path: &RelPath, max_bytes: u64) -> Result<Vec<u8>, TreeError> {
let _ = max_bytes;
Err(TreeError::Io {
path: path.to_string(),
source: std::io::Error::new(
std::io::ErrorKind::Unsupported,
"this tree backend reads text only; read the artifact through a disk or memory tree",
),
})
}
fn read_unseen(
&self,
path: &RelPath,
max_bytes: u64,
seen: &dyn Fn(&FileId) -> bool,
) -> Result<Opened, TreeError>;
fn identity(&self, path: &RelPath) -> Result<FileId, TreeError> {
match self.read_unseen(path, 0, &|_| true)? {
Opened::Seen(id) | Opened::Read { id, .. } => Ok(id),
}
}
fn list(&self, path: &RelPath) -> Result<Vec<Entry>, TreeError>;
fn entry(&self, path: &RelPath) -> Result<Option<EntryKind>, TreeError>;
}