Skip to content

Commit

Permalink
docs: Small fixes to sample code in tutorial (#165)
Browse files Browse the repository at this point in the history
* docs: Fix typos in tutorial

* docs: Fix data type in tutorial code sample

* docs: Add minimal error handling to tutorial code sample

The code will crash a lot without this error handling.
(Especially when editing the CSV file in Emacs)

---------

Co-authored-by: Sridhar Ratnakumar <[email protected]>
  • Loading branch information
rvl and srid authored Aug 21, 2024
1 parent aab624b commit 16e2752
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions docs/tutorial/01-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ instance IsRoute Date where
routeUniverse () = [] -- need model for this
```

1. We don't need any special [[model]] to encode a `Day` route, thus `RouteModel` is a unit. But we'll modify this in next step (to implement `routeUniverse`).
2. `toPrism_` is an Ema function that converts the optics-core `Prism'` into a coercible `Prism_` type that Ema internally uses. A route prism knows how to encode and decode the `Day` route. Our route `Prism'` is built using `formatTime` and `parseTimeM`.
1. We don't need any special [[model]] to encode a `Date` route, thus `RouteModel` is a unit. But we'll modify this in next step (to implement `routeUniverse`).
2. `toPrism_` is an Ema function that converts the optics-core `Prism'` into a coercible `Prism_` type that Ema internally uses. A route prism knows how to encode and decode the `Date` route. Our route `Prism'` is built using `formatTime` and `parseTimeM`.
3. We will implement `routeUniverse` in the next step of the tutorial

The result is that we can use the function `routeUrl` to get the URL to our routes. Let's see this in action in GHCi (run `bin/repl` in the template repository):
Expand All @@ -58,7 +58,7 @@ ghci> -- First get hold of the route Prism, which is passed to `siteOutput`
ghci> let rp = fromPrism_ $ routePrism @Route ()
ghci> Ema.routeUrl rp Route_Index
"" -- The 'index.html' is dropped as it is redundant in HTML.
ghci> Ema.routeUrl rp $ Route_Day $ Date (2022, 04, 23)
ghci> Ema.routeUrl rp $ Route_Date $ Date (2022, 04, 23)
"date/2022-04-23.html"
```

Expand All @@ -78,4 +78,4 @@ Just (Route_Date (Date (2022,4,23)))
See [[route]] for details.

{.last}
[Next]{.next}, [[02-model|we will explain]] how to define our `moods.csv` model and render it .
[Next]{.next}, [[02-model|we will explain]] how to define our `moods.csv` model and render it.
1 change: 1 addition & 0 deletions docs/tutorial/02-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ data Model = Model
}

data Mood = Bad | Neutral | Good
deriving stock (Show, Read)
```

Now we want to *associate* our `Route` type from [[01-routes]] with this `Model`. This can be done as follows:
Expand Down
5 changes: 3 additions & 2 deletions docs/tutorial/03-dynamic.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ In the case of our mood tracker, we will use the [fsnotify](https://hackage.hask
logInfoNS "fsnotify" "Waiting for fs event ..."
evt <- liftIO $ readChan ch
logInfoNS "fsnotify" $ "Got fs event: " <> show evt
setModel =<< readModel (FSNotify.eventPath evt)
handle (\(e :: IOException) -> logErrorNS "fsnotify" (show e)) $
setModel =<< readModel (FSNotify.eventPath evt)
loop
loop
where
readModel fp = do
s <- readFileLBS fp
case toList <$> Csv.decode Csv.NoHeader s of
Left err -> throw $ userError err
Left err -> logErrorNS "csv" (toText err) >> pure (Model mempty)
Right moods -> pure $ Model $ Map.fromList moods
-- Observe changes to a directory path, and return the `Chan` of its events.
watchDirForked :: FilePath -> IO (Chan FSNotify.Event)
Expand Down

0 comments on commit 16e2752

Please sign in to comment.