Skip to content

Commit

Permalink
Introduce a workspace type with recipe globs
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Bottriell <[email protected]>
  • Loading branch information
rydrman committed Jan 14, 2025
1 parent e211a07 commit 76c6159
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"crates/spk-solve",
"crates/spk-solve/crates/*",
"crates/spk-storage",
"crates/spk-workspace",
"crates/spk",
]
resolver = "2"
Expand Down
25 changes: 25 additions & 0 deletions crates/spk-workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "spk-workspace"
authors = { workspace = true }
edition = { workspace = true }
version = { workspace = true }
license-file = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }
readme = { workspace = true }
description = { workspace = true }

[lints]
workspace = true

[features]
sentry = ["spk-solve/sentry"]

[dependencies]
serde = { workspace = true, features = ["derive"] }
glob = { workspace = true }
spk-solve = { workspace = true }

[dev-dependencies]
rstest = { workspace = true }
serde_json = { workspace = true }
1 change: 1 addition & 0 deletions crates/spk-workspace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod spec;
52 changes: 52 additions & 0 deletions crates/spk-workspace/src/spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use serde::{Deserialize, Serialize};

#[cfg(test)]
#[path = "spec_test.rs"]
mod spec_test;

#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd, Deserialize, Serialize)]
pub struct Workspace {
#[serde(default, skip_serializing_if = "Vec::is_empty", with = "glob_from_str")]
pub recipes: Vec<glob::Pattern>,
}

mod glob_from_str {
use serde::{Deserializer, Serialize, Serializer};

pub fn serialize<S>(patterns: &Vec<glob::Pattern>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let patterns: Vec<_> = patterns.iter().map(|p| p.as_str()).collect();
patterns.serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<glob::Pattern>, D::Error>
where
D: Deserializer<'de>,
{
/// Visits a serialized string, decoding it as a digest
struct PatternVisitor;

impl<'de> serde::de::Visitor<'de> for PatternVisitor {
type Value = Vec<glob::Pattern>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a glob pattern")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut patterns = Vec::with_capacity(seq.size_hint().unwrap_or(0));
while let Some(pattern) = seq.next_element()? {
let pattern = glob::Pattern::new(pattern).map_err(serde::de::Error::custom)?;
patterns.push(pattern);
}
Ok(patterns)
}
}
deserializer.deserialize_seq(PatternVisitor)
}
}
18 changes: 18 additions & 0 deletions crates/spk-workspace/src/spec_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use rstest::rstest;

use super::Workspace;

#[rstest]
fn test_workspace_roundtrip() {
let workspace = Workspace {
recipes: vec![
glob::Pattern::new("packages/*/*.spk.yml").unwrap(),
glob::Pattern::new("platforms/*/*.spk.yml").unwrap(),
],
};

let serialized = serde_json::to_string(&workspace).unwrap();
let deserialized: Workspace = serde_json::from_str(&serialized).unwrap();

assert_eq!(workspace, deserialized);
}

0 comments on commit 76c6159

Please sign in to comment.