[pve-devel] [PATCH proxmox-ve-rs v2 12/25] sdn: add name types

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Nov 6 15:18:16 CET 2024


On Thu, Oct 10, 2024 at 05:56:24PM GMT, Stefan Hanreich wrote:
> Signed-off-by: Stefan Hanreich <s.hanreich at proxmox.com>
> ---
>  proxmox-ve-config/src/lib.rs     |   1 +
>  proxmox-ve-config/src/sdn/mod.rs | 240 +++++++++++++++++++++++++++++++
>  2 files changed, 241 insertions(+)
>  create mode 100644 proxmox-ve-config/src/sdn/mod.rs
> 
> diff --git a/proxmox-ve-config/src/lib.rs b/proxmox-ve-config/src/lib.rs
> index 1b6feae..d17136c 100644
> --- a/proxmox-ve-config/src/lib.rs
> +++ b/proxmox-ve-config/src/lib.rs
> @@ -2,3 +2,4 @@ pub mod common;
>  pub mod firewall;
>  pub mod guest;
>  pub mod host;
> +pub mod sdn;
> diff --git a/proxmox-ve-config/src/sdn/mod.rs b/proxmox-ve-config/src/sdn/mod.rs
> new file mode 100644
> index 0000000..4e7c525
> --- /dev/null
> +++ b/proxmox-ve-config/src/sdn/mod.rs
> @@ -0,0 +1,240 @@
> +use std::{error::Error, fmt::Display, str::FromStr};
> +
> +use serde_with::DeserializeFromStr;
> +
> +use crate::firewall::types::Cidr;
> +
> +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
> +pub enum SdnNameError {
> +    Empty,
> +    TooLong,
> +    InvalidSymbols,
> +    InvalidSubnetCidr,
> +    InvalidSubnetFormat,
> +}
> +
> +impl Error for SdnNameError {}
> +
> +impl Display for SdnNameError {
> +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
> +        f.write_str(match self {
> +            SdnNameError::TooLong => "name too long",
> +            SdnNameError::InvalidSymbols => "invalid symbols in name",
> +            SdnNameError::InvalidSubnetCidr => "invalid cidr in name",
> +            SdnNameError::InvalidSubnetFormat => "invalid format for subnet name",
> +            SdnNameError::Empty => "name is empty",
> +        })
> +    }
> +}
> +
> +fn validate_sdn_name(name: &str) -> Result<(), SdnNameError> {
> +    if name.is_empty() {
> +        return Err(SdnNameError::Empty);
> +    }
> +
> +    if name.len() > 8 {
> +        return Err(SdnNameError::TooLong);
> +    }
> +
> +    // safe because of empty check
> +    if !name.chars().next().unwrap().is_ascii_alphabetic() {
> +        return Err(SdnNameError::InvalidSymbols);
> +    }
> +
> +    if !name.chars().all(|c| c.is_ascii_alphanumeric()) {
> +        return Err(SdnNameError::InvalidSymbols);
> +    }
> +
> +    Ok(())
> +}
> +
> +/// represents the name of an sdn zone
> +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, DeserializeFromStr)]
> +pub struct ZoneName(String);
> +
> +impl ZoneName {
> +    /// construct a new zone name
> +    ///
> +    /// # Errors
> +    ///
> +    /// This function will return an error if the name is empty, too long (>8 characters), starts
> +    /// with a non-alphabetic symbol or if there are non alphanumeric symbols contained in the name.
> +    pub fn new(name: String) -> Result<Self, SdnNameError> {
> +        validate_sdn_name(&name)?;
> +        Ok(ZoneName(name))
> +    }
> +
> +    pub fn name(&self) -> &str {

^ I wonder if this should be `as_str()`
And would it make sense to `impl AsRef<str>`?




More information about the pve-devel mailing list