[pbs-devel] [PATCH backup] constify schema usage and dedup some code

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Feb 22 09:47:17 CET 2022


Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 pbs-config/src/acl.rs             | 12 ++---
 pbs-config/src/datastore.rs       | 12 ++---
 pbs-config/src/domains.rs         | 12 ++---
 pbs-config/src/drive.rs           | 23 +++-----
 pbs-config/src/media_pool.rs      |  7 +--
 pbs-config/src/remote.rs          |  9 ++--
 pbs-config/src/sync.rs            | 11 ++--
 pbs-config/src/tape_job.rs        | 12 ++---
 pbs-config/src/traffic_control.rs | 10 ++--
 pbs-config/src/user.rs            | 16 +++---
 pbs-config/src/verify.rs          | 10 ++--
 src/config/acme/plugin.rs         | 16 +++---
 src/tools/systemd/config.rs       | 88 +++++++++----------------------
 13 files changed, 83 insertions(+), 155 deletions(-)

diff --git a/pbs-config/src/acl.rs b/pbs-config/src/acl.rs
index 13af45a9..28f0f686 100644
--- a/pbs-config/src/acl.rs
+++ b/pbs-config/src/acl.rs
@@ -8,7 +8,7 @@ use anyhow::{bail, Error};
 
 use lazy_static::lazy_static;
 
-use proxmox_schema::{ApiStringFormat, ApiType, Schema, StringSchema};
+use proxmox_schema::ApiType;
 
 use pbs_api_types::{Authid, Userid, Role, ROLE_NAME_NO_ACCESS};
 
