1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE DeriveGeneric #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 {-# LANGUAGE NoMonomorphismRestriction #-}
6 {-# LANGUAGE OverloadedStrings #-}
7 {-# LANGUAGE StrictData #-}
8 {-# LANGUAGE TypeApplications #-}
9 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
10 module RelaxNG.Commoning where
13 import Data.Either (Either(..))
14 import Data.Foldable (toList)
15 import Data.Functor ((<$>))
16 import Data.Function (($), (.))
17 import Data.Hashable (Hashable)
18 import Data.Maybe (Maybe(..))
20 import Data.Sequence (Seq)
21 import Data.Tuple (fst)
22 import Text.Show (Show)
23 import GHC.Generics (Generic)
24 import qualified Data.Sequence as Seq
25 import qualified Data.Text.Lazy as TL
26 import qualified Data.TreeSeq.Strict as TS
27 import qualified Text.Megaparsec as P
29 import Symantic.XML as XML
30 import Symantic.XML.RelaxNG as RelaxNG
33 namespace Nothing xmlns_commoning <.>
34 namespace (Just "xsd") "http://www/w3/org/2001/XMLSchema-datatypes" <.>
37 -- * Class 'Commonable'
38 xmlns_commoning :: Namespace
39 xmlns_commoning = "2018/commoning.rnc"
40 elem = element . QName xmlns_commoning
41 attr n = attribute (QName "" n) text
42 many0Seq = dimap Seq.fromList toList . many0
45 newtype Ident = Ident TL.Text
46 deriving (Eq,Ord,Show,Hashable)
48 instance RNCText Ident where
49 rncText_qname = QName xmlns_xsd "id"
50 instance EncodeText Ident where
51 encodeText (Ident t) = t
52 instance DecodeText Ident where
53 decodeText = Ident . fst <$> P.match (P.many P.anySingle)
56 newtype Name = Name TL.Text
57 deriving (Eq,Ord,Show,Hashable)
58 instance RNCText Name where
59 rncText_qname = rncText_qname @TL.Text
60 rncText_params = rncText_params @TL.Text
62 instance EncodeText Name where
63 encodeText (Name t) = t
64 instance DecodeText Name where
65 decodeText = Name . fst <$> P.match (P.many P.anySingle)
70 { commoning_persons :: Persons
71 , commoning_opinions :: Opinions
72 , commoning_groups :: Groups
73 , commoning_operations :: Operations
74 , commoning_resources :: Resources
75 } deriving (Show, Generic)
92 , person_fields :: Seq Fields
93 } deriving (Show, Generic)
102 -- *** Type 'Persons'
103 type Persons = [Person]
111 type Opinions = [Grades]
122 , grade_abbrev :: Maybe Name
123 , grade_color :: Maybe Color
124 } deriving (Show, Generic)
131 optional (attr "abbrev") <:>
132 optional (attr "color")
141 , grades_name :: Maybe Name
142 , grades_list :: [Grade]
143 } deriving (Show, Generic)
150 optional (attr "name") <:>
157 , field_value :: TL.Text
158 } deriving (Show, Generic)
161 type Fields = TS.Tree NodeField
163 -- **** Type 'NodeField'
166 | NodeFields { fields_name :: Name }
167 deriving (Show, Generic)
172 adt @(TS.Tree NodeField) $
173 (dimap NodeFields fields_name $
179 Left f -> TS.tree0 $ NodeField f
182 TS.Tree (NodeField f) _ -> Left f
197 type Group = TS.Tree NodeGroup
199 -- *** Type 'NodeGroup'
203 , group_name :: Maybe Name
204 , group_fields :: Seq Fields
205 , group_members :: Members
206 } deriving (Show, Generic)
209 type Groups = Seq Group
219 adt @(TS.Tree NodeGroup) $
222 optional (attr "name") <:>
231 { member_person :: Ident
232 } deriving (Show, Generic)
234 -- *** Type 'Members'
235 type Members = [Member]
237 members = define "members" $ many0 member
244 -- ** Type 'Operation'
245 type Operation = TS.Tree NodeOperation
246 -- *** Type 'NodeOperation'
247 newtype NodeOperation
249 { operation_id :: Ident
250 } deriving (Show, Generic)
251 -- *** Type 'Operations'
252 type Operations = Seq Operation
255 define "operations" $
262 adt @(TS.Tree NodeOperation) $
263 (adt $ attr "id") <:>
266 -- ** Type 'Resource'
267 type Resource = TS.Tree NodeResource
268 -- *** Type 'NodeResource'
271 { resource_name :: Name
272 , resource_policies :: Policies
273 } deriving (Show, Generic)
275 -- *** Type 'Resources'
276 type Resources = Seq Resource
286 adt @(TS.Tree NodeResource) $
287 (adt $ attr "name" <:> many0 policy) <:>
293 { policy_operation :: Name
295 , policy_toward :: Maybe Ident
296 , policy_rules :: Rules
297 } deriving (Show, Generic)
305 optional (attr "toward") <:>
308 -- *** Type 'Policies'
309 type Policies = [Policy]
314 { rule_grades :: Ident
315 , rule_gradeRange :: GradeRange
316 } deriving (Show, Generic)
328 -- *** Type 'GradeRange'
330 = GradeRange_Single Name
331 | GradeRange_Min Name
332 | GradeRange_Max Name
333 | GradeRange Name Name
334 deriving (Show, Generic)
337 define "gradeRange" $
341 attr "gradeMax" <+> (
347 rule = define "rule" $
357 <$> attribute "grades" ident
358 <*> (GradeRange_Single <$> attribute "grade" name)
361 (\gs gMin mgMax -> Rule gs $ case mgMax of
362 Nothing -> GradeRange_Min gMin
363 Just gMax -> GradeRange gMin gMax)
364 <$> attribute "grades" ident
365 <*> attribute "gradeMin" name
366 <*> optionalPerm (attribute "gradeMax" name)
369 (\gs mgMin gMax -> Rule gs $ case mgMin of
370 Nothing -> GradeRange_Max gMax
371 Just gMin -> GradeRange gMin gMax)
372 <$> attribute "grades" ident
373 <*> optionalPerm (attribute "gradeMin" name)
374 <*> attribute "gradeMax" name