Skip to content

Commit 7fdcefd

Browse files
committed
Correcting data model part 1
1 parent 7c6b64b commit 7fdcefd

File tree

2 files changed

+107
-12
lines changed

2 files changed

+107
-12
lines changed

src/Preprocessor/Binary.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ module Preprocessor.Binary
66
import Data.Maybe
77
import Data.SortedMap
88
import Data.String
9-
9+
import Data.List.Quantifiers
1010
import System
11+
import System.File
12+
13+
import JSON.Simple
1114
1215
import Preprocessor.JSON
1316
@@ -16,10 +19,6 @@ import Preprocessor.JSON
1619

1720
```idris hide
1821
-- Temporary definitions that refer to names that will be defined in other modules
19-
data Book : Type
20-
%name Book book
21-
22-
toJSON : Book -> String
2322
```
2423

2524
## Argument Parsing
@@ -58,16 +57,29 @@ Since we are working in the context of `IO` we can """safely""" unwrap an `Eithe
5857
```idris
5958
unwrapCtx : Interpolation a => (ctx : Lazy String) -> Either a b -> IO b
6059
unwrapCtx ctx (Left x) = do
61-
putStrLn "Error: \{ctx}\n \{x}"
60+
_ <- fPutStrLn stderr "Error: \{ctx}\n \{x}"
6261
exitFailure
6362
unwrapCtx ctx (Right x) = pure x
63+
64+
Interpolation FileError where
65+
interpolate = show
66+
67+
Interpolation DecodingErr where
68+
interpolate = show
6469
```
6570

6671
## Parsing the Book Data
6772

73+
We need to read all of the input from standard in and parse it as our `[Context, Book]`, handling any errors that crop up.
74+
6875
```idris
76+
partial
6977
parseStdin : IO (Either String (Context, Book))
7078
parseStdin = do
79+
input <- unwrapCtx "Reading from standard input" =<< fRead stdin
80+
_ <- writeFile "output.json" input
81+
output : HList [Context, Book] <- unwrapCtx "Decoding JSON" $ decode input
82+
?what
7183
pure (Left "Not Yet implemented")
7284
```
7385

@@ -125,6 +137,7 @@ routeBook (ctx, book) =
125137
Our main simply glues it all together, matching on the parsed arguments, and delegating most of the rest of the work to the appropriate implementations.
126138

127139
```idris
140+
partial
128141
main : IO ()
129142
main = do
130143
args <- parseArgs >>= unwrapCtx "Parsing Arguments"
@@ -133,7 +146,7 @@ main = do
133146
NoArguments => do
134147
pair <- parseStdin >>= unwrapCtx "Parsing Input"
135148
output <- routeBook pair
136-
putStr (toJSON output)
149+
putStr (encode output)
137150
exitSuccess
138151
```
139152

src/Preprocessor/JSON.md

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,26 @@ Rust and Idris are different languages, and the dominant derivable encoding libr
2222
```idris
2323
export
2424
serde : Options
25-
serde = { sum := ObjectWithSingleField } defaultOptions
25+
serde = { sum := ObjectWithSingleField, replaceMissingKeysWithNull := True } defaultOptions
26+
27+
ne : String -> String
28+
ne "non_exhaustive" = "__non_exhaustive"
29+
ne x = x
30+
31+
export
32+
serdeNE : Options
33+
serdeNE = {fieldNameModifier := ne} serde
34+
35+
kebabCase : String -> String
36+
kebabCase = pack . map convertChar . unpack
37+
where
38+
convertChar : Char -> Char
39+
convertChar '_' = '-'
40+
convertChar c = c
41+
42+
export
43+
serdeKebab : Options
44+
serdeKebab = {fieldNameModifier := kebabCase} serde
2645
```
2746

2847
## The `PreprocessorContext` Type
@@ -62,7 +81,6 @@ record BookConfig where
6281
authors : List String
6382
description : Maybe String
6483
src : Path
65-
multilingual : Bool
6684
text_direction : Maybe TextDirection
6785
6886
%name BookConfig bookCfg
@@ -83,7 +101,7 @@ record BuildConfig where
83101
extra_watch_dirs: List Path
84102
85103
%name BuildConfig buildCfg
86-
%runElab derive "BuildConfig" [Show, Eq, customToJSON Export serde, customFromJSON Export serde]
104+
%runElab derive "BuildConfig" [Show, Eq, customToJSON Export serdeKebab, customFromJSON Export serdeKebab]
87105
```
88106

89107
#### RustConfig
@@ -183,15 +201,79 @@ We'll have to do something a little bit special to apply the JSON derives here,
183201
%runElab deriveMutual ["ChapterItem", "BookItem"] [Show, Eq, customToJSON Export serde, customFromJSON Export serde]
184202
```
185203

186-
### `Book` Proper
204+
## `Book` Proper
187205

188206
With everything else written, we only need to provide an equivalent of [`Book`](https://docs.rs/mdbook/latest/mdbook/book/struct.Book.html):
189207

190208
```idris
191209
public export
192210
record Book where
211+
constructor MkBook
193212
sections: List BookItem
213+
-- FIXME: For some reason we need this in here to get json-simple to behave and not just reduce this type to a list
214+
non_exhaustive: Maybe Bool
194215
195216
%name Book book
196-
%runElab derive "Book" [Show, Eq, customToJSON Export serde, customFromJSON Export serde]
217+
%runElab derive "Book" [Show, Eq, customToJSON Export serdeNE, customFromJSON Export serdeNE]
218+
```
219+
220+
## Interactive Testing
221+
222+
```idris
223+
exampleBookConfig : BookConfig
224+
exampleBookConfig = MkBookCfg
225+
{ title = Just "Idris 2 Book"
226+
, authors = ["Me", "You"]
227+
, description = Just "A Book"
228+
, src = "."
229+
, text_direction = Nothing
230+
}
231+
232+
exampleBuildConfig : BuildConfig
233+
exampleBuildConfig = MkBuildCfg
234+
{ build_dir = "."
235+
, create_missing = True
236+
, use_default_preprocessors = True
237+
, extra_watch_dirs = []
238+
}
239+
240+
exampleRustConfig : RustConfig
241+
exampleRustConfig = MkRustCfg Nothing
242+
243+
exampleConfig : Config
244+
exampleConfig = MkCfg
245+
{ book = exampleBookConfig
246+
, build = exampleBuildConfig
247+
, rust = exampleRustConfig
248+
}
249+
250+
exampleContext : Context
251+
exampleContext = MkCtx
252+
{ root = "."
253+
, config = exampleConfig
254+
, renderer = "html"
255+
, mdbook_version = "0.0.0"
256+
}
257+
258+
exampleChapter : BookItem
259+
exampleChapter = Chapter $ MkChapter
260+
{ name = "Name"
261+
, content = "Content"
262+
, number = Just [1, 2, 3]
263+
, sub_items = []
264+
, path = Nothing
265+
, source_path = Nothing
266+
, parent_names = []
267+
}
268+
269+
exampleBook : Book
270+
exampleBook = MkBook [ exampleChapter, Seperator ] Nothing
271+
272+
examplePair : (Context, Book)
273+
examplePair = (exampleContext, exampleBook)
274+
275+
example : IO ()
276+
example = do
277+
let output = encode examplePair
278+
putStrLn output
197279
```

0 commit comments

Comments
 (0)