~subsetpark/bagatto

6ac2d6e6da139126e3abd7108d15395d030e29ed — Zach Smith 10 months ago 51f1d4a
Add some basic error handling
2 files changed, 83 insertions(+), 37 deletions(-)

M main.janet
M src/core.janet
M main.janet => main.janet +9 -1
@@ 48,7 48,15 @@
      (repl nil nil env)
      # Normal mode: evaluate index module and write site.
      (do
        (defn value [sym] ((env sym) :value))
        (defn value
          [sym]
          (try ((env sym) :value)
               ([err fib]
                (error
                 (string "Expected symbol "
                         sym
                         "; couldn't be found after evaluating "
                         index)))))
    
        (setdyn :bagatto-defaults (env :bagatto-defaults))


M src/core.janet => src/core.janet +74 -36
@@ 5,17 5,35 @@
(defn- struct->table [s]
  (->> (or s @{}) (kvs) (splice) (table)))

(defn- maybe-apply [f args]
(defn- apply-error
  [err f args-type ret-type]
  (error (string/format "%s;\nExpected %s function signature:\n(defn %s %s\n %s)"
                        err
                        (case ret-type :p "path" :c "contents")
                        (or (disasm f :name) "f")
                        (case args-type :d "[data]" :di "[data item]")
                        (case ret-type :p `"foo/bar/..."` :c `"<html>..."`))))

(defn- maybe-apply [f args args-type ret-type]
  (if (function? f)
    (f ;args)
    (try
      (f ;args)
      ([err fib] (apply-error err f args-type ret-type)))
    f))



(defn- set-defaults [spec]
  (table/setproto (struct->table spec)
                  (struct->table (dyn :bagatto-defaults))))

(defn- attrs-error
  [err attrs-f]
  (error (string err
                 ";\n"
                 "Required parse function signature:\n"
                 "(defn " (or (disasm attrs-f :name) "f") " [src attrs]\n"
                 " ...\n"
                 " attrs)")))

(defn load-data [data-spec]
  ```
  First phase of main business logic. `data-spec` contains a


@@ 41,33 59,44 @@
  
  (defn make-attrs [filename &opt file-contents attrs-f]
    (default attrs-f (fn [_ x] x))
    (->> @{:path filename :src file-contents}
         (attrs-f file-contents)))
    (try
      (->> @{:path filename :src file-contents}
           (attrs-f file-contents))
      ([err fib] (attrs-error err attrs-f))))
  
  (loop [[entry spec] :pairs data-spec]
    (let [with-defaults (set-defaults spec)
          transform-f (or (spec :transform) identity)]
      (put res entry (match with-defaults
                       
                       ({:src loader :attrs attrs-f} (fiber? loader))
                       (-> (seq [loader-out :generate loader]
                               (match loader-out
                                 [filename file-contents]
                                 (make-attrs filename file-contents attrs-f)
                                 filename
                                 (make-attrs filename)))
                          (transform-f))
                       
                       ({:src loader :attrs attrs-f} (function? loader))
                       (let [[filename file-contents] (loader)]
                         (make-attrs filename file-contents attrs-f))
                       
                       ({:src path :attrs attrs-f} (string? path))
                       (let [file-contents (slurp path)]
                         (make-attrs path file-contents attrs-f))
                       
                       {:attrs attrs}
                       attrs))))
          transform-f (or (spec :transform) identity)
          data (match with-defaults
                 
                 ({:src loader :attrs attrs-f} (fiber? loader))
                 (-> (seq [loader-out :generate loader]
                         (match loader-out
                           [filename file-contents]
                           (make-attrs filename file-contents attrs-f)
                           filename
                           (make-attrs filename)))
                    (transform-f))
                 
                 ({:src loader :attrs attrs-f} (function? loader))
                 (let [[filename file-contents] (loader)]
                   (make-attrs filename file-contents attrs-f))
                 
                 ({:src path :attrs attrs-f} (string? path))
                 (let [file-contents (slurp path)]
                   (make-attrs path file-contents attrs-f))
                 
                 {:attrs attrs}
                 attrs
                 
                 _
                 (error (string "Received invalid data spec: "
                                (string/format "%q" with-defaults)
                                "\n\n"
                                "Specification can be one of the following:\n"
                                "{:src (loader|path) :attrs attrs-f}\n"
                                "{:attrs attrs}\n")))]
      (put res entry data)))
  res)

(defn produce-writer-specs [site data]


@@ 94,29 123,38 @@
         :contents contents-f}
        (loop [item :in (data site-selector)]
          (if-let [_should-read (filter data item)
                   path (maybe-apply path-f [data item])
                   contents (maybe-apply contents-f [data item])]
                   path (maybe-apply path-f [data item] :di :p)
                   contents (maybe-apply contents-f [data item] :di :c)]
            (push-writer :write path contents)))
        
        {:each site-selector
         :path path-f}
        (loop [item :in (data site-selector)]
          (if-let [_should-read (filter data item)
                   path (maybe-apply path-f [data item])]
                   path (maybe-apply path-f [data item] :di :p)]
            (push-writer :copy (item :path) path)))
        
        {:path path-f
         :contents contents-f}
        (if-let [path (maybe-apply path-f [data])
                 contents (maybe-apply contents-f [data])]
        (if-let [path (maybe-apply path-f [data] :d :p)
                 contents (maybe-apply contents-f [data] :d :c)]
          (push-writer :write path contents))
        
        {:some site-selector
         :path path-f}
        (if-let [item (data site-selector)
                 path (maybe-apply path-f [data])]
          (push-writer :copy (item :path) path))))) 
  
                 path (maybe-apply path-f [data] :d :p)]
          (push-writer :copy (item :path) path))

        _ (error (string "Received invalid site spec: "
                         (string/format "%q" with-defaults)
                         "\n\n"
                         "Specification can be one of the following:\n"
                         "{:each site-selector :path path-f :contents contents-f}\n"
                         "{:each site-selector :path path-f}\n"
                         "{:path path-f :contents contents-f}\n"
                         "{:some site-selector :path path-f}"))))) 
        
  writers)

(defn- new-writer [type x y]