(.module: [library [lux (#- Name) [abstract [monad (#+ do)] [equivalence (#+ Equivalence)]] [control [pipe (#+ do>)] ["." try (#+ Try)] ["<>" parser ["<.>" xml (#+ Parser)] ["<.>" text]] [concurrency ["." async (#+ Async)]]] [data ["." product] ["." text ["%" format] [encoding ["." utf8]]] [format ["." xml (#+ XML)]] [collection ["." list ("#\." functor)]]] [math [number ["n" nat]]] ["." time (#+ Time) ["." instant (#+ Instant)] ["." date (#+ Date)] ["." year] ["." month]] [world [net ["." uri (#+ URI)]]]]] ["." // ["/#" // #_ [repository (#+ Repository)] ["#." 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)))]

  [group_format Group .. (|>)]
  [name_format Name .. (|>)]
  [version_format Version .. (|>)]
  [last_updated_format Instant .. ..instant_format]
  )

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

(def: #export (format value)
  (-> Metadata XML)
  (#xml.Node ..
             xml.attributes
             (list (..group_format (get@ #group value))
                   (..name_format (get@ #name value))
                   (#xml.Node ..
                              xml.attributes
                              (list (..versions_format (get@ #versions value))
                                    (..last_updated_format (get@ #last_updated value)))))))

(def: (text tag)
  (-> xml.Tag (Parser Text))
  (<| (.node 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)
  (.then (do <>.monad
                 [date ..date_parser
                  time ..time_parser]
                 (in (instant.of_date_time date time)))
               (..text ..)))

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

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

(def: #export uri
  (-> Artifact URI)
  //.remote_project_uri)

(def: epoch
  Instant
  (instant.of_millis +0))

(def: #export (read repository artifact)
  (-> (Repository Async) Artifact (Async (Try Metadata)))
  (do async.monad
    [project (\ repository download (..uri artifact))]
    (case project
      (#try.Success project)
      (in (|> project
              (do> try.monad
                   [(\ utf8.codec decode)]
                   [(\ xml.codec decode)]
                   [list (.run ..parser)])))
      
      (#try.Failure error)
      (in (#try.Success
           (let [(^slots [#///artifact.group #///artifact.name]) artifact]
             {#group group
              #name name
              #versions (list)
              #last_updated ..epoch}))))))

(def: #export (write repository artifact metadata)
  (-> (Repository Async) Artifact Metadata (Async (Try Any)))
  (|> metadata
      ..format
      (\ xml.codec encode)
      (\ utf8.codec encode)
      (\ repository upload (..uri artifact))))