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