Skip to content

Commit

Permalink
Relocate RepositoryHandle to the handle.rs file
Browse files Browse the repository at this point in the history
This type's implementation was awkwardly split across mod.rs and
handle.rs.

Signed-off-by: J Robert Ray <[email protected]>
  • Loading branch information
jrray committed Nov 27, 2024
1 parent 548f890 commit 3561a23
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 115 deletions.
111 changes: 110 additions & 1 deletion crates/spfs/src/storage/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,120 @@ use spfs_encoding as encoding;

use super::prelude::*;
use super::tag::TagSpecAndTagStream;
use super::{RepositoryHandle, TagNamespace, TagNamespaceBuf, TagStorageMut};
use super::{TagNamespace, TagNamespaceBuf, TagStorageMut};
use crate::graph::ObjectProto;
use crate::tracking::{self, BlobRead};
use crate::{graph, Error, Result};

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum RepositoryHandle {
FS(super::fs::FsRepository),
Tar(super::tar::TarRepository),
Rpc(super::rpc::RpcRepository),
FallbackProxy(Box<super::fallback::FallbackProxy>),
Proxy(Box<super::proxy::ProxyRepository>),
Pinned(Box<super::pinned::PinnedRepository<RepositoryHandle>>),
}

impl RepositoryHandle {
/// Pin this repository to a specific date time, limiting
/// all results to that instant and before.
///
/// If this repository is already pinned, this function
/// CAN move the pin farther into the future than it was
/// before. In other words, pinned repositories are never
/// nested via this function call.
pub fn into_pinned(self, time: DateTime<Utc>) -> Self {
match self {
RepositoryHandle::Pinned(pinned) => Self::Pinned(Box::new(
super::pinned::PinnedRepository::new(Arc::clone(pinned.inner()), time),
)),
_ => Self::Pinned(Box::new(super::pinned::PinnedRepository::new(
Arc::new(self),
time,
))),
}
}

/// Make a pinned version of this repository at a specific date time,
/// limiting all results to that instant and before.
///
/// If this repository is already pinned, this function
/// CAN move the pin farther into the future than it was
/// before. In other words, pinned repositories are never
/// nested via this function call.
pub fn to_pinned(self: &Arc<Self>, time: DateTime<Utc>) -> Self {
match &**self {
RepositoryHandle::Pinned(pinned) => Self::Pinned(Box::new(
super::pinned::PinnedRepository::new(Arc::clone(pinned.inner()), time),
)),
_ => Self::Pinned(Box::new(super::pinned::PinnedRepository::new(
Arc::clone(self),
time,
))),
}
}

pub fn try_as_tag_mut(&mut self) -> Result<&mut dyn TagStorageMut> {
match self {
RepositoryHandle::FS(repo) => Ok(repo),
RepositoryHandle::Tar(repo) => Ok(repo),
RepositoryHandle::Rpc(repo) => Ok(repo),
RepositoryHandle::FallbackProxy(repo) => Ok(&mut **repo),
RepositoryHandle::Proxy(repo) => Ok(&mut **repo),
RepositoryHandle::Pinned(_) => Err(Error::RepositoryIsPinned),
}
}
}

impl From<super::fs::FsRepository> for RepositoryHandle {
fn from(repo: super::fs::FsRepository) -> Self {
RepositoryHandle::FS(repo)
}
}

impl From<super::fs::OpenFsRepository> for RepositoryHandle {
fn from(repo: super::fs::OpenFsRepository) -> Self {
RepositoryHandle::FS(repo.into())
}
}

impl From<Arc<super::fs::OpenFsRepository>> for RepositoryHandle {
fn from(repo: Arc<super::fs::OpenFsRepository>) -> Self {
RepositoryHandle::FS(repo.into())
}
}

impl From<super::tar::TarRepository> for RepositoryHandle {
fn from(repo: super::tar::TarRepository) -> Self {
RepositoryHandle::Tar(repo)
}
}

impl From<super::rpc::RpcRepository> for RepositoryHandle {
fn from(repo: super::rpc::RpcRepository) -> Self {
RepositoryHandle::Rpc(repo)
}
}

impl From<super::fallback::FallbackProxy> for RepositoryHandle {
fn from(repo: super::fallback::FallbackProxy) -> Self {
RepositoryHandle::FallbackProxy(Box::new(repo))
}
}

impl From<super::proxy::ProxyRepository> for RepositoryHandle {
fn from(repo: super::proxy::ProxyRepository) -> Self {
RepositoryHandle::Proxy(Box::new(repo))
}
}

