Browse code
Rename pick->dive add swank utils
Showing 4 changed files
... | ... |
@@ -1,16 +1,6 @@ |
1 | 1 |
;;;; fwoar-lisputils.asd |
2 | 2 |
(in-package :asdf-user) |
3 | 3 |
|
4 |
-(defsystem #:fwoar-lisputils/string-utils |
|
5 |
- :description "A binary parser" |
|
6 |
- :author "fiddlerwoaroof <fiddlerwoaroof@gmail.com" |
|
7 |
- :license "MIT" |
|
8 |
- :depends-on (#:should-test) |
|
9 |
- :components ((:file "string-utils/package") |
|
10 |
- (:file "string-utils/string-utils" :depends-on ("string-utils/package")) |
|
11 |
- #-lispworks |
|
12 |
- (:file "string-utils/test" :depends-on ("string-utils/string-utils")))) |
|
13 |
- |
|
14 | 4 |
(asdf:defsystem #:fwoar-lisputils |
15 | 5 |
:description "Some utilities common to other libraries I'm writing" |
16 | 6 |
:author "fiddlerwoaroof <fiddlerwoaroof@gmail.com" |
... | ... |
@@ -40,7 +30,31 @@ |
40 | 30 |
(:file "non-lispworks") |
41 | 31 |
#-lispworks |
42 | 32 |
(:file "patmatch") |
43 |
- (:file "glambda"))) |
|
33 |
+ (:file "glambda") |
|
34 |
+ (:file "misc"))) |
|
35 |
+ |
|
36 |
+(defsystem #:fwoar-lisputils/string-utils |
|
37 |
+ :description "A string splitter" |
|
38 |
+ :author "fiddlerwoaroof <fiddlerwoaroof@gmail.com" |
|
39 |
+ :license "MIT" |
|
40 |
+ :depends-on (#:should-test) |
|
41 |
+ :components ((:file "string-utils/package") |
|
42 |
+ (:file "string-utils/string-utils" :depends-on ("string-utils/package")) |
|
43 |
+ #-lispworks |
|
44 |
+ (:file "string-utils/test" :depends-on ("string-utils/string-utils")))) |
|
45 |
+ |
|
46 |
+(asdf:defsystem #:fwoar-lisputils/swank-utils |
|
47 |
+ :description "Utilities for use with swank" |
|
48 |
+ :author "fiddlerwoaroof <fiddlerwoaroof@gmail.com" |
|
49 |
+ :license "MIT" |
|
50 |
+ :serial t |
|
51 |
+ :perform (test-op (o s) |
|
52 |
+ (funcall (intern "TEST" :should-test) |
|
53 |
+ :package :fwoar.string-utils)) |
|
54 |
+ :depends-on (#:fwoar-lisputils |
|
55 |
+ #:yason |
|
56 |
+ #:swank) |
|
57 |
+ :components ((:file "swank-utils"))) |
|
44 | 58 |
|
45 | 59 |
(defsystem #:fwoar-lisputils/bin-parser |
46 | 60 |
:description "A binary parser" |
... | ... |
@@ -15,26 +15,45 @@ |
15 | 15 |
;; (pick '("a" "b" "c") |
16 | 16 |
;; {}) |
17 | 17 |
;; =>> nil nil {} nil |
18 |
+(define-condition deprecation-warning (warning) |
|
19 |
+ ((%symbol :initarg :symbol :reader dw-symbol :initform (error "symbol required")) |
|
20 |
+ (%type :initarg :type :reader dw-type :initform (error "type required")) |
|
21 |
+ (%replacement :initarg :replacement :reader dw-replacement :initform (error "replacement required")))) |
|
18 | 22 |
|
23 |
+(defmethod print-object ((x deprecation-warning) stream) |
|
24 |
+ (if *print-escape* |
|
25 |
+ (call-next-method) |
|
26 |
+ (let ((symbol (dw-symbol x)) |
|
27 |
+ (replacement (dw-replacement x))) |
|
28 |
+ (format stream "~(~a~) named ~a:~a is deprecated, use ~a:~a instead" |
|
29 |
+ (dw-type x) |
|
30 |
+ (package-name (symbol-package symbol)) |
|
31 |
+ symbol |
|
32 |
+ (package-name (symbol-package replacement)) |
|
33 |
+ replacement)))) |
|
19 | 34 |
|
20 | 35 |
(defun pick (keys h-t &optional default) |
36 |
+ (warn 'deprecation-warning :symbol 'pick :type :function :replacement 'dive) |
|
37 |
+ (dive keys h-t default)) |
|
38 |
+ |
|
39 |
+(defun dive (keys h-t &optional default) |
|
21 | 40 |
"(PICK KEYS H-T) => (values result found last-value last-key) |
22 | 41 |
result if all keys found, otherwise (or default nil) |
23 | 42 |
last-value the value associated with last-key or H-T if no key matches |
24 | 43 |
last-key the last key to match, or nil |
25 | 44 |
found-p nil if all keys didn't match otherwise truthy" |
26 | 45 |
(let ((result default) (found nil) (last-value h-t) (last-key nil) |
27 |
- (matched-keys 0) (key-count 0)) |
|
46 |
+ (matched-keys 0) (key-count 0)) |
|
28 | 47 |
(dolist (key keys) |
29 | 48 |
(incf key-count) |
30 | 49 |
(if (hash-table-p last-value) |
31 |
- (multiple-value-bind (next-value next-found) (gethash key last-value) |
|
32 |
- (setf found next-found) |
|
33 |
- (when next-found |
|
34 |
- (incf matched-keys) |
|
35 |
- (setf last-key key |
|
36 |
- last-value next-value))) |
|
37 |
- (setf found nil))) |
|
50 |
+ (multiple-value-bind (next-value next-found) (gethash key last-value) |
|
51 |
+ (setf found next-found) |
|
52 |
+ (when next-found |
|
53 |
+ (incf matched-keys) |
|
54 |
+ (setf last-key key |
|
55 |
+ last-value next-value))) |
|
56 |
+ (setf found nil))) |
|
38 | 57 |
(when (= matched-keys key-count) |
39 | 58 |
(setf result last-value)) |
40 | 59 |
(values result found last-value last-key))) |
39 | 40 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,19 @@ |
1 |
+(defpackage :fwoar.swank-utils |
|
2 |
+ #+fw.dev |
|
3 |
+ (:nicknames fw.su) |
|
4 |
+ (:use :cl ) |
|
5 |
+ (:export |
|
6 |
+ #:log-json)) |
|
7 |
+(in-package :fwoar.swank-utils) |
|
8 |
+ |
|
9 |
+(defvar *target-identifier* (alexandria:make-keyword (gensym "JSON"))) |
|
10 |
+(defun log-json (obj &optional (indent t) (target-identifier *target-identifier*)) |
|
11 |
+ (let* ((buffer-stream (swank-buffer-streams:make-buffer-output-stream target-identifier)) |
|
12 |
+ (stream (yason:make-json-output-stream buffer-stream :indent indent))) |
|
13 |
+ (unwind-protect (progn (fresh-line buffer-stream) |
|
14 |
+ (values (yason:encode obj stream) |
|
15 |
+ target-identifier)) |
|
16 |
+ (terpri buffer-stream) |
|
17 |
+ (finish-output stream) |
|
18 |
+ (finish-output buffer-stream) |
|
19 |
+ (close stream)))) |