@@ -22,7 +22,26 @@ Rust and Idris are different languages, and the dominant derivable encoding libr
2222``` idris
2323export
2424serde : 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
188206With 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
191209public export
192210record 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