impl From<Box<super::pinned::PinnedRepository<RepositoryHandle>>> for RepositoryHandle {
fn from(repo: Box<super::pinned::PinnedRepository<RepositoryHandle>>) -> Self {
RepositoryHandle::Pinned(repo)
}
}
/// Runs a code block on each variant of the handle,
/// easily allowing the use of storage code without using
/// a dyn reference
Expand Down
115 changes: 1 addition & 114 deletions crates/spfs/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ pub mod proxy;
pub mod rpc;
pub mod tar;

use std::sync::Arc;

pub use address::Address;
pub use blob::BlobStorage;
use chrono::{DateTime, Utc};
pub use error::OpenRepositoryError;
pub use handle::RepositoryHandle;
pub use layer::LayerStorage;
pub use manifest::ManifestStorage;
pub use payload::PayloadStorage;
Expand All @@ -39,114 +37,3 @@ pub use tag::{EntryType, TagStorage, TagStorageMut};
pub use tag_namespace::{TagNamespace, TagNamespaceBuf, TAG_NAMESPACE_MARKER};

pub use self::config::{FromConfig, FromUrl, OpenRepositoryResult};
use crate::{Error, Result};

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum RepositoryHandle {
FS(fs::FsRepository),
Tar(tar::TarRepository),
Rpc(rpc::RpcRepository),
FallbackProxy(Box<fallback::FallbackProxy>),
Proxy(Box<proxy::ProxyRepository>),
Pinned(Box<pinned::PinnedRepository<RepositoryHandle>>),
}

impl RepositoryHandle {
/// Pin this repository to a specific date time, limiting
/// all results to that instant and before.
///
/// If this repository is already pinned, this function
/// CAN move the pin farther into the future than it was
/// before. In other words, pinned repositories are never
/// nested via this function call.
pub fn into_pinned(self, time: DateTime<Utc>) -> Self {
match self {
RepositoryHandle::Pinned(pinned) => Self::Pinned(Box::new(
pinned::PinnedRepository::new(Arc::clone(pinned.inner()), time),
)),
_ => Self::Pinned(Box::new(pinned::PinnedRepository::new(
Arc::new(self),
time,
))),
}
}

/// Make a pinned version of this repository at a specific date time,
/// limiting all results to that instant and before.
///
/// If this repository is already pinned, this function
/// CAN move the pin farther into the future than it was
/// before. In other words, pinned repositories are never
/// nested via this function call.
pub fn to_pinned(self: &Arc<Self>, time: DateTime<Utc>) -> Self {
match &**self {
RepositoryHandle::Pinned(pinned) => Self::Pinned(Box::new(
pinned::PinnedRepository::new(Arc::clone(pinned.inner()), time),
)),
_ => Self::Pinned(Box::new(pinned::PinnedRepository::new(
Arc::clone(self),
time,
))),
}
}

pub fn try_as_tag_mut(&mut self) -> Result<&mut dyn TagStorageMut> {
match self {
RepositoryHandle::FS(repo) => Ok(repo),
RepositoryHandle::Tar(repo) => Ok(repo),
RepositoryHandle::Rpc(repo) => Ok(repo),
RepositoryHandle::FallbackProxy(repo) => Ok(&mut **repo),
RepositoryHandle::Proxy(repo) => Ok(&mut **repo),
RepositoryHandle::Pinned(_) => Err(Error::RepositoryIsPinned),
}
}
}

impl From<fs::FsRepository> for RepositoryHandle {
fn from(repo: fs::FsRepository) -> Self {
RepositoryHandle::FS(repo)
}
}

impl From<fs::OpenFsRepository> for RepositoryHandle {
fn from(repo: fs::OpenFsRepository) -> Self {
RepositoryHandle::FS(repo.into())
}
}

impl From<Arc<fs::OpenFsRepository>> for RepositoryHandle {
fn from(repo: Arc<fs::OpenFsRepository>) -> Self {
RepositoryHandle::FS(repo.into())
}
}

impl From<tar::TarRepository> for RepositoryHandle {
fn from(repo: tar::TarRepository) -> Self {
RepositoryHandle::Tar(repo)
}
}

impl From<rpc::RpcRepository> for RepositoryHandle {
fn from(repo: rpc::RpcRepository) -> Self {
RepositoryHandle::Rpc(repo)
}
}

impl From<fallback::FallbackProxy> for RepositoryHandle {
fn from(repo: fallback::FallbackProxy) -> Self {
RepositoryHandle::FallbackProxy(Box::new(repo))
}
}

impl From<proxy::ProxyRepository> for RepositoryHandle {
fn from(repo: proxy::ProxyRepository) -> Self {
RepositoryHandle::Proxy(Box::new(repo))
}
}

impl From<Box<pinned::PinnedRepository<RepositoryHandle>>> for RepositoryHandle {
fn from(repo: Box<pinned::PinnedRepository<RepositoryHandle>>) -> Self {
RepositoryHandle::Pinned(repo)
}
}

0 comments on commit 3561a23

Please sign in to comment.