Add support for loading state and config files
Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
This commit is contained in:
parent
3f8c150964
commit
fe660a8929
|
@ -0,0 +1,34 @@
|
||||||
|
;; SPDX-License-Identifier: Apache-2.0
|
||||||
|
;; SPDX-FileCopyrightText: 2022 Sebastian Crane <seabass-labrax@gmx.com>
|
||||||
|
|
||||||
|
(ns system
|
||||||
|
(:require [clojure.data.json :as json]
|
||||||
|
[clj-yaml.core :as yaml]))
|
||||||
|
|
||||||
|
(defn json-data-reader [key value]
|
||||||
|
(if (= key :games)
|
||||||
|
(into (empty value)
|
||||||
|
(map #(hash-map (first %)
|
||||||
|
(set (second %)))
|
||||||
|
value))
|
||||||
|
value))
|
||||||
|
|
||||||
|
(defn load-state [f]
|
||||||
|
(try
|
||||||
|
(with-open [datafile (clojure.java.io/reader f)]
|
||||||
|
(json/read datafile
|
||||||
|
:value-fn json-data-reader
|
||||||
|
:key-fn keyword))
|
||||||
|
(catch Exception e nil)))
|
||||||
|
|
||||||
|
(defn save-state [f data]
|
||||||
|
(try
|
||||||
|
(with-open [datafile (clojure.java.io/writer f)]
|
||||||
|
(json/write data datafile))
|
||||||
|
(catch Exception e nil)))
|
||||||
|
|
||||||
|
(defn load-config [f]
|
||||||
|
(try
|
||||||
|
(with-open [datafile (clojure.java.io/reader f)]
|
||||||
|
(yaml/parse-stream datafile))
|
||||||
|
(catch Exception e nil)))
|
Loading…
Reference in New Issue