diff options
Diffstat (limited to 'users/wpcarro/emacs/pkgs/struct')
-rw-r--r-- | users/wpcarro/emacs/pkgs/struct/README.md | 6 | ||||
-rw-r--r-- | users/wpcarro/emacs/pkgs/struct/default.nix | 29 | ||||
-rw-r--r-- | users/wpcarro/emacs/pkgs/struct/struct.el | 65 | ||||
-rw-r--r-- | users/wpcarro/emacs/pkgs/struct/tests.el | 44 |
4 files changed, 144 insertions, 0 deletions
diff --git a/users/wpcarro/emacs/pkgs/struct/README.md b/users/wpcarro/emacs/pkgs/struct/README.md new file mode 100644 index 000000000000..34dac6614cd1 --- /dev/null +++ b/users/wpcarro/emacs/pkgs/struct/README.md @@ -0,0 +1,6 @@ +# struct.el + +[![Build status](https://badge.buildkite.com/016bff4b8ae2704a3bbbb0a250784e6692007c582983b6dea7.svg?branch=refs/heads/canon)](https://buildkite.com/tvl/depot) + +Provides new macros exposing immutable and mutable interfaces for working with +structs in Elisp. diff --git a/users/wpcarro/emacs/pkgs/struct/default.nix b/users/wpcarro/emacs/pkgs/struct/default.nix new file mode 100644 index 000000000000..558ebd0a3d20 --- /dev/null +++ b/users/wpcarro/emacs/pkgs/struct/default.nix @@ -0,0 +1,29 @@ +{ pkgs, depot, ... }: + +let + struct = pkgs.callPackage + ({ emacsPackages }: + emacsPackages.trivialBuild { + pname = "struct"; + version = "1.0.0"; + src = ./struct.el; + packageRequires = [ ]; + }) + { }; + + emacs = (pkgs.emacsPackagesFor pkgs.emacs28).emacsWithPackages (epkgs: [ + struct + ]); +in +struct.overrideAttrs (_old: { + doCheck = true; + checkPhase = '' + ${emacs}/bin/emacs -batch \ + -l ert -l ${./tests.el} -f ert-run-tests-batch-and-exit + ''; + passthru.meta.ci.extraSteps.github = depot.tools.releases.filteredGitPush { + filter = ":/users/wpcarro/emacs/pkgs/struct"; + remote = "git@github.com:wpcarro/struct.el.git"; + ref = "refs/heads/canon"; + }; +}) diff --git a/users/wpcarro/emacs/pkgs/struct/struct.el b/users/wpcarro/emacs/pkgs/struct/struct.el new file mode 100644 index 000000000000..5d6572bf6dde --- /dev/null +++ b/users/wpcarro/emacs/pkgs/struct/struct.el @@ -0,0 +1,65 @@ +;;; struct.el --- Helpers for working with structs -*- lexical-binding: t -*- + +;; Author: William Carroll <wpcarro@gmail.com> +;; Version: 1.0.0 +;; Package-Requires: ((emacs "24.3")) + +;;; Commentary: +;; Provides new macros for working with structs. Also provides adapter +;; interfaces to existing struct macros, that should have more intuitive +;; interfaces. +;; +;; Sometimes `setf' just isn't enough. + +;;; Code: + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Library +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defmacro struct-update (type field f xs) + "Apply F to FIELD in XS, which is a struct of TYPE. +This is immutable." + (let ((copier (struct--copier-for type)) + (accessor (struct--accessor-for type field))) + `(let ((copy (,copier ,xs))) + (setf (,accessor copy) (funcall ,f (,accessor copy))) + copy))) + +(defmacro struct-update! (type field f xs) + "Mutably apply F to FIELD in XS." + (let ((accessor (struct--accessor-for type field))) + `(progn + (setf (,accessor ,xs) (funcall ,f (,accessor ,xs))) + ,xs))) + +(defmacro struct-set (type field x xs) + "Immutably set FIELD in XS (struct TYPE) to X." + (let ((copier (struct--copier-for type)) + (accessor (struct--accessor-for type field))) + `(let ((copy (,copier ,xs))) + (setf (,accessor copy) ,x) + copy))) + +(defmacro struct-set! (type field x xs) + "Set FIELD in XS (struct TYPE) to X mutably. +This is an adapter interface to `setf'." + (let ((accessor (struct--accessor-for type field))) + `(progn + (setf (,accessor ,xs) ,x) + ,xs))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Helper Functions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defun struct--copier-for (type) + (intern (format "copy-%s" (symbol-name type)))) + +(defun struct--accessor-for (type field) + (intern (format "%s-%s" + (symbol-name type) + (symbol-name field)))) + +(provide 'struct) +;;; struct.el ends here diff --git a/users/wpcarro/emacs/pkgs/struct/tests.el b/users/wpcarro/emacs/pkgs/struct/tests.el new file mode 100644 index 000000000000..a7ddb52c46d6 --- /dev/null +++ b/users/wpcarro/emacs/pkgs/struct/tests.el @@ -0,0 +1,44 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Dependencies +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(require 'ert) +(require 'struct) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Tests +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(cl-defstruct dummy name age) + +(ert-deftest struct-update () + (let* ((test (make-dummy :name "Roofus" :age 19)) + (result (struct-update dummy name #'upcase test))) + ;; test + (should (string= "Roofus" (dummy-name test))) + (should (= 19 (dummy-age test))) + ;; result + (should (string= "ROOFUS" (dummy-name result))) + (should (= 19 (dummy-age result))))) + +(ert-deftest struct-update! () + (let ((test (make-dummy :name "Roofus" :age 19))) + (struct-update! dummy name #'upcase test) + (should (string= "ROOFUS" (dummy-name test))) + (should (= 19 (dummy-age test))))) + +(ert-deftest struct-set () + (let* ((test (make-dummy :name "Roofus" :age 19)) + (result (struct-set dummy name "Shoofus" test))) + ;; test + (should (string= "Roofus" (dummy-name test))) + (should (= 19 (dummy-age test))) + ;; result + (should (string= "Shoofus" (dummy-name result))) + (should (= 19 (dummy-age result))))) + +(ert-deftest struct-set! () + (let ((test (make-dummy :name "Roofus" :age 19))) + (struct-set! dummy name "Doofus" test) + (should (string= "Doofus" (dummy-name test))) + (should (= 19 (dummy-age test))))) |