From f775b5cd8d25b3a7038a47ca319c3ef5e7134fbf Mon Sep 17 00:00:00 2001 From: Ryan Bottriell Date: Fri, 13 Dec 2024 11:14:01 -0800 Subject: [PATCH] Claify the naming and purpose of the workspace file Signed-off-by: Ryan Bottriell --- crates/spk-workspace/src/error.rs | 6 ++-- crates/spk-workspace/src/{spec.rs => file.rs} | 32 +++++++++++-------- .../src/{spec_test.rs => file_test.rs} | 16 +++++----- crates/spk-workspace/src/lib.rs | 4 +-- 4 files changed, 32 insertions(+), 26 deletions(-) rename crates/spk-workspace/src/{spec.rs => file.rs} (78%) rename crates/spk-workspace/src/{spec_test.rs => file_test.rs} (71%) diff --git a/crates/spk-workspace/src/error.rs b/crates/spk-workspace/src/error.rs index 80aa8d156..8caea7ac0 100644 --- a/crates/spk-workspace/src/error.rs +++ b/crates/spk-workspace/src/error.rs @@ -1,13 +1,13 @@ use std::path::PathBuf; #[derive(thiserror::Error, miette::Diagnostic, Debug)] -pub enum LoadWorkspaceError { +pub enum LoadWorkspaceFileError { #[error( "workspace not found, no {} in {0:?} or any parent", - crate::Workspace::FILE_NAME + crate::WorkspaceFile::FILE_NAME )] WorkspaceNotFound(PathBuf), - #[error("'{}' not found in {0:?}", crate::Workspace::FILE_NAME)] + #[error("'{}' not found in {0:?}", crate::WorkspaceFile::FILE_NAME)] NoWorkspaceFile(PathBuf), #[error(transparent)] ReadFailed(std::io::Error), diff --git a/crates/spk-workspace/src/spec.rs b/crates/spk-workspace/src/file.rs similarity index 78% rename from crates/spk-workspace/src/spec.rs rename to crates/spk-workspace/src/file.rs index 1aa4f9b0d..1507b43c2 100644 --- a/crates/spk-workspace/src/spec.rs +++ b/crates/spk-workspace/src/file.rs @@ -3,36 +3,42 @@ use std::path::Path; use serde::{Deserialize, Serialize}; use spk_schema_foundation::FromYaml; -use crate::error::LoadWorkspaceError; +use crate::error::LoadWorkspaceFileError; #[cfg(test)] -#[path = "spec_test.rs"] -mod spec_test; +#[path = "file_test.rs"] +mod file_test; +/// Describes a workspace configuration. +/// +/// Contains information about the layout of the workspace, +/// and where to find data, usually loaded from a file on disk. +/// It must still be fully validated and loaded into a +/// [`super::Workspace`] to be operated on. #[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd, Deserialize, Serialize)] -pub struct Workspace { +pub struct WorkspaceFile { #[serde(default, skip_serializing_if = "Vec::is_empty", with = "glob_from_str")] pub recipes: Vec, } -impl Workspace { +impl WorkspaceFile { pub const FILE_NAME: &str = "workspace.spk.yaml"; /// Load a workspace from its root directory on disk - pub fn load>(root: P) -> Result { + pub fn load>(root: P) -> Result { let root = root .as_ref() .canonicalize() - .map_err(|_| LoadWorkspaceError::NoWorkspaceFile(root.as_ref().into()))?; + .map_err(|_| LoadWorkspaceFileError::NoWorkspaceFile(root.as_ref().into()))?; - let workspace_file = std::fs::read_to_string(root.join(Workspace::FILE_NAME)) - .map_err(LoadWorkspaceError::ReadFailed)?; - Workspace::from_yaml(workspace_file).map_err(LoadWorkspaceError::InvalidYaml) + let workspace_file = std::fs::read_to_string(root.join(WorkspaceFile::FILE_NAME)) + .map_err(LoadWorkspaceFileError::ReadFailed)?; + WorkspaceFile::from_yaml(workspace_file).map_err(LoadWorkspaceFileError::InvalidYaml) } /// Load the workspace for a given dir, looking at parent directories /// as necessary to find the workspace root - pub fn discover>(cwd: P) -> Result { + pub fn discover>(cwd: P) -> Result { let cwd = if cwd.as_ref().is_absolute() { cwd.as_ref().to_owned() } else { @@ -48,7 +54,7 @@ impl Workspace { let mut last_found = None; loop { - if candidate.join(Workspace::FILE_NAME).is_file() { + if candidate.join(WorkspaceFile::FILE_NAME).is_file() { last_found = Some(candidate.clone()); } if !candidate.pop() { @@ -57,7 +63,7 @@ impl Workspace { } match last_found { Some(path) => Self::load(path), - None => Err(LoadWorkspaceError::WorkspaceNotFound(cwd)), + None => Err(LoadWorkspaceFileError::WorkspaceNotFound(cwd)), } } } diff --git a/crates/spk-workspace/src/spec_test.rs b/crates/spk-workspace/src/file_test.rs similarity index 71% rename from crates/spk-workspace/src/spec_test.rs rename to crates/spk-workspace/src/file_test.rs index 7a075ab14..282433099 100644 --- a/crates/spk-workspace/src/spec_test.rs +++ b/crates/spk-workspace/src/file_test.rs @@ -1,6 +1,6 @@ use rstest::{fixture, rstest}; -use super::Workspace; +use super::WorkspaceFile; #[fixture] pub fn tmpdir() -> tempfile::TempDir { @@ -17,7 +17,7 @@ recipes: [] #[rstest] fn test_workspace_roundtrip() { - let workspace = Workspace { + let workspace = WorkspaceFile { recipes: vec![ glob::Pattern::new("packages/*/*.spk.yml").unwrap(), glob::Pattern::new("platforms/*/*.spk.yml").unwrap(), @@ -25,7 +25,7 @@ fn test_workspace_roundtrip() { }; let serialized = serde_json::to_string(&workspace).unwrap(); - let deserialized: Workspace = serde_json::from_str(&serialized).unwrap(); + let deserialized: WorkspaceFile = serde_json::from_str(&serialized).unwrap(); assert_eq!(workspace, deserialized); } @@ -33,14 +33,14 @@ fn test_workspace_roundtrip() { #[rstest] fn test_empty_workspace_loading(tmpdir: tempfile::TempDir) { let root = tmpdir.path(); - std::fs::write(root.join(Workspace::FILE_NAME), EMPTY_WORKSPACE).unwrap(); - let _workspace = Workspace::load(root).expect("failed to load empty workspace"); + std::fs::write(root.join(WorkspaceFile::FILE_NAME), EMPTY_WORKSPACE).unwrap(); + let _workspace = WorkspaceFile::load(root).expect("failed to load empty workspace"); } #[rstest] fn test_must_have_file(tmpdir: tempfile::TempDir) { let root = tmpdir.path(); - Workspace::load(root).expect_err("workspace should fail to load for empty dir"); + WorkspaceFile::load(root).expect_err("workspace should fail to load for empty dir"); } #[rstest] @@ -63,7 +63,7 @@ fn test_workspace_discovery( std::fs::create_dir_all(&cwd).unwrap(); std::fs::create_dir_all(&root).unwrap(); - std::fs::write(root.join(Workspace::FILE_NAME), EMPTY_WORKSPACE).unwrap(); + std::fs::write(root.join(WorkspaceFile::FILE_NAME), EMPTY_WORKSPACE).unwrap(); - Workspace::discover(&cwd).expect("failed to load workspace"); + WorkspaceFile::discover(&cwd).expect("failed to load workspace"); } diff --git a/crates/spk-workspace/src/lib.rs b/crates/spk-workspace/src/lib.rs index 30e6a8e47..0f5d2f62e 100644 --- a/crates/spk-workspace/src/lib.rs +++ b/crates/spk-workspace/src/lib.rs @@ -1,4 +1,4 @@ pub mod error; -mod spec; +mod file; -pub use spec::Workspace; +pub use file::WorkspaceFile;