From f35dbbd6511f4cfc7db613b73fb4052e3380fee2 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 7 Oct 2021 13:39:10 +0200 Subject: [PATCH] add proxmox-section-config crate Signed-off-by: Wolfgang Bumiller --- Cargo.toml | 1 + Makefile | 1 + proxmox-section-config/Cargo.toml | 19 ++++ proxmox-section-config/debian/changelog | 5 + proxmox-section-config/debian/control | 42 ++++++++ proxmox-section-config/debian/copyright | 16 +++ proxmox-section-config/debian/debcargo.toml | 7 ++ .../src/lib.rs | 102 +++++++++--------- 8 files changed, 143 insertions(+), 50 deletions(-) create mode 100644 proxmox-section-config/Cargo.toml create mode 100644 proxmox-section-config/debian/changelog create mode 100644 proxmox-section-config/debian/control create mode 100644 proxmox-section-config/debian/copyright create mode 100644 proxmox-section-config/debian/debcargo.toml rename proxmox/src/api/section_config.rs => proxmox-section-config/src/lib.rs (92%) diff --git a/Cargo.toml b/Cargo.toml index 7048864a..df837b5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "proxmox-lang", "proxmox-router", "proxmox-schema", + "proxmox-section-config", "proxmox-sortable-macro", "proxmox-tfa", "proxmox-time", diff --git a/Makefile b/Makefile index ddf5dc1e..cb195bab 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ CRATES = \ proxmox-lang \ proxmox-router \ proxmox-schema \ + proxmox-section-config \ proxmox-sortable-macro \ proxmox-tfa \ proxmox-time \ diff --git a/proxmox-section-config/Cargo.toml b/proxmox-section-config/Cargo.toml new file mode 100644 index 00000000..78866610 --- /dev/null +++ b/proxmox-section-config/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "proxmox-section-config" +version = "1.0.0" +authors = ["Proxmox Support Team "] +edition = "2018" +license = "AGPL-3" +description = "proxmox schema based section config format parsing" + +exclude = [ "debian" ] + +[dependencies] +anyhow = "1.0" +hex = "0.4" +serde = "1.0" +serde_json = "1.0" + +proxmox-schema = { path = "../proxmox-schema", version = "1.0.0" } +# FIXME: remove! +proxmox-lang = { path = "../proxmox-lang", version = "1.0.0" } diff --git a/proxmox-section-config/debian/changelog b/proxmox-section-config/debian/changelog new file mode 100644 index 00000000..b9b247a1 --- /dev/null +++ b/proxmox-section-config/debian/changelog @@ -0,0 +1,5 @@ +rust-proxmox-section-config (1.0.0-1) stable; urgency=medium + + * initial split out of `librust-proxmox-dev` + + -- Proxmox Support Team Wed, 06 Oct 2021 11:04:36 +0200 diff --git a/proxmox-section-config/debian/control b/proxmox-section-config/debian/control new file mode 100644 index 00000000..c12dea6c --- /dev/null +++ b/proxmox-section-config/debian/control @@ -0,0 +1,42 @@ +Source: rust-proxmox-section-config +Section: rust +Priority: optional +Build-Depends: debhelper (>= 12), + dh-cargo (>= 24), + cargo:native , + rustc:native , + libstd-rust-dev , + librust-anyhow-1+default-dev , + librust-hex-0.4+default-dev , + librust-proxmox-lang-1+default-dev , + librust-proxmox-schema-1+default-dev , + librust-serde-1+default-dev , + librust-serde-json-1+default-dev +Maintainer: Proxmox Support Team +Standards-Version: 4.5.1 +Vcs-Git: git://git.proxmox.com/git/proxmox.git +Vcs-Browser: https://git.proxmox.com/?p=proxmox.git +Rules-Requires-Root: no + +Package: librust-proxmox-section-config-dev +Architecture: any +Multi-Arch: same +Depends: + ${misc:Depends}, + librust-anyhow-1+default-dev, + librust-hex-0.4+default-dev, + librust-proxmox-lang-1+default-dev, + librust-proxmox-schema-1+default-dev, + librust-serde-1+default-dev, + librust-serde-json-1+default-dev +Provides: + librust-proxmox-section-config+default-dev (= ${binary:Version}), + librust-proxmox-section-config-1-dev (= ${binary:Version}), + librust-proxmox-section-config-1+default-dev (= ${binary:Version}), + librust-proxmox-section-config-1.0-dev (= ${binary:Version}), + librust-proxmox-section-config-1.0+default-dev (= ${binary:Version}), + librust-proxmox-section-config-1.0.0-dev (= ${binary:Version}), + librust-proxmox-section-config-1.0.0+default-dev (= ${binary:Version}) +Description: Proxmox schema based section config format parsing - Rust source code + This package contains the source for the Rust proxmox-section-config crate, + packaged by debcargo for use with cargo and dh-cargo. diff --git a/proxmox-section-config/debian/copyright b/proxmox-section-config/debian/copyright new file mode 100644 index 00000000..5661ef60 --- /dev/null +++ b/proxmox-section-config/debian/copyright @@ -0,0 +1,16 @@ +Copyright (C) 2021 Proxmox Server Solutions GmbH + +This software is written by Proxmox Server Solutions GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . diff --git a/proxmox-section-config/debian/debcargo.toml b/proxmox-section-config/debian/debcargo.toml new file mode 100644 index 00000000..b7864cdb --- /dev/null +++ b/proxmox-section-config/debian/debcargo.toml @@ -0,0 +1,7 @@ +overlay = "." +crate_src_path = ".." +maintainer = "Proxmox Support Team " + +[source] +vcs_git = "git://git.proxmox.com/git/proxmox.git" +vcs_browser = "https://git.proxmox.com/?p=proxmox.git" diff --git a/proxmox/src/api/section_config.rs b/proxmox-section-config/src/lib.rs similarity index 92% rename from proxmox/src/api/section_config.rs rename to proxmox-section-config/src/lib.rs index 3fbaf705..36c63cc7 100644 --- a/proxmox/src/api/section_config.rs +++ b/proxmox-section-config/src/lib.rs @@ -18,18 +18,18 @@ //! ... //! ``` -use anyhow::*; - use std::collections::HashMap; use std::collections::HashSet; use std::collections::VecDeque; +use anyhow::{bail, format_err, Error}; use serde::de::DeserializeOwned; use serde::ser::Serialize; use serde_json::{json, Value}; -use super::schema::*; -use crate::try_block; +use proxmox_lang::try_block; +use proxmox_schema::format::{dump_properties, wrap_text, ParameterDisplayStyle}; +use proxmox_schema::*; /// Associates a section type name with a `Schema`. pub struct SectionConfigPlugin { @@ -180,7 +180,7 @@ impl SectionConfigData { let mut list: Vec = vec![]; let digest: Value = match digest { - Some(v) => crate::tools::digest_to_hex(v).into(), + Some(v) => hex::encode(v).into(), None => Value::Null, }; @@ -294,62 +294,64 @@ impl SectionConfig { /// plugins. Please note that `filename` is only used to improve /// error messages. pub fn write(&self, filename: &str, config: &SectionConfigData) -> Result { - try_block!({ - let mut list = VecDeque::new(); + self.write_do(config) + .map_err(|e: Error| format_err!("writing '{}' failed: {}", filename, e)) + } - let mut done = HashSet::new(); + fn write_do(&self, config: &SectionConfigData) -> Result { + let mut list = VecDeque::new(); - for section_id in &config.order { - if config.sections.get(section_id) == None { - continue; - }; - list.push_back(section_id); - done.insert(section_id); + let mut done = HashSet::new(); + + for section_id in &config.order { + if config.sections.get(section_id) == None { + continue; + }; + list.push_back(section_id); + done.insert(section_id); + } + + for section_id in config.sections.keys() { + if done.contains(section_id) { + continue; + }; + list.push_back(section_id); + } + + let mut raw = String::new(); + + for section_id in list { + let (type_name, section_config) = config.sections.get(section_id).unwrap(); + let plugin = self.plugins.get(type_name).unwrap(); + + let id_schema = plugin.get_id_schema().unwrap_or(self.id_schema); + if let Err(err) = parse_simple_value(§ion_id, &id_schema) { + bail!("syntax error in section identifier: {}", err.to_string()); + } + if section_id.chars().any(|c| c.is_control()) { + bail!("detected unexpected control character in section ID."); + } + if let Err(err) = verify_json_object(section_config, plugin.properties) { + bail!("verify section '{}' failed - {}", section_id, err); } - for section_id in config.sections.keys() { - if done.contains(section_id) { - continue; - }; - list.push_back(section_id); + if !raw.is_empty() { + raw += "\n" } - let mut raw = String::new(); + raw += &(self.format_section_header)(type_name, section_id, section_config)?; - for section_id in list { - let (type_name, section_config) = config.sections.get(section_id).unwrap(); - let plugin = self.plugins.get(type_name).unwrap(); - - let id_schema = plugin.get_id_schema().unwrap_or(self.id_schema); - if let Err(err) = parse_simple_value(§ion_id, &id_schema) { - bail!("syntax error in section identifier: {}", err.to_string()); - } - if section_id.chars().any(|c| c.is_control()) { - bail!("detected unexpected control character in section ID."); - } - if let Err(err) = verify_json_object(section_config, plugin.properties) { - bail!("verify section '{}' failed - {}", section_id, err); - } - - if !raw.is_empty() { - raw += "\n" - } - - raw += &(self.format_section_header)(type_name, section_id, section_config)?; - - for (key, value) in section_config.as_object().unwrap() { - if let Some(id_property) = &plugin.id_property { - if id_property == key { - continue; // skip writing out id properties, they are in the section header - } + for (key, value) in section_config.as_object().unwrap() { + if let Some(id_property) = &plugin.id_property { + if id_property == key { + continue; // skip writing out id properties, they are in the section header } - raw += &(self.format_section_content)(type_name, section_id, key, value)?; } + raw += &(self.format_section_content)(type_name, section_id, key, value)?; } + } - Ok(raw) - }) - .map_err(|e: Error| format_err!("writing '{}' failed: {}", filename, e)) + Ok(raw) } /// Parse configuration data.