summaryrefslogtreecommitdiffstats
path: root/src/MdBlocks/Address.hs
blob: 25ef26f7215da62f67b6f854fc3cd367bee5a545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE OverloadedStrings #-}

module MdBlocks.Address
  ( Address(..)
  , parseAddress
  , addressOf
  )
where

import Data.Text (Text)
import qualified Data.Text as T

import MdBlocks.Types

data Address = Address
  { addressFile :: FilePath
  , addressId   :: Text
  }
  deriving (Show, Eq)

parseAddress :: String -> Either String Address
parseAddress input =
  case T.breakOn "#" (T.pack input) of
    (file, ident)
      | T.null file ->
          Left "address must contain a file path before '#'"

      | T.null ident ->
          Left "address must contain a block id after '#'"

      | otherwise ->
          Right
            Address
              { addressFile = T.unpack file
              , addressId   = T.drop 1 ident
              }

addressOf :: Block -> Text
addressOf b =
  case mdId (blockMeta b) of
    Just ident ->
      T.pack (blockFile b) <> "#" <> ident

    Nothing ->
      T.pack (blockFile b)
        <> "#"
        <> T.pack (show (blockNumber b))