From a53cb0cece97a8b2c88c1205fee16b36e1e7e6c2 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 13 Oct 2018 13:39:49 -0400 Subject: Turned the Archive into an abstract type for greater safety when operating on it (cannot skip the abstraction by directly using the dictionary functions). --- stdlib/source/lux/compiler/meta/archive.lux | 52 ++++++++++++++++------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'stdlib/source') diff --git a/stdlib/source/lux/compiler/meta/archive.lux b/stdlib/source/lux/compiler/meta/archive.lux index 1e9240f7b..457e3b874 100644 --- a/stdlib/source/lux/compiler/meta/archive.lux +++ b/stdlib/source/lux/compiler/meta/archive.lux @@ -98,26 +98,32 @@ ["Old document's key" (describe (..signature (get@ #key old)))] ["New document's key" (describe (..signature (get@ #key new)))])) -(type: #export Archive - (Dictionary Text (Ex [d] (Document d)))) - -(def: #export empty Archive (dict.new text.Hash)) - -(def: #export (add name document archive) - (-> Text (Ex [d] (Document d)) Archive (Error Archive)) - (case (dict.get name archive) - (#.Some existing) - (if (is? document existing) - (#error.Success archive) - (ex.throw cannot-replace-document-in-archive [name existing document])) - - #.None - (#error.Success (dict.put name document archive)))) - -(def: #export (merge additions archive) - (-> Archive Archive (Error Archive)) - (monad.fold error.Monad - (function (_ [name' document'] archive') - (..add name' document' archive')) - archive - (dict.entries additions))) +(abstract: #export Archive + {} + + (Dictionary Text (Ex [d] (Document d))) + + (def: #export empty + Archive + (:abstraction (dict.new text.Hash))) + + (def: #export (add name document archive) + (-> Text (Ex [d] (Document d)) Archive (Error Archive)) + (case (dict.get name (:representation archive)) + (#.Some existing) + (if (is? document existing) + (#error.Success archive) + (ex.throw cannot-replace-document-in-archive [name existing document])) + + #.None + (#error.Success (:abstraction (dict.put name document + (:representation archive)))))) + + (def: #export (merge additions archive) + (-> Archive Archive (Error Archive)) + (monad.fold error.Monad + (function (_ [name' document'] archive') + (..add name' document' archive')) + archive + (dict.entries (:representation additions)))) + ) -- cgit v1.2.3