Skip to content

Commit 420720f

Browse files
committed
Convenience conversion from EmbeddedBuildSource to BuildIdent
Signed-off-by: J Robert Ray <[email protected]>
1 parent 426000f commit 420720f

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

crates/spk-schema/crates/foundation/src/ident_build/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ impl std::fmt::Display for Build {
177177
}
178178
}
179179

180+
impl TryFrom<String> for Build {
181+
type Error = super::Error;
182+
183+
fn try_from(value: String) -> Result<Self, Self::Error> {
184+
Self::from_str(&value)
185+
}
186+
}
187+
180188
impl FromStr for Build {
181189
type Err = super::Error;
182190

crates/spk-schema/crates/foundation/src/version/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,14 @@ impl TryFrom<&str> for Version {
554554
}
555555
}
556556

557+
impl TryFrom<String> for Version {
558+
type Error = Error;
559+
560+
fn try_from(value: String) -> Result<Self> {
561+
parse_version(value)
562+
}
563+
}
564+
557565
impl FromStr for Version {
558566
type Err = Error;
559567

crates/spk-schema/crates/ident/src/ident_build.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt::Write;
66
use std::str::FromStr;
77

88
use relative_path::RelativePathBuf;
9-
use spk_schema_foundation::ident_build::Build;
9+
use spk_schema_foundation::ident_build::{Build, EmbeddedSourcePackage};
1010
use spk_schema_foundation::ident_ops::parsing::IdentPartsBuf;
1111
use spk_schema_foundation::ident_ops::{MetadataPath, TagPath};
1212
use spk_schema_foundation::name::{PkgName, PkgNameBuf, RepositoryNameBuf};
@@ -30,6 +30,34 @@ pub type BuildIdent = Ident<VersionIdent, Build>;
3030

3131
crate::ident_version::version_ident_methods!(BuildIdent, .base);
3232

33+
impl TryFrom<EmbeddedSourcePackage> for BuildIdent {
34+
type Error = Error;
35+
36+
fn try_from(value: EmbeddedSourcePackage) -> std::result::Result<Self, Self::Error> {
37+
let IdentPartsBuf {
38+
repository_name: _,
39+
pkg_name,
40+
version_str: Some(version),
41+
build_str: Some(build),
42+
} = value.ident
43+
else {
44+
return if value.ident.build_str.is_some() {
45+
Err(Error::String(
46+
"EmbeddedSourcePackage missing version".to_string(),
47+
))
48+
} else {
49+
Err(Error::String(
50+
"EmbeddedSourcePackage missing build".to_string(),
51+
))
52+
};
53+
};
54+
Ok(Self::new(
55+
VersionIdent::new(pkg_name.try_into()?, version.try_into()?),
56+
build.try_into()?,
57+
))
58+
}
59+
}
60+
3361
macro_rules! build_ident_methods {
3462
($Ident:ty $(, .$($access:ident).+)?) => {
3563
impl $Ident {

0 commit comments

Comments
 (0)