Skip to content

Commit ff5eafa

Browse files
committed
Some improvements to parsing the version string + updated changelog.
1 parent ebae887 commit ff5eafa

7 files changed

Lines changed: 54 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/).
66

7+
## [0.9.0] - 2026-01-20
8+
9+
### Added
10+
11+
- Added the `FapiContext::set_callbacks()` function, which can be used to set or replace all callback functions at once.
12+
- Added the `FapiContext::clear_callbacks()` function, which can be used to unset all callback functions.
13+
- Added the `FapiCallbacks` trait, which represents the set of application-defined callback functions that the FAPI invokes.
14+
15+
### Changed
16+
17+
- Function `get_version()` now returns a `FapiVersion` struct.
18+
- Functions `FapiContext::set_callbacks()` and `FapiContext::clear_callbacks()` now return the previous callbacks.
19+
20+
### Removed
21+
22+
- Removed the `FapiContext::set_auth_callback` function; superseded by `set_callbacks()`.
23+
- Removed the `FapiContext::set_sign_callback` function; superseded by `set_callbacks()`.
24+
- Removed the `FapiContext::set_branch_callback` function; superseded by `set_callbacks()`.
25+
- Removed the `FapiContext::set_policy_action_callback` function; superseded by `set_callbacks()`.
26+
727
## [0.8.1] - 2026-01-15
828

929
### Added

build.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const LIBRARY_NAME: &str = "tss2-fapi";
2525
const LIBRARY_MIN_VERSION: &str = "3.2.0";
2626

2727
/// Regex to parse the version string, assuming the `<major>.<minor>.<patch>` format
28-
static REGEX_VERSION: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\d+)\.(\d+)\.(\d+)").unwrap());
28+
static REGEX_VERSION: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\d+)\.(\d+)\.(\d+)([-+._]\S+)?").unwrap());
2929

3030
/// This build scripts is required to detect and link the "native" FAPI library
3131
fn main() {
@@ -111,13 +111,10 @@ fn detect_tss2_library() -> Result<LibraryConfig, Error> {
111111
fn write_version_string(path: &Path, version_string: &str) -> Result<(), IoError> {
112112
// Parse the version string
113113
let captures = REGEX_VERSION.captures(version_string.trim_ascii()).expect("Invalid version string!");
114-
let vers_major = captures.get(1).unwrap().as_str().parse::<u16>().expect("Failed to parse version string!");
115-
let vers_minor = captures.get(2).unwrap().as_str().parse::<u16>().expect("Failed to parse version string!");
116-
let vers_patch = captures.get(3).unwrap().as_str().parse::<u16>().expect("Failed to parse version string!");
117114

118115
// Try to write the version string to the output file
119116
let mut file = File::create(path).expect("Failed to create output file for version!");
120-
writeln!(file, r#"pub const TSS2_FAPI_VERSION: &str = "{}.{}.{}";"#, vers_major, vers_minor, vers_patch)
117+
writeln!(file, r#"pub const TSS2_FAPI_VERSION: &str = "{}";"#, captures.get(0).unwrap().as_str())
121118
}
122119

123120
/// Parse the given 'flag' string as a boolean value

examples/1_get_random.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn main() {
2828
info!("TSS2 FAPI Wrapper - Example #1");
2929

3030
// Print library version
31-
let version_info = tss2_fapi_rs::get_version();
32-
info!("Using tss2-fapi-rs package version: {}, built with native FAPI library version: {}", version_info.package, version_info.native);
31+
let version = tss2_fapi_rs::get_version();
32+
info!("Using tss2-fapi-rs package version: {}, built with native FAPI library version: {}", version.package, version.library);
3333

3434
// Create a new FAPI context
3535
info!("Creating FAPI context, please wait...");

src/callback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub struct CallbackManager {
213213
}
214214

215215
impl CallbackManager {
216-
pub fn new(callbacks: impl FapiCallbacks + 'static) -> Self {
216+
pub fn new(callbacks: impl FapiCallbacks) -> Self {
217217
Self { inner: Box::new(callbacks), auth_value: None, sign_data: None }
218218
}
219219

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ pub use context::FapiContext;
456456
pub use error::{BaseErrorCode, ErrorCode, InternalError, Tpm2ErrFmt0, Tpm2ErrFmt1, Tpm2ErrorCode, Tpm2Warning};
457457
pub use flags::{BlobType, KeyFlags, NvFlags, PaddingFlags, QuoteFlags, SealFlags};
458458
pub use types::{ImportData, QuoteResult, SignResult, TpmBlobs};
459-
pub use version::{VersionInfo, get_version};
459+
pub use version::{FapiVersion, VersionInfo, get_version};
460460

461461
// Re-export JSON module
462462
pub use ::json;

src/version.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static VERSION_INFO_PKG: OnceLock<VersionInfo> = OnceLock::new();
1010
static VERSION_INFO_SYS: OnceLock<VersionInfo> = OnceLock::new();
1111

1212
/// Contains version information.
13-
#[derive(Copy, Clone)]
13+
#[derive(Clone)]
1414
#[non_exhaustive]
1515
pub 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]
2729
pub 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.
3739
pub 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.
4548
fn 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
5159
impl 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
}

tests/01_version_test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ fn test_version() {
2525
repeat_test!(|_i| {
2626
let version_info = get_version();
2727
debug!("tss2-fapi-rs package version: {}", version_info.package);
28-
debug!("Native FAPI version: {}", version_info.native);
28+
debug!("TSS 2.0 FAPI library version: {}", version_info.library);
29+
30+
let package = black_box(version_info.package);
31+
let library = black_box(version_info.library);
2932

3033
// Verify the package version
31-
assert_eq!(CURRENT_PKG_VERSION, format!("{}", version_info.package));
34+
assert_eq!(CURRENT_PKG_VERSION, format!("{}", package));
3235

3336
// Verify the FAPI version
34-
let native_ver = &black_box(version_info.native);
35-
assert!((native_ver.major > 3u16) || ((native_ver.major == 3u16) && ((native_ver.minor > 0u16) || (native_ver.patch >= 3u16))));
37+
assert!((library.major > 3u16) || ((library.major == 3u16) && ((library.minor > 0u16) || (library.patch >= 3u16))));
3638
});
3739
}

0 commit comments

Comments
 (0)