@@ -10,7 +10,7 @@ static VERSION_INFO_PKG: OnceLock<VersionInfo> = OnceLock::new();
1010static VERSION_INFO_SYS : OnceLock < VersionInfo > = OnceLock :: new ( ) ;
1111
1212/// Contains version information.
13- #[ derive( Copy , Clone ) ]
13+ #[ derive( Clone ) ]
1414#[ non_exhaustive]
1515pub struct VersionInfo {
1616 /// The *major* version of the software.
@@ -19,37 +19,49 @@ pub struct VersionInfo {
1919 pub minor : u16 ,
2020 /// The *patch* level of the software.
2121 pub patch : u16 ,
22+ /// Additional version identifier (e.g. "beta-2")
23+ pub ident : Option < String > ,
2224}
2325
24- /// Contains version information .
25- #[ derive( Copy , Clone ) ]
26+ /// Contains the FAPI version .
27+ #[ derive( Clone ) ]
2628#[ non_exhaustive]
2729pub struct FapiVersion {
28- /// Version of the `tss2-fapi-rs` package
30+ /// Version of the `tss2-fapi-rs` package (Rust wrapper)
2931 pub package : VersionInfo ,
3032 /// Versin of the "native" FAPI library that was used to build `tss2-fapi-rs`
31- pub native : VersionInfo ,
33+ pub library : VersionInfo ,
3234}
3335
3436/// Returns the package version of the **`tss2-fapi-rs`** library.
3537///
3638/// Additionally, the version of the "native" FAPI library that was used to build `tss2-fapi-rs` is returned.
3739pub fn get_version ( ) -> FapiVersion {
40+ const PACKAGE_VERSION : & str = env ! ( "CARGO_PKG_VERSION" , "Package version is not defined!" ) ;
3841 FapiVersion {
39- package : * VERSION_INFO_PKG . get_or_init ( || parse_version ( env ! ( "CARGO_PKG_VERSION" , "Package version not defined!" ) ) ) ,
40- native : * VERSION_INFO_SYS . get_or_init ( || parse_version ( crate :: fapi_sys:: TSS2_FAPI_VERSION ) ) ,
42+ package : VERSION_INFO_PKG . get_or_init ( || parse_version ( PACKAGE_VERSION ) ) . clone ( ) ,
43+ library : VERSION_INFO_SYS . get_or_init ( || parse_version ( crate :: fapi_sys:: TSS2_FAPI_VERSION ) ) . clone ( ) ,
4144 }
4245}
4346
4447/// Parse a version string that is in the `"major.minor.patch"` format into a [`VersionInfo`] struct.
4548fn parse_version ( version_string : & str ) -> VersionInfo {
46- let mut tokens = version_string. split ( '.' ) . map ( |str| str. parse :: < u16 > ( ) . unwrap_or_default ( ) ) ;
47- VersionInfo { major : tokens. next ( ) . unwrap_or_default ( ) , minor : tokens. next ( ) . unwrap_or_default ( ) , patch : tokens. next ( ) . unwrap_or_default ( ) }
49+ let mut tokens = version_string. splitn ( 4usize , [ '.' , '+' , '-' , '_' ] ) . map ( str:: trim_ascii) ;
50+ VersionInfo {
51+ major : tokens. next ( ) . and_then ( |str| str. parse :: < u16 > ( ) . ok ( ) ) . unwrap_or_default ( ) ,
52+ minor : tokens. next ( ) . and_then ( |str| str. parse :: < u16 > ( ) . ok ( ) ) . unwrap_or_default ( ) ,
53+ patch : tokens. next ( ) . and_then ( |str| str. parse :: < u16 > ( ) . ok ( ) ) . unwrap_or_default ( ) ,
54+ ident : tokens. next ( ) . filter ( |str| !str. is_empty ( ) ) . map ( str:: to_owned) ,
55+ }
4856}
4957
5058/// Convert the `VersionInfo` struct to a string in the `"major.minor.patch"` format
5159impl Display for VersionInfo {
5260 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
53- write ! ( f, "{}.{}.{}" , self . major, self . minor, self . patch)
61+ if let Some ( ident_str) = self . ident . as_ref ( ) {
62+ write ! ( f, "{}.{}.{}-{}" , self . major, self . minor, self . patch, ident_str)
63+ } else {
64+ write ! ( f, "{}.{}.{}" , self . major, self . minor, self . patch)
65+ }
5466 }
5567}
0 commit comments