Skip to content

Commit

Permalink
Refactor rendering logic into separate methods
Browse files Browse the repository at this point in the history
This change was prompted by wanting to refactor the code to work towards
having different trait bounds for the functions that need a repository
that supports renders (e.g., rendering into a repository) versus
functions that render into a plain directory.

Rework `RenderType` so the method that renders into a hardlink doesn't
need to worry about getting a `RenderType::Copy` value.

Signed-off-by: J Robert Ray <[email protected]>
  • Loading branch information
jrray committed Dec 10, 2024
1 parent d08ddba commit 51a8e45
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 353 deletions.
12 changes: 7 additions & 5 deletions crates/spfs-cli/cmd-render/src/cmd_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ pub struct CmdRender {
/// using a local directory and `HardLink` for the repository.
#[clap(
long,
value_parser = clap::builder::PossibleValuesParser::new(spfs::storage::fs::RenderType::VARIANTS)
.map(|s| s.parse::<spfs::storage::fs::RenderType>().unwrap())
value_parser = clap::builder::PossibleValuesParser::new(spfs::storage::fs::CliRenderType::VARIANTS)
.map(|s| s.parse::<spfs::storage::fs::CliRenderType>().unwrap())
)]
strategy: Option<spfs::storage::fs::RenderType>,
strategy: Option<spfs::storage::fs::CliRenderType>,

/// The tag or digest of what to render, use a '+' to join multiple layers
reference: String,
Expand Down Expand Up @@ -121,7 +121,9 @@ impl CmdRender {
.render_into_directory(
env_spec,
&target_dir,
self.strategy.unwrap_or(spfs::storage::fs::RenderType::Copy),
self.strategy
.map(Into::into)
.unwrap_or(spfs::storage::fs::RenderType::Copy),
)
.await?;
Ok(RenderResult {
Expand Down Expand Up @@ -165,7 +167,7 @@ impl CmdRender {
.collect();
tracing::trace!("stack: {:?}", stack);
renderer
.render(&stack, self.strategy)
.render(&stack, self.strategy.map(Into::into))
.await
.map(|paths_rendered| RenderResult {
paths_rendered,
Expand Down
4 changes: 2 additions & 2 deletions crates/spfs/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
use super::config::get_config;
use crate::prelude::*;
use crate::storage::fallback::FallbackProxy;
use crate::storage::fs::{ManifestRenderPath, RenderSummary};
use crate::storage::fs::{CliRenderType, ManifestRenderPath, RenderSummary};
use crate::{graph, runtime, storage, tracking, Error, Result};

#[cfg(test)]
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn render_via_subcommand(
// of overlayfs. To avoid any issues editing files and
// hardlinks the rendering for them switches to Copy.
cmd.arg("--strategy");
cmd.arg::<&str>(crate::storage::fs::RenderType::Copy.into());
cmd.arg::<&str>(CliRenderType::Copy.into());
}
cmd.arg(spec.to_string());
tracing::debug!("{:?}", cmd);
Expand Down
2 changes: 2 additions & 0 deletions crates/spfs/src/storage/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub use render_reporter::{
};
pub use render_summary::{RenderSummary, RenderSummaryReporter};
pub use renderer::{
CliRenderType,
HardLinkRenderType,
RenderType,
Renderer,
DEFAULT_MAX_CONCURRENT_BLOBS,
Expand Down
30 changes: 28 additions & 2 deletions crates/spfs/src/storage/fs/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,39 @@ pub const DEFAULT_MAX_CONCURRENT_BLOBS: usize = 100;
/// See: [`Renderer::with_max_concurrent_branches`]
pub const DEFAULT_MAX_CONCURRENT_BRANCHES: usize = 5;

/// Render type options available to command line commands.
#[derive(Debug, Copy, Clone, strum::EnumString, strum::VariantNames, strum::IntoStaticStr)]
pub enum RenderType {
pub enum CliRenderType {
HardLink,
HardLinkNoProxy,
Copy,
}

#[derive(Debug, Default, Copy, Clone)]
pub enum HardLinkRenderType {
#[default]
WithProxy,
WithoutProxy,
}

#[derive(Debug, Copy, Clone)]
pub enum RenderType {
HardLink(HardLinkRenderType),
Copy,
}

impl From<CliRenderType> for RenderType {
fn from(cli_render_type: CliRenderType) -> Self {
match cli_render_type {
CliRenderType::HardLink => RenderType::HardLink(HardLinkRenderType::WithProxy),
CliRenderType::HardLinkNoProxy => {
RenderType::HardLink(HardLinkRenderType::WithoutProxy)
}
CliRenderType::Copy => RenderType::Copy,
}
}
}

impl OpenFsRepository {
fn get_render_storage(&self) -> Result<&crate::storage::fs::FsHashStore> {
match &self.fs_impl.renders {
Expand Down Expand Up @@ -338,7 +364,7 @@ where
self.render_manifest_into_dir(
manifest,
&working_dir,
render_type.unwrap_or(RenderType::HardLink),
render_type.unwrap_or(RenderType::HardLink(HardLinkRenderType::default())),
)
.await
.map_err(|err| {
Expand Down
Loading

0 comments on commit 51a8e45

Please sign in to comment.