-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Add Platform V1 Spec #1187
Changes from all commits
8243114
b53a101
18cc3f4
10c867c
0e7bb4f
f8353dd
9ad0e86
d109b39
73f6942
10cabc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It would be great if |
||
} | ||
} | ||
|
||
pub fn parse_version_range<S: AsRef<str>>(range: S) -> Result<VersionRange> { | ||
let mut filter = VersionFilter::from_str(range.as_ref())?; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(_)) | ||
|
@@ -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, | ||
|
@@ -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`)") | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ mod spec; | |
mod template; | ||
mod test; | ||
pub mod v0; | ||
pub mod v1; | ||
pub mod validation; | ||
pub mod variant; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.