(.module: [lux (#- Name) [abstract [monad (#+ do)] [equivalence (#+ Equivalence)]] [control [pipe (#+ do>)] ["." try (#+ Try)] ["<>" parser ["<.>" xml (#+ Parser)] ["<.>" text]] [concurrency ["." promise (#+ Promise)]]] [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)))]

  [format_group Group .. (|>)]
  [format_name Name .. (|>)]
  [format_version Version .. (|>)]
  [format_last_updated Instant .. ..instant_format]
  )

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

(def: #export (format value)
  (-> Metadata XML)
  (#xml.Node ..
             xml.attributes
             (list (..format_group (get@ #group value))
                   (..format_name (get@ #name value))
                   (#xml.Node ..
                              xml.attributes
                              (list (..format_versions (get@ #versions value))
                                    (..format_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
      ))

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

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

(def: #export (read repository artifact)
  (-> (Repository Promise) Artifact (Promise (Try Metadata)))
  (do promise.monad
    [project (\ repository download (..uri artifact))]
    (case project
      (#try.Success project)
      (wrap (|> project
                (do> try.monad
                     [(\ utf8.codec decode)]
                     [(\ xml.codec decode)]
                     [list (.run ..parser)])))
      
      (#try.Failure error)
      (wrap (#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 Promise) Artifact Metadata (Promise (Try Any)))
  (|> metadata
      ..format
      (\ xml.codec encode)
      (\ utf8.codec encode)
      (\ repository upload (..uri artifact))))