All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Vallejo <alejandro.vallejo@cloud.com>
To: xen-devel@lists.xenproject.org
Cc: Alejandro Vallejo <alejandro.vallejo@cloud.com>,
	Anthony PERARD <anthony.perard@vates.tech>,
	Teddy Astie <teddy.astie@vates.tech>,
	Yann Dirson <yann.dirson@vates.tech>
Subject: [RFC PATCH 04/25] tools/xenbindgen: Add a TOML spec reader
Date: Fri, 15 Nov 2024 11:51:33 +0000	[thread overview]
Message-ID: <20241115115200.2824-5-alejandro.vallejo@cloud.com> (raw)
In-Reply-To: <20241115115200.2824-1-alejandro.vallejo@cloud.com>

There isn't any deserialisation yet. It's mere plumbing.

Signed-off-by: Alejandro Vallejo <alejandro.vallejo@cloud.com>
---
 tools/rust/xenbindgen/src/main.rs | 28 ++++++++-
 tools/rust/xenbindgen/src/spec.rs | 96 +++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+), 3 deletions(-)
 create mode 100644 tools/rust/xenbindgen/src/spec.rs

diff --git a/tools/rust/xenbindgen/src/main.rs b/tools/rust/xenbindgen/src/main.rs
index 08fcf8fe4da6..497cf39d3bbd 100644
--- a/tools/rust/xenbindgen/src/main.rs
+++ b/tools/rust/xenbindgen/src/main.rs
@@ -2,14 +2,22 @@
 //! crafted TOML files. The format of these files follows the following
 //! rules.
 
+mod spec;
+
+use std::path::PathBuf;
+
 use clap::Parser;
 use env_logger::Env;
-use log::info;
+use log::{error, info};
 
 /// A CLI tool to generate struct definitions in several languages.
 #[derive(Parser, Debug)]
 #[command(version, about)]
-struct Cli;
+struct Cli {
+    /// Path to the input directory containing the hypercall specification
+    #[arg(short, long)]
+    indir: PathBuf,
+}
 
 fn main() {
     env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
@@ -17,5 +25,19 @@ fn main() {
     let cli = Cli::parse();
     info!("args: {:?}", cli);
 
-    todo!("read spec files and generate output files");
+    let _specification = match spec::Spec::new(&cli.indir) {
+        Ok(x) => x,
+        Err(spec::Error::Toml(x)) => {
+            error!("TOML parsing error:");
+            error!("{x:#?}");
+            std::process::exit(1);
+        }
+        Err(spec::Error::Io(x)) => {
+            error!("IO error:");
+            error!("{x:#?}");
+            std::process::exit(1);
+        }
+    };
+
+    todo!("generate output files");
 }
diff --git a/tools/rust/xenbindgen/src/spec.rs b/tools/rust/xenbindgen/src/spec.rs
new file mode 100644
index 000000000000..e69f7c78dc7a
--- /dev/null
+++ b/tools/rust/xenbindgen/src/spec.rs
@@ -0,0 +1,96 @@
+//! Specification descriptions
+//!
+//! The TOML files are not parsed by hand. This module provides a sort of
+//! schema for the TOML descriptions, in the sense that `serde` itself ensures
+//! every field is deserialised into its equivalent Rust structure or the
+//! deserialization procedure fails.
+//!
+//! If the specification is clearly invalid (i.e: missing fields) it'll scream
+//! in a rather obvious way.
+//!
+//! A special case is the `typ` field in the specifications is meant to have
+//! the format present in the specifications under `extra`. This allows `serde`
+//! to properly decode the type and match it with a variant of the [`Typ`] type
+//! with the payload landing in the payload of the variant itself.
+
+use std::{fs::read_to_string, path::Path};
+
+use log::{debug, info};
+
+/// A language-agnostic specification.
+#[derive(Debug, serde::Deserialize)]
+struct InFileDef;
+
+/// Description of an abstract output (i.e: `.rs`, `.h`, etc).
+///
+/// Contains every element of the ABI that needs representing.
+#[derive(Debug)]
+pub struct OutFileDef {
+    /// The name of the output file, without the final extension.
+    pub name: String,
+}
+
+impl OutFileDef {
+    /// Creates a new _output_ file description. Each [`OutFileDef`] is
+    /// associated with a number of [`InFileDef`] and holds the merged
+    /// contents described in all of them.
+    ///
+    /// # Errors
+    /// Fails if the TOML is invalid or on IO error.
+    pub fn new(name: String, dir: &Path) -> Result<Self, Error> {
+        info!("Reading {dir:?} to generate an output file");
+
+        let mut ret = Self { name };
+
+        for entry in from_ioerr(dir.read_dir())? {
+            let path = from_ioerr(entry)?.path();
+            debug!("Reading {:?} to generate outfile={}", path, ret.name);
+            let toml_str = from_ioerr(read_to_string(path))?;
+            let filedef: InFileDef = toml::from_str(&toml_str).map_err(Error::Toml)?;
+        }
+
+        Ok(ret)
+    }
+}
+
+/// Internal error type for every error spec parsing could encounter
+#[derive(Debug)]
+pub enum Error {
+    /// Wrapper around IO errors
+    Io(std::io::Error),
+    /// Wrapper around deserialization errors
+    Toml(toml::de::Error),
+}
+
+/// Maps an [`std::io::Error`] onto a [`Error`] type for easier propagation
+fn from_ioerr<T>(t: std::io::Result<T>) -> Result<T, Error> {
+    t.map_err(Error::Io)
+}
+
+/// Object containing the abstract definitions of all output files.
+///
+/// See [`OutFileDef`] to details on the specification contents of each output.
+#[derive(Debug)]
+pub struct Spec(pub Vec<OutFileDef>);
+
+impl Spec {
+    /// Creates a new abstract specification from a top-level directory full
+    /// of specification files. This is used later to aggregate all the content
+    /// and generate the appropriate language outputs.
+    ///
+    /// # Errors
+    /// Fails on IO errors.
+    pub fn new(root: &Path) -> Result<Self, Error> {
+        info!("Reading {root:?} as top-level directory");
+
+        let mut ret = Self(Vec::new());
+        for outfile in from_ioerr(root.read_dir())? {
+            // Each folder in the root defines a single output file
+            let outfile = from_ioerr(outfile)?;
+            let name = outfile.file_name().to_string_lossy().to_string();
+            ret.0.push(OutFileDef::new(name, &outfile.path())?);
+        }
+
+        Ok(ret)
+    }
+}
-- 
2.47.0



  parent reply	other threads:[~2024-11-15 11:53 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-15 11:51 [RFC PATCH 00/25] Introduce xenbindgen to autogen hypercall structs Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 01/25] xen/domctl: Refine grant_opts into max_grant_version Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 02/25] xen/domctl: Replace altp2m_opts with altp2m_mode Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 03/25] tools/xenbindgen: Introduce a Xen hypercall IDL generator Alejandro Vallejo
