From: Hannes Laimer <h.laimer@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH proxmox-yew-comp 1/4] form: add helpers for extractig data out of schemas
Date: Thu, 30 Oct 2025 15:33:59 +0100	[thread overview]
Message-ID: <20251030143406.193744-7-h.laimer@proxmox.com> (raw)
In-Reply-To: <20251030143406.193744-1-h.laimer@proxmox.com>
Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 src/form/mod.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
diff --git a/src/form/mod.rs b/src/form/mod.rs
index 0c23053..4e17d5b 100644
--- a/src/form/mod.rs
+++ b/src/form/mod.rs
@@ -74,6 +74,76 @@ pub fn typed_load<T: DeserializeOwned + Serialize>(
     .url(url_cloned)
 }
 
+/// Get a nested field schema from a parent schema by traversing a path
+///
+/// This function recursively traverses through Object schemas and PropertyString schemas
+/// to find the nested field schema at the given path.
+///
+/// # Arguments
+/// * `s` - The parent schema to traverse
+/// * `field` - A vector of field names representing the path to traverse
+///
+/// # Returns
+/// The schema at the specified path, or the input schema if the path cannot be traversed
+pub fn get_field_schema(s: &'static Schema, mut field: Vec<&str>) -> &'static Schema {
+    let Some(looking_for) = field.first() else {
+        return s;
+    };
+
+    if let Schema::Object(s) = s {
+        for (name, _, ss) in s.properties() {
+            if *looking_for == *name {
+                field.remove(0);
+                return get_field_schema(ss, field);
+            }
+        }
+    }
+    if let Schema::String(s) = s {
+        if let Some(proxmox_schema::ApiStringFormat::PropertyString(pss)) = s.format {
+            return get_field_schema(pss, field);
+        }
+    }
+    s
+}
+
+/// Extract the the placeholder for a fields from a schema
+pub fn placeholder_from_schema(schema: &'static proxmox_schema::Schema) -> String {
+    if let proxmox_schema::Schema::String(s) = schema {
+        if let Some(v) = s.default {
+            return v.to_string();
+        }
+    }
+    if let proxmox_schema::Schema::Integer(s) = schema {
+        if let Some(v) = s.default {
+            return v.to_string();
+        }
+    }
+    if let proxmox_schema::Schema::Number(s) = schema {
+        if let Some(v) = s.default {
+            return v.to_string();
+        }
+    }
+    "".to_string()
+}
+
+/// Extract the enum varian items from a schema
+///
+/// Can be used to populate items for a combobox
+pub fn enum_items_from_schema(
+    schema: &'static proxmox_schema::Schema,
+) -> Vec<pwt::prelude::AttrValue> {
+    if let proxmox_schema::Schema::String(s) = schema {
+        if let Some(proxmox_schema::ApiStringFormat::Enum(e)) = s.format {
+            let items: Vec<pwt::prelude::AttrValue> = e
+                .iter()
+                .map(|entry| entry.value.to_string().into())
+                .collect();
+            return items;
+        }
+    }
+    vec![]
+}
+
 /// Convert a property string to separate properties
 ///
 /// This is useful for use in an [`crate::PropertyEditDialog`] when editing parts of a property string.
-- 
2.47.3
_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel
next prev parent reply	other threads:[~2025-10-30 14:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-30 14:33 [pdm-devel] [PATCH proxmox{, -yew-comp, -datacenter-manager} 00/13] add basic integration of PVE firewall Hannes Laimer
2025-10-30 14:33 ` [pdm-devel] [PATCH proxmox 1/5] pve-api-types: update pve-api.json Hannes Laimer
2025-10-30 14:33 ` [pdm-devel] [PATCH proxmox 2/5] pve-api-types: add get/update firewall options endpoints Hannes Laimer
2025-10-30 14:33 ` [pdm-devel] [PATCH proxmox 3/5] pve-api-types: schema2rust: handle `macro` keyword like we do `type` Hannes Laimer
2025-10-30 14:33 ` [pdm-devel] [PATCH proxmox 4/5] pve-api-types: add list firewall rules endpoints Hannes Laimer
2025-10-30 14:33 ` [pdm-devel] [PATCH proxmox 5/5] pve-api-types: regenerate Hannes Laimer
2025-10-30 14:33 ` Hannes Laimer [this message]
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-yew-comp 2/4] firewall: add FirewallContext Hannes Laimer
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-yew-comp 3/4] firewall: add options edit form Hannes Laimer
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-yew-comp 4/4] firewall: add rules table Hannes Laimer
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-datacenter-manager 1/4] pdm-api-types: add firewall status types Hannes Laimer
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-datacenter-manager 2/4] api: firewall: add option, rules and status endpoints Hannes Laimer
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-datacenter-manager 3/4] pdm-client: add api methods for firewall options, " Hannes Laimer
2025-10-30 14:34 ` [pdm-devel] [PATCH proxmox-datacenter-manager 4/4] ui: add firewall status tree Hannes Laimer
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=20251030143406.193744-7-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pdm-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.