summaryrefslogtreecommitdiff
path: root/src/config.lisp
blob: 55575bbefb41124dbe7cfb3fa5330ec5c440ec70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
;; SPDX-License-Identifier: EUPL-1.2
;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com>
(defpackage :ukkoclot/config
  (:use :c2cl :ukkoclot/hash-tables)
  (:documentation
   "Stuff for loading the configuration of the bot")
  (:export
   :config-load :config-merge
   :config-p
   :config-bot-name :config-bot-token :config-db-path :config-dev-group :config-owner))
(in-package :ukkoclot/config)

(defmacro defconfig (&rest slots-and-types)
  "Macro to make the config struct creation easier."
  `(defstruct config
     ,@(loop for (name type) on slots-and-types by #'cddr
             collect `(,(intern (symbol-name name)) (error "No value given for ~A" ,name) :type ,type :read-only t))))

(defconfig
  :bot-name string
  :bot-token string
  :db-path string
  :dev-group integer
  :owner integer)

(defun config-load (filename)
  "Load the config from the given `filename'.  All entries must be specified."
  (apply #'make-config (with-open-file (f filename) (read f))))

(defun config-merge (config filename)
  "Merge the current config with new entries from `filename'."
  (loop for (name value) on (with-open-file (f filename) (read f)) by #'cddr do
    (setf (slot-value config (intern (symbol-name name) :ukkoclot/config)) value)))