public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: Proxmox Backup Server development discussion
	<pbs-devel@lists.proxmox.com>,
	 Gabriel Goller <g.goller@proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox-backup 2/4] api types: introduce `BackupArchiveName` type
Date: Tue, 9 Jul 2024 14:09:32 +0200 (CEST)	[thread overview]
Message-ID: <210562830.1744.1720526972589@webmail.proxmox.com> (raw)
In-Reply-To: <20240709093737.zvwlhvneuhxlxcrw@luna.proxmox.com>

> On 09.07.2024 11:37 CEST Gabriel Goller <g.goller@proxmox.com> wrote:
> 
>  
> Doesn't apply on master anymore, but here some quick things I noticed
> skimming over.
> 
> On 04.07.2024 10:55, Christian Ebner wrote:
> >diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs
> >index b63e7c2ff..16c3eef59 100644
> >--- a/pbs-api-types/src/datastore.rs
> >+++ b/pbs-api-types/src/datastore.rs
> >+impl std::fmt::Display for BackupArchiveName {
> 
> I think we can import the std::* traits here (and below as well.)

Okay, fine by me, will use the already imported fmt module for traits located in there instead, to be in line with the current code and import other traits at the beginning.

> 
> >+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
> >+        write!(f, "{name}", name = self.name)
> >+    }
> >+}
> >+
> >+serde_plain::derive_deserialize_from_fromstr!(BackupArchiveName, "archive name");
> >+
> >+impl std::str::FromStr for BackupArchiveName {
> >+    type Err = Error;
> >+
> >+    fn from_str(name: &str) -> Result<Self, Self::Err> {
> >+        Self::try_from(name)
> >+    }
> >+}
> >+
> >+serde_plain::derive_serialize_from_display!(BackupArchiveName);
> >+
> >+impl std::convert::TryFrom<&str> for BackupArchiveName {
> >+    type Error = anyhow::Error;
> >+
> >+    fn try_from(value: &str) -> Result<Self, Self::Error> {
> >+        let (name, ty) = Self::parse_archive_type(value)?;
> >+        Ok(Self { name, ty })
> >+    }
> >+}
> >+
> >+impl std::convert::AsRef<str> for BackupArchiveName {
> >+    fn as_ref(&self) -> &str {
> >+        &self.name
> >+    }
> >+}
> >+
> >+impl BackupArchiveName {
> >+    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
> >+        let path = path.as_ref();
> >+        let file_name = path
> >+            .file_name()
> >+            .ok_or_else(|| format_err!("invalid archive name"))?;
> >+        let name = file_name
> >+            .to_str()
> >+            .ok_or_else(|| format_err!("archive name not valid UTF-8"))?;
> >+
> >+        Self::try_from(name)
> >+    }
> >+
> >+    pub fn archive_type(&self) -> ArchiveType {
> >+        self.ty.clone()
> >+    }
> >+
> >+    pub fn ends_with(&self, postfix: &str) -> bool {
> >+        self.name.ends_with(postfix)
> >+    }
> >+
> >+    pub fn has_pxar_filename_extension(&self) -> bool {
> >+        self.name.ends_with(".pxar.didx")
> >+            || self.name.ends_with(".mpxar.didx")
> >+            || self.name.ends_with(".ppxar.didx")
> >+    }
> >+
> >+    pub fn without_type_extension(&self) -> String {
> >+        match self.ty {
> >+            ArchiveType::DynamicIndex => self.name.strip_suffix(".didx").unwrap().into(),
> >+            ArchiveType::FixedIndex => self.name.strip_suffix(".fidx").unwrap().into(),
> >+            ArchiveType::Blob => self.name.strip_suffix(".blob").unwrap().into(),
> >+        }
> >+    }
> >+
> >+    fn parse_archive_type(archive_name: &str) -> Result<(String, ArchiveType), Error> {
> >+        if archive_name.ends_with(".didx")
> >+            || archive_name.ends_with(".fidx")
> >+            || archive_name.ends_with(".blob")
> >+        {
> >+            Ok((archive_name.into(), ArchiveType::from_path(archive_name)?))
> >+        } else if archive_name.ends_with(".pxar")
> >+            || archive_name.ends_with(".mpxar")
> >+            || archive_name.ends_with(".ppxar")
> >+        {
> >+            Ok((format!("{}.didx", archive_name), ArchiveType::DynamicIndex))
> 
> `archive_name` can be inlined here (below as well.)

Will be inlined in v2.

> 
> >+        } else if archive_name.ends_with(".img") {
> >+            Ok((format!("{}.fidx", archive_name), ArchiveType::FixedIndex))
> >+        } else {
> >+            Ok((format!("{}.blob", archive_name), ArchiveType::Blob))
> >+        }
> >+    }
> >+}
> >+
> >+impl ApiType for BackupArchiveName {
> >+    const API_SCHEMA: Schema = BACKUP_ARCHIVE_NAME_SCHEMA;
> >+}
> 
> Everything quite straightforward, but we could throw in some unit-tests
> as it won't do us any harm.

Sure, will add tests covering the relevant cases.


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  reply	other threads:[~2024-07-09 12:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-04  8:55 [pbs-devel] [PATCH proxmox-backup 0/4] introduce dedcated archive name api type Christian Ebner
2024-07-04  8:55 ` [pbs-devel] [PATCH proxmox-backup 1/4] datastore: move `ArchiveType` to PBS api types crate Christian Ebner
2024-07-04  8:55 ` [pbs-devel] [PATCH proxmox-backup 2/4] api types: introduce `BackupArchiveName` type Christian Ebner
2024-07-09  9:37   ` Gabriel Goller
2024-07-09 12:09     ` Christian Ebner [this message]
2024-07-04  8:55 ` [pbs-devel] [PATCH proxmox-backup 3/4] client/server: use dedicated api type for all archive names Christian Ebner
2024-07-04  8:55 ` [pbs-devel] [PATCH proxmox-backup 4/4] client: drop unused parse_archive_type helper Christian Ebner
2024-07-10  7:51 ` [pbs-devel] [PATCH proxmox-backup 0/4] introduce dedcated archive name api type Christian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=210562830.1744.1720526972589@webmail.proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=g.goller@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal