summaryrefslogtreecommitdiff
path: root/src/state.lisp
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2025-10-23 10:17:00 +0300
committerGravatar Uko Kokņevičs2025-10-23 10:32:36 +0300
commitfec434a4e2d0ff65510581e461d87a945d25759a (patch)
tree676891233e6121f8801f4751d3e2d1ca7ad4e09c /src/state.lisp
parentUse alexandria's make-keyword & symbolicate (diff)
downloadukkoclot-fec434a4e2d0ff65510581e461d87a945d25759a.tar.gz
ukkoclot-fec434a4e2d0ff65510581e461d87a945d25759a.tar.xz
ukkoclot-fec434a4e2d0ff65510581e461d87a945d25759a.zip
Use serapeum's -> & defsubst
Diffstat (limited to 'src/state.lisp')
-rw-r--r--src/state.lisp19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/state.lisp b/src/state.lisp
index ef4050d..9f1a38f 100644
--- a/src/state.lisp
+++ b/src/state.lisp
@@ -5,8 +5,9 @@
5 (:nicknames :state) 5 (:nicknames :state)
6 (:use :c2cl :ukkoclot/src/rw-lock) 6 (:use :c2cl :ukkoclot/src/rw-lock)
7 (:import-from :com.dieggsy.f-string :enable-f-strings) 7 (:import-from :com.dieggsy.f-string :enable-f-strings)
8 (:import-from :conf :*config* :bot-token) 8 (:import-from :conf :config :*config* :bot-token)
9 (:import-from :sqlite :sqlite-handle) 9 (:import-from :serapeum :->)
10 (:import-from :ukkoclot/src/db :db)
10 (:export 11 (:export
11 #:*state* 12 #:*state*
12 #:state 13 #:state
@@ -26,38 +27,44 @@
26 27
27(defstruct (state (:constructor make-state%)) 28(defstruct (state (:constructor make-state%))
28 (lock (make-rw-lock :name "state's lock") :type rw-lock :read-only t) 29 (lock (make-rw-lock :name "state's lock") :type rw-lock :read-only t)
29 (db (error "No value given for DB") :type sqlite-handle :read-only t) 30 (db (error "No value given for DB") :type db :read-only t)
30 (base-uri (error "No value given for base-uri") :type string :read-only t) 31 (base-uri (error "No value given for base-uri") :type string :read-only t)
31 (power-on t :type boolean) 32 (power-on t :type boolean)
32 (username% nil :type (or string null)) 33 (username% nil :type (or string null))
33 (id% nil :type (or integer null))) 34 (id% nil :type (or integer null)))
34 35
36(-> make-state (db &optional config) state)
35(defun make-state (db &optional (config *config*)) 37(defun make-state (db &optional (config *config*))
36 (check-type db sqlite-handle) 38 (check-type db db)
37 (let ((base-uri #f"https://api.telegram.org/bot{(bot-token config)}/")) 39 (let ((base-uri #f"https://api.telegram.org/bot{(bot-token config)}/"))
38 (make-state% :db db :base-uri base-uri))) 40 (make-state% :db db :base-uri base-uri)))
39 41
40(defvar *state* nil 42(defvar *state* nil
41 "Bot's general state. You should initialise this with a value before doing anything fun.") 43 "Bot's general state. You should initialise this with a value before doing anything fun.")
44(declaim (type (or state null) *state*))
42 45
46(-> db (&optional state) db)
43(defun db (&optional (state *state*)) 47(defun db (&optional (state *state*))
44 "Get the database handle of the bot." 48 "Get the database handle of the bot."
45 (with-slots (lock db) state 49 (with-slots (lock db) state
46 (with-read-lock (lock) 50 (with-read-lock (lock)
47 db))) 51 db)))
48 52
53(-> base-uri (&optional state) string)
49(defun base-uri (&optional (state *state*)) 54(defun base-uri (&optional (state *state*))
50 "Get the base URI of the bot." 55 "Get the base URI of the bot."
51 (with-slots (lock base-uri) state 56 (with-slots (lock base-uri) state
52 (with-read-lock (lock) 57 (with-read-lock (lock)
53 base-uri))) 58 base-uri)))
54 59
60(-> power-on (&optional state) boolean)
55(defun power-on (&optional (state *state*)) 61(defun power-on (&optional (state *state*))
56 "Get whether the bot is running" 62 "Get whether the bot is running"
57 (with-slots (lock power-on) state 63 (with-slots (lock power-on) state
58 (with-read-lock (lock) 64 (with-read-lock (lock)
59 power-on))) 65 power-on)))
60 66
67(-> set-power-on (boolean &optional state) boolean)
61(defun set-power-on (new-value &optional (state *state*)) 68(defun set-power-on (new-value &optional (state *state*))
62 "Set the value of the power-on" 69 "Set the value of the power-on"
63 (with-slots (lock power-on) state 70 (with-slots (lock power-on) state
@@ -67,12 +74,14 @@
67(defsetf power-on (&optional (state '*state*)) (new-value) 74(defsetf power-on (&optional (state '*state*)) (new-value)
68 `(set-power-on ,new-value ,state)) 75 `(set-power-on ,new-value ,state))
69 76
77(-> username% (&optional state) (or string null))
70(defun username% (&optional (state *state*)) 78(defun username% (&optional (state *state*))
71 "Get the cached bot's username, you should probably use `ukkoclot/src/tg:bot-username' instead." 79 "Get the cached bot's username, you should probably use `ukkoclot/src/tg:bot-username' instead."
72 (with-slots (lock username%) state 80 (with-slots (lock username%) state
73 (with-read-lock (lock) 81 (with-read-lock (lock)
74 username%))) 82 username%)))
75 83
84(-> set-username% (string &optional state) string)
76(defun set-username% (new-value &optional (state *state*)) 85(defun set-username% (new-value &optional (state *state*))
77 (with-slots (lock username%) state 86 (with-slots (lock username%) state
78 (with-write-lock (lock) 87 (with-write-lock (lock)
@@ -81,12 +90,14 @@
81(defsetf username% (&optional (state '*state*)) (new-value) 90(defsetf username% (&optional (state '*state*)) (new-value)
82 `(set-username% ,new-value ,state)) 91 `(set-username% ,new-value ,state))
83 92
93(-> id% (&optional state) (or integer null))
84(defun id% (&optional (state *state*)) 94(defun id% (&optional (state *state*))
85 "Get the cached bot's ID, you should probably use `ukkoclot/src/tg:bot-id' instead." 95 "Get the cached bot's ID, you should probably use `ukkoclot/src/tg:bot-id' instead."
86 (with-slots (lock id%) state 96 (with-slots (lock id%) state
87 (with-read-lock (lock) 97 (with-read-lock (lock)
88 id%))) 98 id%)))
89 99
100(-> set-id% (integer &optional state) integer)
90(defun set-id% (new-value &optional (state *state*)) 101(defun set-id% (new-value &optional (state *state*))
91 (with-slots (lock id%) state 102 (with-slots (lock id%) state
92 (with-write-lock (lock) 103 (with-write-lock (lock)