{-# 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))