@@ -20,12 +20,12 @@ lazy_static! {
     pub static ref ROLE_NAMES: HashMap<&'static str, (u64, &'static str)> = {
         let mut map = HashMap::new();
 
-        let list = match Role::API_SCHEMA {
-            Schema::String(StringSchema { format: Some(ApiStringFormat::Enum(list)), .. }) => list,
-            _ => unreachable!(),
-        };
+        const ROLE_SCHEMA_ENUM_LIST: &'static [proxmox_schema::EnumEntry] = Role::API_SCHEMA
+            .unwrap_string_schema()
+            .unwrap_format()
+            .unwrap_enum_format();
 
-        for entry in list.iter() {
+        for entry in ROLE_SCHEMA_ENUM_LIST {
             let privs: u64 = Role::from_str(entry.value).unwrap() as u64;
             map.insert(entry.value, (privs, entry.description));
         }
diff --git a/pbs-config/src/datastore.rs b/pbs-config/src/datastore.rs
index 12071a9f..dc56e0c3 100644
--- a/pbs-config/src/datastore.rs
+++ b/pbs-config/src/datastore.rs
@@ -2,7 +2,7 @@ use anyhow::{Error};
 use lazy_static::lazy_static;
 use std::collections::HashMap;
 
-use proxmox_schema::{ApiType, Schema};
+use proxmox_schema::ApiType;
 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
 
 use pbs_api_types::{DataStoreConfig, DATASTORE_SCHEMA};
@@ -14,12 +14,10 @@ lazy_static! {
 }
 
 fn init() -> SectionConfig {
-    let obj_schema = match DataStoreConfig::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-
-    let plugin = SectionConfigPlugin::new("datastore".to_string(), Some(String::from("name")), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::ObjectSchema =
+        DataStoreConfig::API_SCHEMA.unwrap_object_schema();
+    let plugin =
+        SectionConfigPlugin::new("datastore".to_string(), Some(String::from("name")), OBJ_SCHEMA);
     let mut config = SectionConfig::new(&DATASTORE_SCHEMA);
     config.register_plugin(plugin);
 
diff --git a/pbs-config/src/domains.rs b/pbs-config/src/domains.rs
index 90a83bc6..12a58048 100644
--- a/pbs-config/src/domains.rs
+++ b/pbs-config/src/domains.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
 use anyhow::{Error};
 use lazy_static::lazy_static;
 
-use proxmox_schema::{ApiType, Schema};
+use proxmox_schema::ApiType;
 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
 
 use pbs_api_types::{OpenIdRealmConfig, REALM_ID_SCHEMA};
@@ -15,12 +15,10 @@ lazy_static! {
 
 
 fn init() -> SectionConfig {
-    let obj_schema = match OpenIdRealmConfig::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-
-    let plugin = SectionConfigPlugin::new("openid".to_string(), Some(String::from("realm")), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::ObjectSchema =
+        OpenIdRealmConfig::API_SCHEMA.unwrap_object_schema();
+    let plugin =
+        SectionConfigPlugin::new("openid".to_string(), Some(String::from("realm")), OBJ_SCHEMA);
     let mut config = SectionConfig::new(&REALM_ID_SCHEMA);
     config.register_plugin(plugin);
 
diff --git a/pbs-config/src/drive.rs b/pbs-config/src/drive.rs
index 13be8841..66aa3c78 100644
--- a/pbs-config/src/drive.rs
+++ b/pbs-config/src/drive.rs
@@ -35,25 +35,18 @@ lazy_static! {
 fn init() -> SectionConfig {
     let mut config = SectionConfig::new(&DRIVE_NAME_SCHEMA);
 
-    let obj_schema = match VirtualTapeDrive::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-    let plugin = SectionConfigPlugin::new("virtual".to_string(), Some("name".to_string()), obj_schema);
+    const VIRT_SCHEMA: &ObjectSchema = VirtualTapeDrive::API_SCHEMA.unwrap_object_schema();
+    let plugin =
+        SectionConfigPlugin::new("virtual".to_string(), Some("name".to_string()), VIRT_SCHEMA);
     config.register_plugin(plugin);
 
-    let obj_schema = match LtoTapeDrive::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-    let plugin = SectionConfigPlugin::new("lto".to_string(), Some("name".to_string()), obj_schema);
+    const LTO_SCHEMA: &ObjectSchema = LtoTapeDrive::API_SCHEMA.unwrap_object_schema();
+    let plugin = SectionConfigPlugin::new("lto".to_string(), Some("name".to_string()), LTO_SCHEMA);
     config.register_plugin(plugin);
 
-    let obj_schema = match ScsiTapeChanger::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-    let plugin = SectionConfigPlugin::new("changer".to_string(), Some("name".to_string()), obj_schema);
+    const CHANGER_SCHEMA: &ObjectSchema = ScsiTapeChanger::API_SCHEMA.unwrap_object_schema();
+    let plugin =
+        SectionConfigPlugin::new("changer".to_string(), Some("name".to_string()), CHANGER_SCHEMA);
     config.register_plugin(plugin);
     config
 }
diff --git a/pbs-config/src/media_pool.rs b/pbs-config/src/media_pool.rs
index 35a2924e..40403d27 100644
--- a/pbs-config/src/media_pool.rs
+++ b/pbs-config/src/media_pool.rs
@@ -26,11 +26,8 @@ lazy_static! {
 fn init() -> SectionConfig {
     let mut config = SectionConfig::new(&MEDIA_POOL_NAME_SCHEMA);
 
-    let obj_schema = match MediaPoolConfig::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-    let plugin = SectionConfigPlugin::new("pool".to_string(), Some("name".to_string()), obj_schema);
+    const OBJ_SCHEMA: &ObjectSchema = MediaPoolConfig::API_SCHEMA.unwrap_object_schema();
+    let plugin = SectionConfigPlugin::new("pool".to_string(), Some("name".to_string()), OBJ_SCHEMA);
     config.register_plugin(plugin);
 
     config
diff --git a/pbs-config/src/remote.rs b/pbs-config/src/remote.rs
index 921410e2..81da3574 100644
--- a/pbs-config/src/remote.rs
+++ b/pbs-config/src/remote.rs
@@ -15,12 +15,9 @@ lazy_static! {
 }
 
 fn init() -> SectionConfig {
-    let obj_schema = match Remote::API_SCHEMA {
-        Schema::AllOf(ref allof_schema) => allof_schema,
-        _ => unreachable!(),
-    };
-
-    let plugin = SectionConfigPlugin::new("remote".to_string(), Some("name".to_string()), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::AllOfSchema = Remote::API_SCHEMA.unwrap_all_of_schema();
+    let plugin =
+        SectionConfigPlugin::new("remote".to_string(), Some("name".to_string()), OBJ_SCHEMA);
     let mut config = SectionConfig::new(&REMOTE_ID_SCHEMA);
     config.register_plugin(plugin);
 
diff --git a/pbs-config/src/sync.rs b/pbs-config/src/sync.rs
index f515613f..a9e137b6 100644
--- a/pbs-config/src/sync.rs
+++ b/pbs-config/src/sync.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
 use anyhow::Error;
 use lazy_static::lazy_static;
 
-use proxmox_schema::{ApiType, Schema};
+use proxmox_schema::ApiType;
 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
 
 use pbs_api_types::{JOB_ID_SCHEMA, SyncJobConfig};
@@ -16,12 +16,9 @@ lazy_static! {
 
 
 fn init() -> SectionConfig {
-    let obj_schema = match SyncJobConfig::API_SCHEMA {
-        Schema::AllOf(ref allof_schema) => allof_schema,
-        _ => unreachable!(),
-    };
-
-    let plugin = SectionConfigPlugin::new("sync".to_string(), Some(String::from("id")), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::AllOfSchema =
+        SyncJobConfig::API_SCHEMA.unwrap_all_of_schema();
+    let plugin = SectionConfigPlugin::new("sync".to_string(), Some(String::from("id")), OBJ_SCHEMA);
     let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
     config.register_plugin(plugin);
 
diff --git a/pbs-config/src/tape_job.rs b/pbs-config/src/tape_job.rs
index 992d4382..f208f7a1 100644
--- a/pbs-config/src/tape_job.rs
+++ b/pbs-config/src/tape_job.rs
@@ -2,7 +2,7 @@ use anyhow::{Error};
 use lazy_static::lazy_static;
 use std::collections::HashMap;
 
-use proxmox_schema::{Schema, ApiType};
+use proxmox_schema::ApiType;
 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
 
 use pbs_api_types::{TapeBackupJobConfig, JOB_ID_SCHEMA};
@@ -14,12 +14,10 @@ lazy_static! {
 }
 
 fn init() -> SectionConfig {
-    let obj_schema = match TapeBackupJobConfig::API_SCHEMA {
-        Schema::AllOf(ref allof_schema) => allof_schema,
-        _ => unreachable!(),
-    };
-
-    let plugin = SectionConfigPlugin::new("backup".to_string(), Some(String::from("id")), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::AllOfSchema =
+        TapeBackupJobConfig::API_SCHEMA.unwrap_all_of_schema();
+    let plugin =
+        SectionConfigPlugin::new("backup".to_string(), Some(String::from("id")), OBJ_SCHEMA);
     let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
     config.register_plugin(plugin);
 
diff --git a/pbs-config/src/traffic_control.rs b/pbs-config/src/traffic_control.rs
index 860d0fb7..98b6a544 100644
--- a/pbs-config/src/traffic_control.rs
+++ b/pbs-config/src/traffic_control.rs
@@ -4,7 +4,7 @@ use std::collections::HashMap;
 use anyhow::Error;
 use lazy_static::lazy_static;
 
-use proxmox_schema::{ApiType, Schema};
+use proxmox_schema::ApiType;
 
 use pbs_api_types::{TrafficControlRule, TRAFFIC_CONTROL_ID_SCHEMA};
 
@@ -21,11 +21,9 @@ lazy_static! {
 fn init() -> SectionConfig {
     let mut config = SectionConfig::new(&TRAFFIC_CONTROL_ID_SCHEMA);
 
-    let obj_schema = match TrafficControlRule::API_SCHEMA {
-        Schema::AllOf(ref allof_schema) => allof_schema,
-        _ => unreachable!(),
-    };
-    let plugin = SectionConfigPlugin::new("rule".to_string(), Some("name".to_string()), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::AllOfSchema =
+        TrafficControlRule::API_SCHEMA.unwrap_all_of_schema();
+    let plugin = SectionConfigPlugin::new("rule".to_string(), Some("name".to_string()), OBJ_SCHEMA);
     config.register_plugin(plugin);
 
     config
diff --git a/pbs-config/src/user.rs b/pbs-config/src/user.rs
index f62d45db..a69865a9 100644
--- a/pbs-config/src/user.rs
+++ b/pbs-config/src/user.rs
@@ -22,18 +22,14 @@ lazy_static! {
 fn init() -> SectionConfig {
     let mut config = SectionConfig::new(&Authid::API_SCHEMA);
 
-    let user_schema = match User::API_SCHEMA {
-        Schema::Object(ref user_schema) => user_schema,
-        _ => unreachable!(),
-    };
-    let user_plugin = SectionConfigPlugin::new("user".to_string(), Some("userid".to_string()), user_schema);
+    const USER_SCHEMA: &ObjectSchema = User::API_SCHEMA.unwrap_object_schema();
+    let user_plugin =
+        SectionConfigPlugin::new("user".to_string(), Some("userid".to_string()), USER_SCHEMA);
     config.register_plugin(user_plugin);
 
-    let token_schema = match ApiToken::API_SCHEMA {
-        Schema::Object(ref token_schema) => token_schema,
-        _ => unreachable!(),
-    };
-    let token_plugin = SectionConfigPlugin::new("token".to_string(), Some("tokenid".to_string()), token_schema);
+    const TOKEN_SCHEMA: &ObjectSchema = ApiToken::API_SCHEMA.unwrap_object_schema();
+    let token_plugin =
+        SectionConfigPlugin::new("token".to_string(), Some("tokenid".to_string()), TOKEN_SCHEMA);
     config.register_plugin(token_plugin);
 
     config
diff --git a/pbs-config/src/verify.rs b/pbs-config/src/verify.rs
index b6c70caa..efe22996 100644
--- a/pbs-config/src/verify.rs
+++ b/pbs-config/src/verify.rs
@@ -15,12 +15,10 @@ lazy_static! {
 }
 
 fn init() -> SectionConfig {
-    let obj_schema = match VerificationJobConfig::API_SCHEMA {
-        Schema::Object(ref obj_schema) => obj_schema,
-        _ => unreachable!(),
-    };
-
-    let plugin = SectionConfigPlugin::new("verification".to_string(), Some(String::from("id")), obj_schema);
+    const OBJ_SCHEMA: &proxmox_schema::ObjectSchema =
+        VerificationJobConfig::API_SCHEMA.unwrap_object_schema();
+    let plugin =
+        SectionConfigPlugin::new("verification".to_string(), Some(String::from("id")), OBJ_SCHEMA);
     let mut config = SectionConfig::new(&JOB_ID_SCHEMA);
     config.register_plugin(plugin);
 
diff --git a/src/config/acme/plugin.rs b/src/config/acme/plugin.rs
index 5decc154..1788267f 100644
--- a/src/config/acme/plugin.rs
+++ b/src/config/acme/plugin.rs
@@ -107,25 +107,21 @@ impl DnsPlugin {
 fn init() -> SectionConfig {
     let mut config = SectionConfig::new(&PLUGIN_ID_SCHEMA);
 
-    let standalone_schema = match &StandalonePlugin::API_SCHEMA {
-        Schema::Object(schema) => schema,
-        _ => unreachable!(),
-    };
+    const STANDALONE_SCHEMA: &proxmox_schema::ObjectSchema =
+        StandalonePlugin::API_SCHEMA.unwrap_object_schema();
     let standalone_plugin = SectionConfigPlugin::new(
         "standalone".to_string(),
         Some("id".to_string()),
-        standalone_schema,
+        STANDALONE_SCHEMA,
     );
     config.register_plugin(standalone_plugin);
 
-    let dns_challenge_schema = match DnsPlugin::API_SCHEMA {
-        Schema::AllOf(ref schema) => schema,
-        _ => unreachable!(),
-    };
+    const DNS_CHALLENGE_SCHEMA: &proxmox_schema::AllOfSchema =
+        DnsPlugin::API_SCHEMA.unwrap_all_of_schema();
     let dns_challenge_plugin = SectionConfigPlugin::new(
         "dns".to_string(),
         Some("id".to_string()),
-        dns_challenge_schema,
+        DNS_CHALLENGE_SCHEMA,
     );
     config.register_plugin(dns_challenge_plugin);
 
diff --git a/src/tools/systemd/config.rs b/src/tools/systemd/config.rs
index 95c1a942..8dae1bf5 100644
--- a/src/tools/systemd/config.rs
+++ b/src/tools/systemd/config.rs
@@ -15,31 +15,25 @@ lazy_static! {
     pub static ref MOUNT_CONFIG: SectionConfig = init_mount();
 }
 
+fn register_unit_section(config: &mut SectionConfig) {
+    const UNIT_SCHEMA: &ObjectSchema = SystemdUnitSection::API_SCHEMA.unwrap_object_schema();
+    config.register_plugin(SectionConfigPlugin::new("Unit".to_string(), None, UNIT_SCHEMA));
+}
+
+fn register_install_section(config: &mut SectionConfig) {
+    const INSTALL_SCHEMA: &ObjectSchema = SystemdInstallSection::API_SCHEMA.unwrap_object_schema();
+    config.register_plugin(SectionConfigPlugin::new("Install".to_string(), None, INSTALL_SCHEMA));
+}
+
 fn init_service() -> SectionConfig {
 
     let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
 
-    match SystemdUnitSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
-    match SystemdInstallSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
-    match SystemdServiceSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Service".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
+    register_unit_section(&mut config);
+    register_install_section(&mut config);
+
+    const SERVICE_SCHEMA: &ObjectSchema = SystemdServiceSection::API_SCHEMA.unwrap_object_schema();
+    config.register_plugin(SectionConfigPlugin::new("Service".to_string(), None, SERVICE_SCHEMA));
 
     config
 }
@@ -48,27 +42,11 @@ fn init_timer() -> SectionConfig {
 
     let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
 
-    match SystemdUnitSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
-    match SystemdInstallSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
-    match SystemdTimerSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Timer".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
+    register_unit_section(&mut config);
+    register_install_section(&mut config);
+
+    const TIMER_SCHEMA: &ObjectSchema = SystemdTimerSection::API_SCHEMA.unwrap_object_schema();
+    config.register_plugin(SectionConfigPlugin::new("Timer".to_string(), None, TIMER_SCHEMA));
 
     config
 }
@@ -77,27 +55,11 @@ fn init_mount() -> SectionConfig {
 
     let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
 
-    match SystemdUnitSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
-    match SystemdInstallSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
-    match SystemdMountSection::API_SCHEMA {
-        Schema::Object(ref obj_schema) =>  {
-            let plugin = SectionConfigPlugin::new("Mount".to_string(), None, obj_schema);
-            config.register_plugin(plugin);
-        }
-        _ => unreachable!(),
-    };
+    register_unit_section(&mut config);
+    register_install_section(&mut config);
+
+    const MOUNT_SCHEMA: &ObjectSchema = SystemdMountSection::API_SCHEMA.unwrap_object_schema();
+    config.register_plugin(SectionConfigPlugin::new("Mount".to_string(), None, MOUNT_SCHEMA));
 
     config
 }
-- 
2.30.2






More information about the pbs-devel mailing list