summaryrefslogtreecommitdiff
path: root/src/Language/Fiddle/Internal/Scopes.hs
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-10-03 01:58:23 -0600
committerJosh Rahm <joshuarahm@gmail.com>2024-10-03 01:58:23 -0600
commitfa32199f5ffc6405bd405e055051e11e85c80668 (patch)
tree87effa6909f7cc6f05782f818c01d0a983a620fb /src/Language/Fiddle/Internal/Scopes.hs
parent719c8f8ed3d1e6337f27d3b9d5a033a4b63726b8 (diff)
downloadfiddle-fa32199f5ffc6405bd405e055051e11e85c80668.tar.gz
fiddle-fa32199f5ffc6405bd405e055051e11e85c80668.tar.bz2
fiddle-fa32199f5ffc6405bd405e055051e11e85c80668.zip
Another monolithic change. Not good git ettiquite.
Import statements are fully implemented including compiling to an interface file for faster compilations.
Diffstat (limited to 'src/Language/Fiddle/Internal/Scopes.hs')
-rw-r--r--src/Language/Fiddle/Internal/Scopes.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Language/Fiddle/Internal/Scopes.hs b/src/Language/Fiddle/Internal/Scopes.hs
index eea4c6f..ac6f7d1 100644
--- a/src/Language/Fiddle/Internal/Scopes.hs
+++ b/src/Language/Fiddle/Internal/Scopes.hs
@@ -3,6 +3,8 @@
module Language.Fiddle.Internal.Scopes where
import Control.Monad (forM)
+import Data.Aeson
+import Data.Aeson.Key
import Data.List (inits, intercalate)
import Data.List.NonEmpty (NonEmpty (..), prependList)
import Data.Map (Map)
@@ -64,6 +66,9 @@ upsertScope (s :| (a : as)) v (Scope ss sv) =
(replaced, subscope') = upsertScope (a :| as) v subscope
in (replaced, Scope (Map.insert s subscope' ss) sv)
+insertScope :: (Ord k) => NonEmpty k -> t -> Scope k t -> Scope k t
+insertScope p s = snd . upsertScope p s
+
-- insertScope :: (Ord k) => NonEmpty k -> t -> Scope k t -> Scope k t
-- insertScope a b = snd . upsertScope a b
@@ -108,3 +113,17 @@ lookupScopeWithPath (ScopePath current others) key scope =
case lookupScope (prependList prefix key) scope of
Just s -> [(prependList prefix key, s)]
Nothing -> []
+
+instance (ToJSONKey k, ToJSON v, Ord k) => ToJSON (Scope k v) where
+ toJSON (Scope subScopes scopeValues) =
+ object
+ [ fromString "subScopes" .= toJSON subScopes,
+ fromString "scopeValues" .= toJSON scopeValues
+ ]
+
+instance (FromJSONKey k, FromJSON v, Ord k) => FromJSON (Scope k v) where
+ parseJSON (Object v) =
+ Scope
+ <$> v .: fromString "subScopes"
+ <*> v .: fromString "scopeValues"
+ parseJSON _ = fail "Expected an object for Scope"