1 {-# LANGUAGE DerivingStrategies #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 Data types to represent Edifact values.
7 See "Text.Edifact.Parsing" to learn how to build parsers and use such parsers.
9 module Text.Edifact.Types
25 import Data.Scientific (Scientific)
26 import Data.String (IsString (..))
27 import Data.Text (Text)
29 -- | Code for a message.
31 -- Content is expected to match this regexp: @[A-Z]{6}@.
32 newtype MessageCode = MessageCode { getMessageCode :: String } deriving newtype (Eq, Show, IsString)
34 -- | Code for a segment group.
36 -- It's a code local to the message definition.
37 newtype GroupCode = GroupCode { getGroupCode :: String } deriving newtype (Eq, Show, IsString)
39 -- | Code for a segment.
41 -- Content is expected to match this regexp: @[A-Z]{3}@.
43 -- Standard segment codes are expected to match this regexp: @UN[A-Z]@.
44 newtype SegmentCode = SegmentCode { getSegmentCode :: String } deriving newtype (Eq, Show, IsString)
46 -- | Code for a composite element.
48 -- Content is expected to match this regexp: @C[0-9]{3}@.
50 -- It can also be used for standalone composites, frequently in standard segment
51 -- definitions. In this case the codes are expected to match this regexp: @S[0-9]{3}@.
52 newtype CompositeCode = CompositeCode String deriving newtype (Eq, Show, IsString)
54 -- | Code for a simple element.
56 -- Content is expected to match this regexp: @[0-9]{4}@.
57 newtype SimpleCode = SimpleCode String deriving newtype (Eq, Show, IsString)
59 -- | Annotation of the position of the value relative to the parent value.
61 -- Content is expected to match this regexp: @[0-9]{3,4}@.
65 -- > "010" :: Position
66 -- > "0210" :: Position
68 -- See 'Text.Edifact.Parsing.position' for how to parse one.
69 newtype Position = Position { getPosition :: String } deriving newtype (Eq, Show, IsString)
71 -- | Representation of a simple component.
73 -- When defined by the 'Text.Edifact.Parsing.numeric' combinator, the simple
74 -- component will produce a 'Number'.
76 -- When parsed by the 'Text.Edifact.Parsing.alphaNumeric' or
77 -- 'Text.Edifact.Parsing.alpha' combinators, the simple component will produce a
78 -- 'Text.Edifact.Types.String' from the raw textual representation.
79 data Primitive = String Text -- ^ Default representation of a simple component.
80 | Number Scientific -- ^ Representation of a numerical simple component.
81 deriving stock (Eq, Show)
83 -- | String like primitive values can be constructed via overloaded strings.
84 -- This is convenient, but might be removed.
85 instance IsString Primitive where
86 fromString = String . fromString
88 -- | Recursive data structure to represent parsed Edifact values.
89 data Value = Message MessageCode [(Position, [Value])]
90 | Group GroupCode [(Position, [Value])]
91 | Segment SegmentCode [(Position, Maybe Value)]
92 | Composite CompositeCode [(Position, Maybe Value)]
93 | Simple SimpleCode Primitive
94 deriving stock (Show, Eq)
96 -- | Defines the special charactors the parser should respect.
98 -- This is defined in every payload via the @UNA@ segment (first segment expected).
99 data Syntax = Syntax { compositeSeparator :: Char
100 , elementSeparator :: Char
101 , decimalSign :: Char
103 , segmentSeparator :: Char
106 -- | Default value to initialize the parser.
108 -- > Syntax { compositeSeparator = ':'
109 -- > , elementSeparator = '+'
110 -- > , decimalSign = '.'
112 -- > , segmentSeparator = '\''
115 -- Those default charactors should be considered as recommended values rather
116 -- than official default values.
117 defaultSyntax :: Syntax
119 Syntax { compositeSeparator = ':'
120 , elementSeparator = '+'
123 , segmentSeparator = '\''