never executed always true always false
1 module Conjure.Language.ModelDiff
2 ( modelDiff
3 , modelDiffIO
4 ) where
5
6 -- conjure
7 import Conjure.Prelude
8 import Conjure.UserError
9 import Conjure.Language.Definition
10 import Conjure.Language.Pretty
11
12 -- containers
13 import qualified Data.Set as S
14
15
16 modelDiffIO :: MonadUserError m => Model -> Model -> m ()
17 modelDiffIO m1 m2 =
18 case modelDiff m1 m2 of
19 Nothing -> return ()
20 Just msg -> userErr $ return $ sep ["Files differ, specifically:", msg]
21
22
23 -- | returns `Just msg` if the models are different, msg being an explanation of what's diff.
24 -- returns `Nothing` if the models are the same.
25 modelDiff :: Model -> Model -> Maybe Doc
26 modelDiff m1 m2 =
27 let
28 explode = concatMap $ \ st -> case st of SuchThat xs -> map (SuchThat . return) xs
29 Where xs -> map (Where . return) xs
30 _ -> [st]
31 m1Statements = m1 |> mStatements |> explode |> S.fromList
32 m2Statements = m2 |> mStatements |> explode |> S.fromList
33 m1Extra = S.difference m1Statements m2Statements
34 m2Extra = S.difference m2Statements m1Statements
35 in
36 if S.null m1Extra && S.null m2Extra
37 then Nothing
38 else Just $ vcat $ concat
39 [ [ "These models seem to be different." ]
40 , [ hang "Only in the 1st:" 8 (vcat (map pretty (S.toList m1Extra))) | not (S.null m1Extra) ]
41 , [ hang "Only in the 2nd:" 8 (vcat (map pretty (S.toList m2Extra))) | not (S.null m2Extra) ]
42 ]
43