api-types: datastore type improvements

let BackupGroup implement Hash

let BackupGroup and BackupDir be AsRef<BackupGroup>
let BackupDir be AsRef<BackupDir>

the pbs-datastore types will implement these AsRefs as well

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-04-19 12:11:16 +02:00
parent 2d5c20c8f5
commit b444eb68af
2 changed files with 38 additions and 1 deletions

View File

@ -471,7 +471,7 @@ impl std::cmp::PartialOrd for BackupType {
"backup-id": { schema: BACKUP_ID_SCHEMA },
},
)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
/// A backup group (without a data store).
pub struct BackupGroup {
@ -488,6 +488,28 @@ impl BackupGroup {
pub fn new<T: Into<String>>(ty: BackupType, id: T) -> Self {
Self { ty, id: id.into() }
}
pub fn matches(&self, filter: &crate::GroupFilter) -> bool {
use crate::GroupFilter;
match filter {
GroupFilter::Group(backup_group) => {
match backup_group.parse::<BackupGroup>() {
Ok(group) => *self == group,
Err(_) => false, // shouldn't happen if value is schema-checked
}
}
GroupFilter::BackupType(backup_type) => self.ty.as_str() == backup_type,
GroupFilter::Regex(regex) => regex.is_match(&self.to_string()),
}
}
}
impl AsRef<BackupGroup> for BackupGroup {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl From<(BackupType, String)> for BackupGroup {
@ -568,6 +590,20 @@ pub struct BackupDir {
pub time: i64,
}
impl AsRef<BackupGroup> for BackupDir {
#[inline]
fn as_ref(&self) -> &BackupGroup {
&self.group
}
}
impl AsRef<BackupDir> for BackupDir {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl From<(BackupGroup, i64)> for BackupDir {
fn from(data: (BackupGroup, i64)) -> Self {
Self {

View File

@ -342,6 +342,7 @@ pub struct TapeBackupJobStatus {
/// Filter for matching `BackupGroup`s, for use with `BackupGroup::filter`.
pub enum GroupFilter {
/// BackupGroup type - either `vm`, `ct`, or `host`.
// FIXME: Should be `BackupType`
BackupType(String),
/// Full identifier of BackupGroup, including type
Group(String),