Skip to content

Commit

Permalink
Create an ident::AsVersion trait
Browse files Browse the repository at this point in the history
This is useful for code that is generic over Ident to be able to require
that the Ident can represent itself as a VersionIdent.

Signed-off-by: J Robert Ray <jrray@imageworks.com>
  • Loading branch information
J Robert Ray committed Sep 27, 2024
1 parent 1db2a92 commit d56e61e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
15 changes: 15 additions & 0 deletions crates/spk-schema/crates/ident/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::VersionIdent;

#[cfg(test)]
#[path = "./ident_test.rs"]
mod ident_test;
Expand Down Expand Up @@ -201,3 +203,16 @@ where
deserializer.deserialize_str(IdentVisitor(PhantomData))
}
}

/// Idents that can be turned into a [`VersionIdent`] can implement this trait.
pub trait AsVersion {
/// Return a borrowed version of this ident converted into a
/// [`VersionIdent`].
fn as_version(&self) -> &VersionIdent;
}

impl<T> AsVersion for Ident<VersionIdent, T> {
fn as_version(&self) -> &VersionIdent {
self.base().as_version()
}
}
8 changes: 7 additions & 1 deletion crates/spk-schema/crates/ident/src/ident_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use spk_schema_foundation::ident_ops::{TagPath, TagPathStrategy};
use spk_schema_foundation::name::{PkgName, PkgNameBuf};
use spk_schema_foundation::version::Version;

use crate::{parsing, AnyIdent, BuildIdent, Ident, Result, ToAnyWithoutBuild};
use crate::{parsing, AnyIdent, AsVersion, BuildIdent, Ident, Result, ToAnyWithoutBuild};

/// Identifies a package name and number version.
pub type VersionIdent = Ident<PkgNameBuf, Version>;
Expand Down Expand Up @@ -58,6 +58,12 @@ impl VersionIdent {
}
}

impl AsVersion for VersionIdent {
fn as_version(&self) -> &VersionIdent {
self
}
}

macro_rules! version_ident_methods {
($Ident:ty $(, .$($access:ident).+)?) => {
impl $Ident {
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/crates/ident/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod request;
mod satisfy;

pub use error::{Error, Result};
pub use ident::Ident;
pub use ident::{AsVersion, Ident};
pub use ident_any::{parse_ident, AnyIdent, ToAnyWithoutBuild};
pub use ident_build::{parse_build_ident, BuildIdent};
pub use ident_located::{LocatedBuildIdent, LocatedVersionIdent};
Expand Down

0 comments on commit d56e61e

Please sign in to comment.