[pdm-devel] [PATCH datacenter-manager v4 01/10] pdm-api-types: resources: add helper methods for fields

Dominik Csapak d.csapak at proxmox.com
Wed Sep 3 09:38:22 CEST 2025



On 9/2/25 4:29 PM, Wolfgang Bumiller wrote:
> On Thu, Aug 28, 2025 at 03:16:00PM +0200, Dominik Csapak wrote:
>> namely 'resource_type' and 'status'. All resources have those fields
>> in one way or another, so adding a helper that one does not have to
>> use `match` on every call site makes code there shorter.
>>
>> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
>> ---
>> no changes in v4
>>   lib/pdm-api-types/src/resource.rs | 27 +++++++++++++++++++++++++++
>>   1 file changed, 27 insertions(+)
>>
>> diff --git a/lib/pdm-api-types/src/resource.rs b/lib/pdm-api-types/src/resource.rs
>> index 6227855..0dfadd5 100644
>> --- a/lib/pdm-api-types/src/resource.rs
>> +++ b/lib/pdm-api-types/src/resource.rs
>> @@ -62,6 +62,33 @@ impl Resource {
>>               Resource::PbsDatastore(r) => r.name.as_str(),
>>           }
>>       }
>> +
>> +    pub fn resource_type(&self) -> &str {
> 
> ^ This may be nicer as simple `enum` with a `Display`/`FromStr`
> implementation.
> 

mhmm the only use i have of that currently would it make harder to use 
with a separate enum
(also we'd copy the type with `to_string()` once more)

e.g. i use

`resource.resource_type().starts_with(...)`

with a separate enum it would be

`resource.resource_type().to_string().starts_with(...)`

which is more to write and one string copy extra...

>> +        match self {
>> +            Resource::PveStorage(_) => "storage",
>> +            Resource::PveQemu(_) => "qemu",
>> +            Resource::PveLxc(_) => "lxc",
>> +            Resource::PveNode(_) | Resource::PbsNode(_) => "node",
>> +            Resource::PbsDatastore(_) => "datastore",
>> +        }
>> +    }
>> +
>> +    pub fn status(&self) -> &str {
>> +        match self {
>> +            Resource::PveStorage(r) => r.status.as_str(),
>> +            Resource::PveQemu(r) => r.status.as_str(),
>> +            Resource::PveLxc(r) => r.status.as_str(),
>> +            Resource::PveNode(r) => r.status.as_str(),
>> +            Resource::PbsNode(r) => {
>> +                if r.uptime > 0 {
>> +                    "online"
>> +                } else {
>> +                    "offline"
>> +                }
>> +            }
>> +            Resource::PbsDatastore(_) => "online",
>> +        }
>> +    }
>>   }
>>   
>>   #[api(
>> -- 
>> 2.47.2





More information about the pdm-devel mailing list