module ShortcutLinks
( Result(..)
, Shortcut
, allShortcuts
, useShortcut
, useShortcutFrom
) where
import Data.Text (Text)
import ShortcutLinks.All (Result (..), Shortcut, allShortcuts)
import ShortcutLinks.Utils (format)
useShortcut
:: Text
-> Maybe Text
-> Text
-> Result Text
useShortcut = useShortcutFrom allShortcuts
useShortcutFrom
:: [([Text], Shortcut)]
-> Text
-> Maybe Text
-> Text
-> Result Text
useShortcutFrom shortcuts name option link =
case filter givenShortcut shortcuts of
[] -> fail (format "there's no shortcut named '{}'" name)
[sh] -> snd sh option link
_ -> fail (format "there's more than one shortcut named '{}'" name)
where
givenShortcut :: ([Text], Shortcut) -> Bool
givenShortcut (names, _) = name `elem` names