From 169cb88b8ec774868ce8befbc0ccb2be6a91aa73 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 4 Dec 2018 20:27:33 -0400 Subject: Restored the "encode" function for duration, to correct for overkill when removing contributor code. --- stdlib/source/lux/data/text/format.lux | 5 +++++ stdlib/source/lux/macro/poly/json.lux | 6 +++--- stdlib/source/lux/time/duration.lux | 25 +++++++++++++++++++++++++ stdlib/test/test/lux/data/format/json.lux | 11 ++++++----- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/stdlib/source/lux/data/text/format.lux b/stdlib/source/lux/data/text/format.lux index ad0653e76..234a639f2 100644 --- a/stdlib/source/lux/data/text/format.lux +++ b/stdlib/source/lux/data/text/format.lux @@ -15,6 +15,7 @@ [list ("list/." Monad)]]] [time ["." instant] + ["." duration] ["." date]] [math ["." modular]] @@ -57,6 +58,10 @@ [%date date.Date (:: date.Codec encode)] ) +(def: #export %duration + (Format duration.Duration) + duration.encode) + (def: #export (%cursor [file line column]) (Format Cursor) (|> (list (%t file) (%n line) (%n column)) diff --git a/stdlib/source/lux/macro/poly/json.lux b/stdlib/source/lux/macro/poly/json.lux index f1a5a840a..96c647c6b 100644 --- a/stdlib/source/lux/macro/poly/json.lux +++ b/stdlib/source/lux/macro/poly/json.lux @@ -24,7 +24,7 @@ ["d" dictionary]]] [time ## ["i" instant] - ["du" duration] + ## ["du" duration] ["da" date]] [macro (#+ with-gensyms) ["s" syntax (#+ syntax:)] @@ -103,7 +103,7 @@ (wrap (` (: (~ (@JSON//encode inputT)) (|>> (:: (~' encode)) #//.String)))))] - [du.Duration du.Codec] + ## [du.Duration du.Codec] ## [i.Instant i.Codec] [da.Date da.Codec] [da.Day da.Codec] @@ -219,7 +219,7 @@ (wrap (` (: (~ (@JSON//decode inputT)) (p.codec //.string)))))] - [du.Duration du.Codec] + ## [du.Duration du.Codec] ## [i.Instant i.Codec] [da.Date da.Codec] [da.Day da.Codec] diff --git a/stdlib/source/lux/time/duration.lux b/stdlib/source/lux/time/duration.lux index a39e6f9ed..35401497a 100644 --- a/stdlib/source/lux/time/duration.lux +++ b/stdlib/source/lux/time/duration.lux @@ -108,3 +108,28 @@ (structure: #export _ (Monoid Duration) (def: identity empty) (def: compose merge)) + +(def: #export (encode duration) + (-> Duration Text) + (if (:: Equivalence = empty duration) + "+0ms" + (let [signed? (negative? duration) + [days time-left] [(query day duration) (frame day duration)] + days (if signed? + (int/abs days) + days) + time-left (if signed? + (scale-up -1 time-left) + time-left) + [hours time-left] [(query hour time-left) (frame hour time-left)] + [minutes time-left] [(query minute time-left) (frame minute time-left)] + [seconds time-left] [(query second time-left) (frame second time-left)] + millis (to-millis time-left)] + ($_ text/compose + (if signed? "-" "+") + (if (i/= +0 days) "" (text/compose (nat/encode (.nat days)) "D")) + (if (i/= +0 hours) "" (text/compose (nat/encode (.nat hours)) "h")) + (if (i/= +0 minutes) "" (text/compose (nat/encode (.nat minutes)) "m")) + (if (i/= +0 seconds) "" (text/compose (nat/encode (.nat seconds)) "s")) + (if (i/= +0 millis) "" (text/compose (nat/encode (.nat millis)) "ms")) + )))) diff --git a/stdlib/test/test/lux/data/format/json.lux b/stdlib/test/test/lux/data/format/json.lux index dc30d54d7..0f905a844 100644 --- a/stdlib/test/test/lux/data/format/json.lux +++ b/stdlib/test/test/lux/data/format/json.lux @@ -30,13 +30,14 @@ [time ["ti" instant] ["tda" date] - ["tdu" duration]] + ## ["tdu" duration] + ] test] [test [lux [time ["_." instant] - ["_." duration] + ## ["_." duration] ["_." date]]]] ) @@ -93,7 +94,7 @@ ## #tuple [Bit Frac Text] #recursive Recursive ## #instant ti.Instant - #duration tdu.Duration + ## #duration tdu.Duration #date tda.Date #grams (unit.Qty unit.Gram) }) @@ -125,7 +126,7 @@ ## ($_ r.and r.bit r.frac (r.unicode size)) gen-recursive ## _instant.instant - _duration.duration + ## _duration.duration _date.date qty ))) @@ -161,7 +162,7 @@ ## (:: text.Equivalence = tL2 tR2))) (:: Equivalence = (get@ #recursive recL) (get@ #recursive recR)) ## (:: ti.Equivalence = (get@ #instant recL) (get@ #instant recR)) - (:: tdu.Equivalence = (get@ #duration recL) (get@ #duration recR)) + ## (:: tdu.Equivalence = (get@ #duration recL) (get@ #duration recR)) (:: tda.Equivalence = (get@ #date recL) (get@ #date recR)) (:: unit.Equivalence = (get@ #grams recL) (get@ #grams recR)) )))) -- cgit v1.2.3