summaryrefslogtreecommitdiffstats
path: root/src/MdBlocks/Address.hs
blob: 8a32e0fb66c15418cc765a840022cc6145e9153f (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
{-# LANGUAGE OverloadedStrings #-}

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

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

import MdBlocks.Types

data Address
  = GlobalId Text
  | FileAddress FilePath 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"

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

      | otherwise ->
          Right
            (FileAddress
              (T.unpack file)
              (T.drop 1 ident))

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

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