Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Platform V1 Spec #1187

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions crates/parsedbuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ macro_rules! parsed {
}
}

impl std::convert::AsRef<str> for $type_name {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl std::convert::AsRef<str> for $owned_type_name {
fn as_ref(&self) -> &str {
&self.0
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-build/src/build/binary_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ async fn test_build_var_pinning() {
Request::Var(r) => assert_eq!(r.value.as_pinned(), Some("topvalue")),
_ => panic!("expected var request"),
}
let depreq = spec.runtime_requirements().get(1).unwrap().clone();
let depreq = spec.runtime_requirements()[1].clone();
match depreq {
Request::Var(r) => assert_eq!(r.value.as_pinned(), Some("depvalue")),
_ => panic!("expected var request"),
Expand Down
4 changes: 2 additions & 2 deletions crates/spk-schema/crates/foundation/src/spec_ops/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::name::PkgName;

/// Some item that has an associated package name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Some item that has an associated package name
/// Some item that has an associated name

#[enum_dispatch::enum_dispatch]
pub trait Named {
pub trait Named<N: AsRef<str> + ?Sized = PkgName> {
/// The name of the associated package
fn name(&self) -> &PkgName;
fn name(&self) -> &N;
}

impl<T: Named> Named for Arc<T> {
Expand Down
19 changes: 19 additions & 0 deletions crates/spk-schema/crates/foundation/src/version_range/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,25 @@ impl FromStr for VersionFilter {
}
}

impl<'de> serde::de::Deserialize<'de> for VersionFilter {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw = String::deserialize(deserializer)?;
Self::from_str(&raw).map_err(serde::de::Error::custom)
}
}

impl serde::ser::Serialize for VersionFilter {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTreeSet::from_iter([">1,<2", "=3"]) wouldn't survive the round trip without getting changed into BTreeSet::from_iter([">1,<2,=3"]). Probably doesn't matter?

It would be great if VersionFilter wasn't recursively defined.

}
}

pub fn parse_version_range<S: AsRef<str>>(range: S) -> Result<VersionRange> {
let mut filter = VersionFilter::from_str(range.as_ref())?;

Expand Down
58 changes: 44 additions & 14 deletions crates/spk-schema/crates/ident/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,20 @@ pub enum Request {
Var(VarRequest<PinnableValue>),
}

impl Request {
/// Return the canonical name of this request."""
pub fn name(&self) -> &OptName {
impl spk_schema_foundation::spec_ops::Named<OptName> for Request {
fn name(&self) -> &OptName {
match self {
Request::Var(r) => &r.var,
Request::Pkg(r) => r.pkg.name.as_opt_name(),
}
}
}

impl Request {
/// Return the canonical name of this request.
pub fn name(&self) -> &OptName {
spk_schema_foundation::spec_ops::Named::name(self)
}
Comment on lines +196 to +199
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to keep this?


pub fn is_pkg(&self) -> bool {
matches!(self, Self::Pkg(_))
Expand Down Expand Up @@ -1129,11 +1135,26 @@ pub fn is_false(value: &bool) -> bool {
/// A deserializable name and optional value where
/// the value it identified by its position following
/// a forward slash (eg: `/<value>`)
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct NameAndValue<Name = OptNameBuf>(pub Name, pub Option<String>)
where
Name: FromStr,
<Name as FromStr>::Err: std::fmt::Display;

impl<Name> std::str::FromStr for NameAndValue<Name>
where
Name: FromStr,
<Name as FromStr>::Err: std::fmt::Display,
{
type Err = Name::Err;

fn from_str(v: &str) -> std::result::Result<Self, Self::Err> {
let mut parts = v.splitn(2, '/');
let name = parts.next().unwrap().parse()?;
Ok(Self(name, parts.next().map(String::from)))
}
}

impl<'de, Name> Deserialize<'de> for NameAndValue<Name>
where
Name: FromStr,
Expand All @@ -1153,7 +1174,7 @@ where
Name: FromStr,
<Name as FromStr>::Err: std::fmt::Display,
{
type Value = (Name, Option<String>);
type Value = NameAndValue<Name>;

fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a var name an optional value (eg, `my-var`, `my-var/value`)")
Expand All @@ -1163,19 +1184,28 @@ where
where
E: serde::de::Error,
{
let mut parts = v.splitn(2, '/');
let name = parts
.next()
.unwrap()
.parse()
.map_err(serde::de::Error::custom)?;
Ok((name, parts.next().map(String::from)))
NameAndValue::from_str(v).map_err(serde::de::Error::custom)
}
}

deserializer
.deserialize_str(NameAndValueVisitor::<Name>(PhantomData))
.map(|(n, v)| NameAndValue(n, v))
deserializer.deserialize_str(NameAndValueVisitor::<Name>(PhantomData))
}
}

impl<Name> serde::ser::Serialize for NameAndValue<Name>
where
Name: FromStr + std::fmt::Display,
<Name as FromStr>::Err: std::fmt::Display,
{
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let out = match &self.1 {
Some(v) => format!("{}/{v}", self.0),
None => self.0.to_string(),
};
serializer.serialize_str(&out)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/spk-schema/src/build_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AutoHostVars {
distro_name = distro.clone();
match OptName::new(&distro_name) {
Ok(name) => {
let mut var_opt = VarOpt::new(name.to_owned())?;
let mut var_opt = VarOpt::new(name)?;
// Look for any configured compat rules for the
// distro
let config = get_config()?;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl AutoHostVars {
"No distro name set by host. A {}= will be used instead.",
OptName::unknown_distro()
);
settings.push(Opt::Var(VarOpt::new(OptName::unknown_distro().to_owned())?));
settings.push(Opt::Var(VarOpt::new(OptName::unknown_distro())?));
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions crates/spk-schema/src/component_spec_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ impl ComponentSpecList {
visited.remove(&Component::All);
visited
}

/// Retrieve the component with the provided name
pub fn get<C>(&self, name: C) -> Option<&ComponentSpec>
where
C: std::cmp::PartialEq<Component>,
{
self.iter().find(|c| name == c.name)
}

/// Retrieve a component with the provided name or build an insert new one
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Retrieve a component with the provided name or build an insert new one
/// Retrieve a component with the provided name or insert a new one

pub fn get_or_insert_with(
&mut self,
name: Component,
default: impl FnOnce() -> ComponentSpec,
) -> &mut ComponentSpec {
let position = match self.iter().position(|c| c.name == name) {
Some(p) => p,
None => {
self.push(default());
self.len() - 1
}
};
&mut self[position]
}
}

impl Default for ComponentSpecList {
Expand Down
1 change: 1 addition & 0 deletions crates/spk-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod spec;
mod template;
mod test;
pub mod v0;
pub mod v1;
pub mod validation;
pub mod variant;

Expand Down
13 changes: 13 additions & 0 deletions crates/spk-schema/src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ pub trait BuildEnv {
fn env_vars(&self) -> HashMap<String, String>;
}

/// An empty build environment implementation, primarily used in testing
impl BuildEnv for () {
type Package = crate::Spec;

fn build_env(&self) -> Vec<Self::Package> {
Vec::new()
}

fn env_vars(&self) -> HashMap<String, String> {
HashMap::default()
}
}

/// Can be used to build a package.
#[enum_dispatch::enum_dispatch]
pub trait Recipe:
Expand Down
Loading
Loading