From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <c.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id C38D9938E4
 for <pve-devel@lists.proxmox.com>; Tue,  6 Feb 2024 10:51:25 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A269F31991
 for <pve-devel@lists.proxmox.com>; Tue,  6 Feb 2024 10:51:25 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue,  6 Feb 2024 10:51:24 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A9B514470D
 for <pve-devel@lists.proxmox.com>; Tue,  6 Feb 2024 10:51:24 +0100 (CET)
From: Christian Ebner <c.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue,  6 Feb 2024 10:51:00 +0100
Message-Id: <20240206095101.89765-1-c.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  2
 AWL -0.001 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 FSL_BULK_SIG            1.008 Bulk signature with no Unsubscribe
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RAZOR2_CF_RANGE_51_100  1.886 Razor2 gives confidence level above 50%
 RAZOR2_CHECK            0.922 Listed in Razor2 (http://razor.sf.net/)
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [file.rs, release.rs, github.io]
 URIBL_SBL_A 0.1 Contains URL's A record listed in the Spamhaus SBL blocklist
 [185.199.108.153, 185.199.109.153, 185.199.110.153, 185.199.111.153]
Subject: [pve-devel] [PATCH v2 proxmox master stable-2 1/2] apt: repos:
 extend `DebianCodename` by `Unknown` variant
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 06 Feb 2024 09:51:25 -0000

Instead of returning an Option for the DebianCodename variant, with None
for unknowns, extend the enum by an Unknown variant with additional
internal type to avoid misuse of this variant.

Co-authored-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
Changes since v1:
- Fixed incorrect refactoring of codename matching
- s/Codename/DebianCodename/ in commit title and message\
- fixed warning for DebianCodename Display::fmt implementation by
  removing unused variable binding.

 proxmox-apt/src/repositories/file.rs    |  2 +-
 proxmox-apt/src/repositories/release.rs | 61 +++++++++++++++----------
 2 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/proxmox-apt/src/repositories/file.rs b/proxmox-apt/src/repositories/file.rs
index b4c6b08..bce7271 100644
--- a/proxmox-apt/src/repositories/file.rs
+++ b/proxmox-apt/src/repositories/file.rs
@@ -405,7 +405,7 @@ impl APTRepositoryFile {
                     add_info("warning", message_old(base_suite));
                 }
 
-                if Some(codename) == current_codename.next() {
+                if codename == current_codename.next() {
                     add_info("ignore-pre-upgrade-warning", message_new(base_suite));
                 } else if codename > current_codename {
                     add_info("warning", message_new(base_suite));
diff --git a/proxmox-apt/src/repositories/release.rs b/proxmox-apt/src/repositories/release.rs
index da391e5..43c9746 100644
--- a/proxmox-apt/src/repositories/release.rs
+++ b/proxmox-apt/src/repositories/release.rs
@@ -3,8 +3,17 @@ use std::io::{BufRead, BufReader};
 
 use anyhow::{bail, format_err, Error};
 
+mod private {
+    // public types in private modules are unnamable by external users, this is similar to the
+    // "sealed trait" pattern
+    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
+    pub struct Internal;
+}
+use private::Internal;
+
 /// The code names of Debian releases. Does not include `sid`.
 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+#[repr(u8)]
 pub enum DebianCodename {
     Lenny = 5,
     Squeeze,
@@ -15,13 +24,37 @@ pub enum DebianCodename {
     Bullseye,
     Bookworm,
     Trixie,
+    Unknown(u8, Internal),
 }
 
 impl DebianCodename {
-    pub fn next(&self) -> Option<Self> {
-        match (*self as u8 + 1).try_into() {
-            Ok(codename) => Some(codename),
-            Err(_) => None,
+    pub fn value(&self) -> u8 {
+        match self {
+            Self::Unknown(number, _) => *number,
+            // see 'arbitrary_enum_discriminant' feature for why this is safe:
+            // https://rust-lang.github.io/rfcs/2363-arbitrary-enum-discriminant.html
+            other => unsafe { *(other as *const Self as *const u8) },
+        }
+    }
+
+    pub fn next(&self) -> Self {
+        (self.value() + 1).into()
+    }
+}
+
+impl From<u8> for DebianCodename {
+    fn from(number: u8) -> Self {
+        match number {
+            5 => Self::Lenny,
+            6 => Self::Squeeze,
+            7 => Self::Wheezy,
+            8 => Self::Jessie,
+            9 => Self::Stretch,
+            10 => Self::Buster,
+            11 => Self::Bullseye,
+            12 => Self::Bookworm,
+            13 => Self::Trixie,
+            number => Self::Unknown(number, Internal),
         }
     }
 }
@@ -45,25 +78,6 @@ impl TryFrom<&str> for DebianCodename {
     }
 }
 
-impl TryFrom<u8> for DebianCodename {
-    type Error = Error;
-
-    fn try_from(number: u8) -> Result<Self, Error> {
-        match number {
-            5 => Ok(DebianCodename::Lenny),
-            6 => Ok(DebianCodename::Squeeze),
-            7 => Ok(DebianCodename::Wheezy),
-            8 => Ok(DebianCodename::Jessie),
-            9 => Ok(DebianCodename::Stretch),
-            10 => Ok(DebianCodename::Buster),
-            11 => Ok(DebianCodename::Bullseye),
-            12 => Ok(DebianCodename::Bookworm),
-            13 => Ok(DebianCodename::Trixie),
-            _ => bail!("unknown Debian release number '{}'", number),
-        }
-    }
-}
-
 impl Display for DebianCodename {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         match self {
@@ -76,6 +90,7 @@ impl Display for DebianCodename {
             DebianCodename::Bullseye => write!(f, "bullseye"),
             DebianCodename::Bookworm => write!(f, "bookworm"),
             DebianCodename::Trixie => write!(f, "trixie"),
+            DebianCodename::Unknown(_, _) => write!(f, "unknown"),
         }
     }
 }
-- 
2.30.2