Browse code
Various changes
Ed Langley authored on 22/10/2018 06:36:04
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -1,9 +1,9 @@ |
1 | 1 |
;;;; araneus.asd |
2 | 2 |
|
3 | 3 |
(asdf:defsystem #:araneus |
4 |
- :description "Describe araneus here" |
|
5 |
- :author "Your Name <your.name@example.com>" |
|
6 |
- :license "Specify license here" |
|
4 |
+ :description "Another web framework" |
|
5 |
+ :author "Edward Langley <el@elangley.org>" |
|
6 |
+ :license "MIT" |
|
7 | 7 |
:depends-on (#:anaphora |
8 | 8 |
#:alexandria |
9 | 9 |
#:serapeum |
10 | 10 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,76 @@ |
1 |
+(in-package :araneus.routes) |
|
2 |
+ |
|
3 |
+(defgeneric resolve-route (url method content-type &key headers) |
|
4 |
+ (:documentation "The most generic method for resolving a route. |
|
5 |
+ |
|
6 |
+ Specialize with a keyword eql-specializer for method or content-type |
|
7 |
+ to define a particular understanding of either")) |
|
8 |
+ |
|
9 |
+(defgeneric match-token (url token) |
|
10 |
+ (:documentation "Match a url against a given token.")) |
|
11 |
+ |
|
12 |
+(defclass match-tree () |
|
13 |
+ ((-token :initarg :token :accessor token) |
|
14 |
+ (-children :accessor children :initform (make-array 5 :adjustable t :fill-pointer 0)) |
|
15 |
+ (-name :initarg :name :accessor name :initform nil))) |
|
16 |
+ |
|
17 |
+ |
|
18 |
+ |
|
19 |
+(defun token= (tok1 tok2) |
|
20 |
+ (if (typep tok1 'match-tree) |
|
21 |
+ (setf tok1 (token tok1))) |
|
22 |
+ |
|
23 |
+ (if (typep tok2 'match-tree) |
|
24 |
+ (setf tok2 (token tok2))) |
|
25 |
+ |
|
26 |
+ (equal tok1 tok2)) |
|
27 |
+ |
|
28 |
+(defgeneric add-to-tree (match-tree token) |
|
29 |
+ (:method ((match-tree match-tree) (tokens list)) |
|
30 |
+ (loop with current = match-tree |
|
31 |
+ for token in tokens |
|
32 |
+ do (setf current (add-to-tree current token)))) |
|
33 |
+ |
|
34 |
+ (:method ((match-tree match-tree) (token integer)) |
|
35 |
+ (with-accessors ((children children)) match-tree |
|
36 |
+ (if (eql (token match-tree) token) |
|
37 |
+ match-tree |
|
38 |
+ (prog1 (elt children |
|
39 |
+ (or (position token (children match-tree) :test #'token=) |
|
40 |
+ (vector-push-extend (make-instance 'match-tree :token token) |
|
41 |
+ children))) |
|
42 |
+ (setf children (stable-sort children #'< :key #'token))))))) |
|
43 |
+ |
|
44 |
+(define-modify-macro orf (it) or) |
|
45 |
+ |
|
46 |
+(defgeneric match-match-tree (match-tree item) |
|
47 |
+ (:method ((match-tree match-tree) (tokens list)) |
|
48 |
+ (let ((current match-tree)) |
|
49 |
+ (dolist (token tokens current) |
|
50 |
+ (let ((next (match-match-tree current token))) |
|
51 |
+ (setf current next))) |
|
52 |
+ )) |
|
53 |
+ (:method ((match-tree match-tree) (token integer)) |
|
54 |
+ (find token |
|
55 |
+ (children match-tree) |
|
56 |
+ :test #'token=))) |
|
57 |
+ |
|
58 |
+(defun random-match-tree (&optional (iters 10)) |
|
59 |
+ (flet ((random-list (len lim) (loop repeat len collect (1+ (random lim))))) |
|
60 |
+ (let ((result (make-instance 'match-tree :token nil))) |
|
61 |
+ (loop repeat iters |
|
62 |
+ do (add-to-tree result |
|
63 |
+ (random-list (1+ (random 5)) (1+ (random 10)))) |
|
64 |
+ finally (return result))))) |
|
65 |
+ |
|
66 |
+(defun print-match-tree (match-tree &optional (level 0)) |
|
67 |
+ (format t "~&~v,2,1,'-<~>~a~%" level (token match-tree)) |
|
68 |
+ (loop for child across (children match-tree) |
|
69 |
+ do (print-match-tree child (1+ level)))) |
|
70 |
+ |
|
71 |
+(coalesce-lists |
|
72 |
+ (coalesce-lists '(1 2 3) '(1 2 3 4)) |
|
73 |
+ (coalesce-lists '(1 2 4) '(1 3 4))) |
|
74 |
+; => '(1 (2 3) (3 4)) |
|
75 |
+ |
|
76 |
+(defun match-url) |