[pdm-devel] [PATCH proxmox v2 02/14] network-types: move `Fqdn` type from proxmox-installer-common
Christoph Heiss
c.heiss at proxmox.com
Tue Dec 9 13:26:36 CET 2025
Thanks for the review!
On Tue Dec 9, 2025 at 10:13 AM CET, Lukas Wagner wrote:
> Looks good in general, I assume this was reviewed before this it was
> moved from somewhere else.
>
> One note inline.
>
> Reviewed-by: Lukas Wagner <l.wagner at proxmox.com>
>
> On Fri Dec 5, 2025 at 12:25 PM CET, Christoph Heiss wrote:
[..]
>> +impl Fqdn {
>> + /// Maximum length of a single label of the FQDN
>> + const MAX_LABEL_LENGTH: usize = 63;
>> + /// Maximum total length of the FQDN
>> + const MAX_LENGTH: usize = 253;
>> +
>> + pub fn from(fqdn: &str) -> Result<Self, FqdnParseError> {
>> + if fqdn.len() > Self::MAX_LENGTH {
>> + return Err(FqdnParseError::TooLong(fqdn.len()));
>> + }
>> +
>> + let parts = fqdn
>> + .split('.')
>> + .map(ToOwned::to_owned)
>> + .collect::<Vec<String>>();
>> +
>> + for part in &parts {
>> + if !Self::validate_single(part) {
>> + return Err(FqdnParseError::InvalidPart(part.clone()));
>> + }
>> + }
>> +
>> + if parts.len() < 2 {
>> + Err(FqdnParseError::MissingHostname)
>
> (a)
>
> Since `Fqdn::from` seems to be the only way to instantiate the Fqdn
> type, and since the parts.len() < 2 establishes the invariant that the
> number of parts can only ever be >= 2,
>
>> + } else if parts[0].chars().all(|c| c.is_ascii_digit()) {
>> + // Do not allow a purely numeric hostname, see:
>> + // https://bugzilla.proxmox.com/show_bug.cgi?id=1054
>> + Err(FqdnParseError::NumericHostname)
>> + } else {
>> + Ok(Self { parts })
>> + }
>> + }
>> +
>> + pub fn host(&self) -> Option<&str> {
>> + self.has_host().then_some(&self.parts[0])
>> + }
>
> (c)
>
> ... therefore you could assume that it is safe to just access
> self.parts[0] and avoid the `Option<..>` in the return type?
>
> I'm always on board with being careful, but since this type is
> relatively small, I think it would be reasonable to rely on the
> invariant, which makes the call sites a bit nicer since it does not have
> to handle the Option.
>
> What do you think?
That seems very sensible, thanks for reasoning about this! The given
invariant is also already established by the current unit tests
below, so fine from my side too.
Less `Option`s to handle at callsites is always a nice improvement IMO.
I will change it for v2, it's a good chance to improve upon things.
>
>> +
>> + pub fn domain(&self) -> String {
>> + let parts = if self.has_host() {
>> + &self.parts[1..]
>> + } else {
>> + &self.parts
>> + };
>> +
>> + parts.join(".")
>> + }
>> +
>> + /// Checks whether the FQDN has a hostname associated with it, i.e. is has more than 1 part.
>> + fn has_host(&self) -> bool {
>> + self.parts.len() > 1
>> + }
>
> (b)
>
> ... which means that this should always return true, ...
>
More information about the pdm-devel
mailing list