From 3561a233d7904da29c4454fb74c98e382ea276c6 Mon Sep 17 00:00:00 2001 From: J Robert Ray Date: Wed, 27 Nov 2024 10:45:55 -0800 Subject: [PATCH] Relocate RepositoryHandle to the handle.rs file This type's implementation was awkwardly split across mod.rs and handle.rs. Signed-off-by: J Robert Ray --- crates/spfs/src/storage/handle.rs | 111 +++++++++++++++++++++++++++- crates/spfs/src/storage/mod.rs | 115 +----------------------------- 2 files changed, 111 insertions(+), 115 deletions(-) diff --git a/crates/spfs/src/storage/handle.rs b/crates/spfs/src/storage/handle.rs index f588f1adb..0ce5d8a37 100644 --- a/crates/spfs/src/storage/handle.rs +++ b/crates/spfs/src/storage/handle.rs @@ -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), + Proxy(Box), + Pinned(Box>), +} + +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) -> 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, time: DateTime) -> 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 for RepositoryHandle { + fn from(repo: super::fs::FsRepository) -> Self { + RepositoryHandle::FS(repo) + } +} + +impl From for RepositoryHandle { + fn from(repo: super::fs::OpenFsRepository) -> Self { + RepositoryHandle::FS(repo.into()) + } +} + +impl From> for RepositoryHandle { + fn from(repo: Arc) -> Self { + RepositoryHandle::FS(repo.into()) + } +} + +impl From for RepositoryHandle { + fn from(repo: super::tar::TarRepository) -> Self { + RepositoryHandle::Tar(repo) + } +} + +impl From for RepositoryHandle { + fn from(repo: super::rpc::RpcRepository) -> Self { + RepositoryHandle::Rpc(repo) + } +} + +impl From for RepositoryHandle { + fn from(repo: super::fallback::FallbackProxy) -> Self { + RepositoryHandle::FallbackProxy(Box::new(repo)) + } +} + +impl From for RepositoryHandle { + fn from(repo: super::proxy::ProxyRepository) -> Self { + RepositoryHandle::Proxy(Box::new(repo)) + } +} + +impl From>> for RepositoryHandle { + fn from(repo: Box>) -> 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 diff --git a/crates/spfs/src/storage/mod.rs b/crates/spfs/src/storage/mod.rs index 673ad6d35..c346aa4f1 100644 --- a/crates/spfs/src/storage/mod.rs +++ b/crates/spfs/src/storage/mod.rs @@ -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; @@ -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), - Proxy(Box), - Pinned(Box>), -} - -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) -> 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, time: DateTime) -> 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 for RepositoryHandle { - fn from(repo: fs::FsRepository) -> Self { - RepositoryHandle::FS(repo) - } -} - -impl From for RepositoryHandle { - fn from(repo: fs::OpenFsRepository) -> Self { - RepositoryHandle::FS(repo.into()) - } -} - -impl From> for RepositoryHandle { - fn from(repo: Arc) -> Self { - RepositoryHandle::FS(repo.into()) - } -} - -impl From for RepositoryHandle { - fn from(repo: tar::TarRepository) -> Self { - RepositoryHandle::Tar(repo) - } -} - -impl From for RepositoryHandle { - fn from(repo: rpc::RpcRepository) -> Self { - RepositoryHandle::Rpc(repo) - } -} - -impl From for RepositoryHandle { - fn from(repo: fallback::FallbackProxy) -> Self { - RepositoryHandle::FallbackProxy(Box::new(repo)) - } -} - -impl From for RepositoryHandle { - fn from(repo: proxy::ProxyRepository) -> Self { - RepositoryHandle::Proxy(Box::new(repo)) - } -} - -impl From>> for RepositoryHandle { - fn from(repo: Box>) -> Self { - RepositoryHandle::Pinned(repo) - } -}