3
3
module Main (main ) where
4
4
5
5
import Codec.CBOR.Cuddle.CBOR.Gen (generateCBORTerm )
6
- import Codec.CBOR.Cuddle.CDDL (Name (.. ))
7
- import Codec.CBOR.Cuddle.CDDL.Resolve
8
- ( fullResolveCDDL ,
9
- )
6
+ import Codec.CBOR.Cuddle.CDDL (Name (.. ), sortCDDL )
7
+ import Codec.CBOR.Cuddle.CDDL.Resolve (
8
+ fullResolveCDDL ,
9
+ )
10
10
import Codec.CBOR.Cuddle.Parser (pCDDL )
11
11
import Codec.CBOR.Cuddle.Pretty ()
12
12
import Codec.CBOR.FlatTerm (toFlatTerm )
@@ -22,131 +22,131 @@ import System.Exit (exitFailure, exitSuccess)
22
22
import System.IO (hPutStrLn , stderr )
23
23
import System.Random (getStdGen )
24
24
import Text.Megaparsec (ParseErrorBundle , Parsec , errorBundlePretty , runParser )
25
- import Codec.CBOR.Cuddle.CDDL (sortCDDL )
26
25
27
26
data Opts = Opts Command String
28
27
29
28
data Command
30
- = Format FormatOpts
31
- | Validate
32
- | GenerateCBOR GenOpts
29
+ = Format FormatOpts
30
+ | Validate
31
+ | GenerateCBOR GenOpts
33
32
34
33
-- | Various formats for outputtting CBOR
35
34
data CBOROutputFormat
36
- = AsCBOR
37
- | AsPrettyCBOR
38
- | AsTerm
39
- | AsFlatTerm
35
+ = AsCBOR
36
+ | AsPrettyCBOR
37
+ | AsTerm
38
+ | AsFlatTerm
40
39
41
40
pCBOROutputFormat :: ReadM CBOROutputFormat
42
41
pCBOROutputFormat = eitherReader $ \ case
43
- " cbor" -> Right AsCBOR
44
- " pretty" -> Right AsPrettyCBOR
45
- " term" -> Right AsTerm
46
- " flat" -> Right AsFlatTerm
47
- s -> Left s
42
+ " cbor" -> Right AsCBOR
43
+ " pretty" -> Right AsPrettyCBOR
44
+ " term" -> Right AsTerm
45
+ " flat" -> Right AsFlatTerm
46
+ s -> Left s
48
47
49
48
data GenOpts = GenOpts
50
- { itemName :: T. Text,
51
- outputFormat :: CBOROutputFormat
52
- }
49
+ { itemName :: T. Text
50
+ , outputFormat :: CBOROutputFormat
51
+ }
53
52
54
53
pGenOpts :: Parser GenOpts
55
54
pGenOpts =
56
- GenOpts
57
- <$> strOption
58
- ( long " rule"
59
- <> short ' r'
60
- <> metavar " RULE"
61
- <> help " Name of the CDDL rule to generate a CBOR term for"
62
- )
63
- <*> option
64
- pCBOROutputFormat
65
- ( long " format"
66
- <> short ' f'
67
- <> help " Output format"
68
- <> value AsCBOR
69
- )
55
+ GenOpts
56
+ <$> strOption
57
+ ( long " rule"
58
+ <> short ' r'
59
+ <> metavar " RULE"
60
+ <> help " Name of the CDDL rule to generate a CBOR term for"
61
+ )
62
+ <*> option
63
+ pCBOROutputFormat
64
+ ( long " format"
65
+ <> short ' f'
66
+ <> help " Output format"
67
+ <> value AsCBOR
68
+ )
70
69
71
70
newtype FormatOpts = FormatOpts
72
- { sort :: Bool }
71
+ { sort :: Bool }
73
72
74
73
pFormatOpts :: Parser FormatOpts
75
74
pFormatOpts =
76
- FormatOpts
77
- <$> switch
78
- ( long " sort-rules"
79
- <> help " Sort the CDDL rule definitions before printing."
80
- )
75
+ FormatOpts
76
+ <$> switch
77
+ ( long " sort-rules"
78
+ <> help " Sort the CDDL rule definitions before printing."
79
+ )
81
80
82
81
opts :: Parser Opts
83
82
opts =
84
- Opts
85
- <$> subparser
86
- ( command
87
- " format"
88
- ( info
89
- ( Format <$> pFormatOpts)
90
- ( progDesc " Format the provided CDDL file"
91
- )
92
- )
93
- <> command
94
- " validate"
95
- ( info
96
- (pure Validate )
97
- ( progDesc " Validate the provided CDDL file"
83
+ Opts
84
+ <$> subparser
85
+ ( command
86
+ " format"
87
+ ( info
88
+ (Format <$> pFormatOpts)
89
+ ( progDesc " Format the provided CDDL file"
90
+ )
98
91
)
92
+ <> command
93
+ " validate"
94
+ ( info
95
+ (pure Validate )
96
+ ( progDesc " Validate the provided CDDL file"
97
+ )
98
+ )
99
+ <> command
100
+ " gen"
101
+ ( info
102
+ (GenerateCBOR <$> pGenOpts)
103
+ ( progDesc " Generate a CBOR term matching the schema"
104
+ )
105
+ )
99
106
)
100
- <> command
101
- " gen"
102
- ( info
103
- (GenerateCBOR <$> pGenOpts)
104
- ( progDesc " Generate a CBOR term matching the schema"
105
- )
106
- )
107
- )
108
- <*> argument str (metavar " CDDL_FILE" )
107
+ <*> argument str (metavar " CDDL_FILE" )
109
108
110
109
main :: IO ()
111
110
main = do
112
- options <-
113
- execParser $
114
- info
115
- (opts <**> helper)
116
- ( fullDesc
117
- <> progDesc " Manipulate CDDL files"
118
- <> header " cuddle"
119
- )
120
- run options
111
+ options <-
112
+ execParser $
113
+ info
114
+ (opts <**> helper)
115
+ ( fullDesc
116
+ <> progDesc " Manipulate CDDL files"
117
+ <> header " cuddle"
118
+ )
119
+ run options
121
120
122
121
run :: Opts -> IO ()
123
122
run (Opts cmd cddlFile) = do
124
- parseFromFile pCDDL cddlFile >>= \ case
125
- Left err -> do
126
- putStrLnErr $ errorBundlePretty err
127
- exitFailure
128
- Right res -> case cmd of
129
- Format fOpts -> let defs = if sort fOpts then sortCDDL res else res in
130
- putDocW 80 $ pretty defs
131
- Validate -> case fullResolveCDDL res of
132
- Left err -> putStrLnErr (show err) >> exitFailure
133
- Right _ -> exitSuccess
134
- (GenerateCBOR x) -> case fullResolveCDDL res of
135
- Left err -> putStrLnErr (show err) >> exitFailure
136
- Right mt -> do
137
- stdGen <- getStdGen
138
- let term = generateCBORTerm mt (Name $ itemName x) stdGen
139
- in case outputFormat x of
140
- AsTerm -> print term
141
- AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
142
- AsCBOR -> print . toStrictByteString $ encodeTerm term
143
- AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
123
+ parseFromFile pCDDL cddlFile >>= \ case
124
+ Left err -> do
125
+ putStrLnErr $ errorBundlePretty err
126
+ exitFailure
127
+ Right res -> case cmd of
128
+ Format fOpts ->
129
+ let defs = if sort fOpts then sortCDDL res else res
130
+ in putDocW 80 $ pretty defs
131
+ Validate -> case fullResolveCDDL res of
132
+ Left err -> putStrLnErr (show err) >> exitFailure
133
+ Right _ -> exitSuccess
134
+ (GenerateCBOR x) -> case fullResolveCDDL res of
135
+ Left err -> putStrLnErr (show err) >> exitFailure
136
+ Right mt -> do
137
+ stdGen <- getStdGen
138
+ let term = generateCBORTerm mt (Name $ itemName x) stdGen
139
+ in case outputFormat x of
140
+ AsTerm -> print term
141
+ AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
142
+ AsCBOR -> print . toStrictByteString $ encodeTerm term
143
+ AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
144
144
145
145
putStrLnErr :: String -> IO ()
146
146
putStrLnErr = hPutStrLn stderr
147
147
148
148
parseFromFile ::
149
- Parsec e T. Text a ->
150
- String ->
151
- IO (Either (ParseErrorBundle T. Text e ) a )
149
+ Parsec e T. Text a ->
150
+ String ->
151
+ IO (Either (ParseErrorBundle T. Text e ) a )
152
152
parseFromFile p file = runParser p file <$> T. readFile file
0 commit comments