-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a workspace type with recipe globs
Signed-off-by: Ryan Bottriell <[email protected]>
- Loading branch information
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod spec; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |