(.module: [lux (#- Name) [abstract [monad (#+ do)] [equivalence (#+ Equivalence)]] [control ["<>" parser ["<.>" xml (#+ Parser)] ["<.>" text]]] [data ["." product] ["." text ["%" format (#+ format)]] [format ["." xml (#+ XML)]] [collection ["." list ("#\." functor)]]] [math [number ["n" nat]]] ["." time (#+ Time) ["." instant (#+ Instant)] ["." date (#+ Date)] ["." year] ["." month]]] ["." /// #_ ["#." artifact (#+ Group Name Version Artifact)]]) (type: #export Metadata {#group Group #name Name #versions (List Version) #last_updated Instant}) (def: (pad value) (-> Nat Text) (if (n.< 10 value) (format "0" (%.nat value)) (%.nat value))) (def: (date_format value) (%.Format Date) (format (|> value date.year year.value .nat %.nat) (|> value date.month month.number ..pad) (|> value date.day_of_month ..pad))) (def: (time_format value) (%.Format Time) (let [(^slots [#time.hour #time.minute #time.second]) (time.clock value)] (format (..pad hour) (..pad minute) (..pad second)))) (def: (instant_format value) (%.Format Instant) (format (..date_format (instant.date value)) (..time_format (instant.time value)))) (template [ ] [(def: xml.Tag ["" ])] [ "groupId"] [ "artifactId"] [ "version"] [ "versioning"] [ "versions"] [ "lastUpdated"] [ "metadata"] ) (template [
]
  [(def: 
     (->  XML)
     (|>> 
 #xml.Text list (#xml.Node  xml.attributes)))]

  [write_group Group .. (|>)]
  [write_name Name .. (|>)]
  [write_version Version .. (|>)]
  [write_last_updated Instant .. ..instant_format]
  )

(def: write_versions
  (-> (List Version) XML)
  (|>> (list\map ..write_version) (#xml.Node .. xml.attributes)))

(def: #export (write value)
  (-> Metadata XML)
  (#xml.Node ..
             xml.attributes
             (list (..write_group (get@ #group value))
                   (..write_name (get@ #name value))
                   (#xml.Node ..
                              xml.attributes
                              (list (..write_versions (get@ #versions value))
                                    (..write_last_updated (get@ #last_updated value)))))))

(def: (sub tag parser)
  (All [a] (-> xml.Tag (Parser a) (Parser a)))
  (do <>.monad
    [_ (.node tag)]
    (.children parser)))

(def: (text tag)
  (-> xml.Tag (Parser Text))
  (..sub tag .text))

(def: date_parser
  (.Parser Date)
  (do <>.monad
    [year (<>.codec n.decimal (.exactly 4 .decimal))
     year (<>.lift (year.year (.int year)))
     month (<>.codec n.decimal (.exactly 2 .decimal))
     month (<>.lift (month.by_number month))
     day_of_month (<>.codec n.decimal (.exactly 2 .decimal))]
    (<>.lift (date.date year month day_of_month))))

(def: time_parser
  (.Parser Time)
  (do <>.monad
    [hour (<>.codec n.decimal (.exactly 2 .decimal))
     minute (<>.codec n.decimal (.exactly 2 .decimal))
     second (<>.codec n.decimal (.exactly 2 .decimal))]
    (<>.lift (time.time
              {#time.hour hour
               #time.minute minute
               #time.second second
               #time.milli_second 0}))))

(def: last_updated_parser
  (Parser Instant)
  (.embed (do <>.monad
                  [date ..date_parser
                   time ..time_parser]
                  (wrap (instant.from_date_time date time)))
                (..text ..)))

(def: #export parser
  (Parser Metadata)
  (<| (..sub ..)
      ($_ <>.and
          (.somewhere (..text ..))
          (.somewhere (..text ..))
          (<| (..sub ..)
              ($_ <>.and
                  (<| .somewhere
                      (..sub ..)
                      (<>.many (..text ..)))
                  (.somewhere ..last_updated_parser)
                  ))
          )))

(def: #export equivalence
  (Equivalence Metadata)
  ($_ product.equivalence
      text.equivalence
      text.equivalence
      (list.equivalence text.equivalence)
      instant.equivalence
      ))