~subsetpark/bagatto

05cfd98183563a77f0577267ca7c940004fdab04 — Zach Smith 3 months ago 37b87a8
Refactor loader/generator logic
4 files changed, 69 insertions(+), 63 deletions(-)

M src/core.janet
M src/generators.janet
M src/loaders.janet
A test/generators.janet
M src/core.janet => src/core.janet +6 -63
@@ 54,33 54,13 @@
  function that will be called on each file-contents.
  ```
  [data-spec env]

  (let [data-pairs (pairs data-spec)
        jobs (seq [[spec-name spec] :in data-pairs]

               (threads/print "Reading " spec-name " data spec...")

               (setdyn :error-context {:spec-name spec-name})

               (let [with-defaults (set-defaults spec)
                     transform-f (or (spec :transform) identity)]
                 (match with-defaults
                   ({:src loader :attrs parser} (function? loader))
                   (loaders/from-file-spec-loader spec-name
                                                  loader
                                                  parser
                                                  transform-f)

                   ({:src path :attrs parser} (string? path))
                   (loaders/from-path-loader spec-name
                                             path
                                             parser)

                   {:attrs attrs}
                   (loaders/bare-attr-loader spec-name
                                             attrs)

                   _ (error/data-error with-defaults))))]
               (loaders/from-spec (set-defaults spec) spec-name))]
    
    (threads/distribute jobs thread-init)))

(defn produce-writer-specs


@@ 93,47 73,10 @@
  [site data env]
  (let [site-pairs (pairs site)
        jobs (seq [[spec-name spec] :in site-pairs]

               (threads/print "Reading " spec-name " site spec...")

               (let [with-defaults (set-defaults spec)
                     filter (spec :filter)]
                 (default filter (fn [_site _item] true))

                 (match with-defaults
                   {:each site-selector
                    :dest path-generator
                    :out renderer}
                   (generators/render-each-generator data
                                                     spec-name
                                                     filter
                                                     site-selector
                                                     path-generator
                                                     renderer)

                   {:each site-selector
                    :dest path-generator}
                   (generators/copy-each-generator data
                                                   spec-name
                                                   filter
                                                   site-selector
                                                   path-generator)

                   {:dest path-generator
                    :out renderer}
                   (generators/render-generator data
                                                spec-name
                                                path-generator
                                                renderer)

                   {:some site-selector
                    :dest path-generator}
                   (generators/copy-some-generator data
                                                   spec-name
                                                   site-selector
                                                   path-generator)

                   _ (error/site-error with-defaults))))]
                  (threads/print "Reading " spec-name " site spec...")
                  (generators/from-spec (set-defaults spec)
                                        spec-name
                                        data))]

    (let [segments (-> (threads/distribute jobs thread-init) (values))]
      (array/concat ;segments))))

M src/generators.janet => src/generators.janet +39 -0
@@ 75,3 75,42 @@
             to (apply-path path-generator [data] :d)]
      (array/push res [:copy from to]))
    (:send parent [:res spec-name res])))

(defn from-spec
  [spec spec-name data]
  (let [filter (spec :filter)]
    (default filter (fn [_site _item] true))
    (match spec
      {:each site-selector
       :dest path-generator
       :out renderer}
      (render-each-generator data
                             spec-name
                             filter
                             site-selector
                             path-generator
                             renderer)

      {:each site-selector
       :dest path-generator}
      (copy-each-generator data
                           spec-name
                           filter
                           site-selector
                           path-generator)

      {:dest path-generator
       :out renderer}
      (render-generator data
                        spec-name
                        path-generator
                        renderer)

      {:some site-selector
       :dest path-generator}
      (copy-some-generator data
                           spec-name
                           site-selector
                           path-generator)

      _ (error/site-error spec))))

M src/loaders.janet => src/loaders.janet +21 -0
@@ 62,3 62,24 @@
  (fn [parent]
    (threads/print "Loaded " spec-name)
    (:send parent [:res spec-name attrs])))

(defn from-spec
  [spec spec-name]
  (let [transform-f (or (spec :transform) identity)]
    (match spec
      ({:src loader :attrs parser} (function? loader))
      (from-file-spec-loader spec-name
                                     loader
                                     parser
                                     transform-f)
      
      ({:src path :attrs parser} (string? path))
      (from-path-loader spec-name
                        path
                        parser)
      
      {:attrs attrs}
      (bare-attr-loader spec-name
                        attrs)
      
      _ (error/data-error spec))))

A test/generators.janet => test/generators.janet +3 -0
@@ 0,0 1,3 @@
(import testament :prefix "" :exit true)

(run-tests!)