#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub struct ApiVersion(pub i16);
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct ApiVersionRange {
min: ApiVersion,
max: ApiVersion,
}
impl std::fmt::Display for ApiVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[allow(dead_code)]
impl ApiVersionRange {
pub const fn new(min: ApiVersion, max: ApiVersion) -> Self {
assert!(min.0 <= max.0);
Self { min, max }
}
pub fn min(&self) -> ApiVersion {
self.min
}
pub fn max(&self) -> ApiVersion {
self.max
}
}
impl std::fmt::Display for ApiVersionRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.min, self.max)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_version_display() {
let version = ApiVersion(3);
assert_eq!(version.to_string(), "3");
}
#[test]
fn api_version_range_accessors() {
let range = ApiVersionRange::new(ApiVersion(1), ApiVersion(4));
assert_eq!(range.min(), ApiVersion(1));
assert_eq!(range.max(), ApiVersion(4));
assert_eq!(range.to_string(), "1:4");
}
#[test]
#[should_panic]
fn api_version_range_panics_on_invalid_bounds() {
let _ = ApiVersionRange::new(ApiVersion(4), ApiVersion(1));
}
}