UP | HOME

Cleopatra’s Library of Babel

Table of Contents

(defun top-tail (str)
  (let ((len (length str)))
    (substring str 1 (- len 1))))

1 Miscellaneous

"1.0.0-dev"

2 Rust

2.1 Generating a Crate’s List of Dependencies

nom "5" Parsing something with style
clap "*" Dealing with command-line arguments

generates the following TOML entry.

[dependencies]
nom = "5"
clap = "*"
(defun top-tail (str)
  (let ((len (length str)))
    (substring str 1 (- len 1))))

(format
 "[dependencies]\n%s"
 (mapconcat (lambda (dep)
              (format "%s = %s"
                      (top-tail (nth 0 dep))
                      (top-tail (nth 1 dep))))
            deps
            "\n"))

2.2 Generating a Rust Structure

  • Serialize
  • Deserialize
field1 isize Some integer because why not serde(default)
field2 PathBuf Let’s be parametric  

generates the following Rust struct

pub struct Test {
    /// Some integer because why not
    #[serde(default)]
    pub field1 : isize,
    /// Let’s be parametric
    pub field2 : PathBuf,
}
(defun top-tail (str)
  (let ((len (length str)))
    (substring str 1 (- len 1))))

(format
 "pub struct %s%s {\n%s\n}"
 name
 type-args
 (mapconcat
  (lambda (field)
    (let* ((name (top-tail (nth 0 field)))
           (type (top-tail (nth 1 field)))
           (docs-maybe (nth 2 field))
           (docs (if (string= docs-maybe "") ""
                   (format "/// %s\n    " docs-maybe)))
           (attrs-maybe (nth 3 field))
           (attrs (if (string= attrs-maybe "") ""
                    (format "#[%s]\n    " (top-tail attrs-maybe)))))
      (format "    %s%spub %s : %s," docs attrs name type)))
  fields
  "\n"))
ConfigurationParsing PathBuf, toml::de::Error There is a parsing error inside a configuration file
ConfigurationNotFound   There is no configuration file neither in current directory nor in any of its parents
Anomaly String Something else happened, that was not supposed to
(defun top-tail (str)
  (let ((len (length str)))
    (substring str 1 (- len 1))))

(format
 "pub enum %s%s {\n%s\n}"
 name
 type-args
 (mapconcat
  (lambda (variant)
    (let* ((name (top-tail (nth 0 variant)))
           (type-maybe (nth 1 variant))
           (type (if (string= type-maybe "") ""
                   (format "(%s)" (top-tail type-maybe))))
           (docs-maybe (nth 2 variant))
           (docs (if (string= docs-maybe "") ""
                   (format "/// %s\n    " docs-maybe))))
      (format "    %s%s%s," docs name type)))
  variants
  "\n"))

Author: Thomas Letan

Created: 2020-04-05 Sun 13:56

Validate