From: Stefan Hanreich <s.hanreich@proxmox.com>
To: Wolfgang Bumiller <w.bumiller@proxmox.com>
Cc: pve-devel@lists.proxmox.com
Subject: Re: [pve-devel] [PATCH proxmox v3 2/2] network-types: add hostname type
Date: Fri, 4 Apr 2025 13:26:46 +0200 [thread overview]
Message-ID: <407c30ee-462b-4649-a415-a9f7a4c0c7b4@proxmox.com> (raw)
In-Reply-To: <77727bc6-1cac-433d-baa4-131232952d3d@proxmox.com>
On 4/4/25 09:51, Stefan Hanreich wrote:
>>> +///
>>> +/// It checks for the following conditions:
>>> +/// * At most 63 characters long.
>>> +/// * It must not start or end with a hyphen.
>>> +/// * Must only contain ASCII alphanumeric characters as well as hyphens.
>>> +/// * It must not be purely numerical.
>>> +#[derive(Debug, Deserialize, Serialize, Clone, Eq, Hash, PartialOrd, Ord, PartialEq)]
>>> +pub struct Hostname(String);
>>> +
>>> +impl std::str::FromStr for Hostname {
>>> + type Err = HostnameError;
>>> +
>>> + fn from_str(hostname: &str) -> Result<Self, Self::Err> {
>>> + Self::new(hostname)
>>> + }
>>> +}
>>> +
>>> +impl AsRef<str> for Hostname {
>>> + fn as_ref(&self) -> &str {
>>> + &self.0
>>> + }
>>> +}
>>> +
>>> +impl Display for Hostname {
>>> + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
>>> + self.0.fmt(f)
>>> + }
>>> +}
>>> +
>>> +impl Hostname {
>>> + /// Constructs a new hostname from a string
>>> + ///
>>> + /// This function accepts characters in any case, but the resulting hostname will be
>>> + /// lowercased.
>>> + pub fn new(name_ref: impl AsRef<str>) -> Result<Self, HostnameError> {
>>
>> Nit: I'd recommend using a `check()` function which does not create the
>> `Hostname` itself, because then:
>>
>> - in `FromStr` we know we have a reference (&str) and need to clone.
>> - We could add a `TryFrom<&str>` wich just uses `.parse()`
>> - We could add a `TryFrom<String>` which avoids the clone.
What about a constructor that just takes String (if a function needs to
own the value it should demand a String anyway) and then calling that
constructor from the proposed trait implementations? Maybe something
more generic than String as a param, but that could usually be tacked on
later without breaking the API.
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2025-04-04 11:27 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-01 14:52 [pve-devel] [PATCH proxmox v3 1/2] network-types: initial commit Stefan Hanreich
2025-04-01 14:52 ` [pve-devel] [PATCH proxmox v3 2/2] network-types: add hostname type Stefan Hanreich
2025-04-04 7:31 ` Wolfgang Bumiller
2025-04-04 7:51 ` Stefan Hanreich
2025-04-04 9:22 ` Wolfgang Bumiller
2025-04-04 11:26 ` Stefan Hanreich [this message]
2025-04-04 11:41 ` Wolfgang Bumiller
2025-04-01 14:52 ` [pve-devel] [PATCH proxmox-ve-rs v3 1/1] ve-config: move types to proxmox-network-types Stefan Hanreich
2025-04-01 14:52 ` [pve-devel] [PATCH proxmox-firewall v3 1/1] firewall: nftables: migrate " Stefan Hanreich
2025-04-02 9:06 ` [pve-devel] [PATCH proxmox v3 1/2] network-types: initial commit Christoph Heiss
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=407c30ee-462b-4649-a415-a9f7a4c0c7b4@proxmox.com \
--to=s.hanreich@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
--cc=w.bumiller@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