module MdBlocks.Resolver ( ResolveError(..) , resolveBlock , formatResolveError ) where import Data.List (intercalate) import qualified Data.Text as T import MdBlocks.Address import MdBlocks.Types data ResolveError = BlockNotFound | DuplicateBlockId [Block] deriving (Show, Eq) resolveBlock :: Address -> [Block] -> Either ResolveError Block resolveBlock address blocks = case matches of [] -> Left BlockNotFound [block] -> Right block many -> Left (DuplicateBlockId many) where matches = [ b | b <- blocks , blockFile b == addressFile address , mdId (blockMeta b) == Just (addressId address) ] formatResolveError :: Address -> ResolveError -> String formatResolveError address err = case err of BlockNotFound -> "block not found: " ++ showAddress address DuplicateBlockId blocks -> "duplicate block id '" ++ T.unpack (addressId address) ++ "' in " ++ addressFile address ++ "\n found at " ++ formatLines blocks formatLines :: [Block] -> String formatLines blocks = "lines " ++ intercalate ", " (map (show . blockStart) blocks) showAddress :: Address -> String showAddress address = addressFile address ++ "#" ++ T.unpack (addressId address)