2024-11-15 11:51 ` Alejandro Vallejo [this message]
2024-11-25 15:13   ` [RFC PATCH 04/25] tools/xenbindgen: Add a TOML spec reader Teddy Astie
2024-11-25 16:51     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 05/25] tools/xenbindgen: Add basic plumbing for the C backend Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 06/25] tools/xenbindgen: Add xenbindgen's Cargo.lock file Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 07/25] tools/xenbindgen: Add support for structs in TOML specs Alejandro Vallejo
2024-11-25 12:39   ` Teddy Astie
2024-11-25 17:07     ` Alejandro Vallejo
2024-11-25 15:03   ` Teddy Astie
2024-11-25 17:16     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 08/25] tools/xenbindgen: Add support for enums " Alejandro Vallejo
2024-11-25 16:39   ` Teddy Astie
2024-11-25 17:18     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 09/25] tools/xenbindgen: Add support for bitmaps " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 10/25] tools/xenbindgen: Add support for includes in the " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 11/25] tools/xenbindgen: Validate ABI rules at generation time Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 12/25] xen: Replace sysctl/readconsole with autogenerated version Alejandro Vallejo
2024-11-25 12:05   ` Jan Beulich
2024-11-25 18:51     ` Alejandro Vallejo
2024-11-26  9:40       ` Jan Beulich
2024-11-26 12:27         ` Alejandro Vallejo
2024-11-26 13:20           ` Jan Beulich
2024-11-26 14:36             ` Alejandro Vallejo
2024-11-26 16:30               ` Jan Beulich
2024-11-26 14:39             ` Teddy Astie
2024-11-26 16:28               ` Jan Beulich
2024-11-15 11:51 ` [RFC PATCH 13/25] xen: Replace hand-crafted altp2m_mode descriptions with autogenerated ones Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 14/25] xen: Replace common bitmaps in domctl.createdomain with autogenerated versions Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 15/25] xen/arm: Replace hand-crafted xen_arch_domainconfig with autogenerated one Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 16/25] xen/x86: " Alejandro Vallejo
2024-11-25 12:09   ` Jan Beulich
2024-11-25 18:53     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 17/25] xen/ppc: Replace empty " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 18/25] xen/riscv: " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 19/25] xen: Replace hand-crafted domctl/createdomain with autogenerated version Alejandro Vallejo
2024-12-04 14:48   ` Teddy Astie
2024-11-15 11:51 ` [RFC PATCH 20/25] tools/xen-sys: Create a crate with autogenerated Rust constructs Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 21/25] tools/xenbindgen: Add Rust backend to xenbindgen Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 22/25] tools/xen-sys: Add autogenerated Rust files Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 23/25] licence: Add Unicode-DFS-2016 to the list of licences Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 24/25] tools/rust: Add deny.toml Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 25/25] ci: Add a CI checker for Rust-related helpful properties Alejandro Vallejo
2024-11-21 17:46 ` [RFC PATCH 00/25] Introduce xenbindgen to autogen hypercall structs Anthony PERARD
2024-11-22 10:52   ` Teddy Astie
2024-11-22 13:26     ` Alejandro Vallejo
2024-11-22 13:12   ` Alejandro Vallejo
2024-11-22 16:34     ` Anthony PERARD

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=20241115115200.2824-5-alejandro.vallejo@cloud.com \
    --to=alejandro.vallejo@cloud.com \
    --cc=anthony.perard@vates.tech \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    --cc=yann.dirson@vates.tech \
    /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.