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 :: Text -> Maybe Text -> Text -> Result Text
useShortcut = [([Text], Maybe Text -> Text -> Result Text)]
-> Text -> Maybe Text -> Text -> Result Text
useShortcutFrom [([Text], Maybe Text -> Text -> Result Text)]
allShortcuts
useShortcutFrom
:: [([Text], Shortcut)]
-> Text
-> Maybe Text
-> Text
-> Result Text
useShortcutFrom :: [([Text], Maybe Text -> Text -> Result Text)]
-> Text -> Maybe Text -> Text -> Result Text
useShortcutFrom shortcuts :: [([Text], Maybe Text -> Text -> Result Text)]
shortcuts name :: Text
name option :: Maybe Text
option link :: Text
link =
case (([Text], Maybe Text -> Text -> Result Text) -> Bool)
-> [([Text], Maybe Text -> Text -> Result Text)]
-> [([Text], Maybe Text -> Text -> Result Text)]
forall a. (a -> Bool) -> [a] -> [a]
filter ([Text], Maybe Text -> Text -> Result Text) -> Bool
givenShortcut [([Text], Maybe Text -> Text -> Result Text)]
shortcuts of
[] -> String -> Result Text
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (Text -> Text -> String
forall r. FormatType r => Text -> r
format "there's no shortcut named '{}'" Text
name)
[sh :: ([Text], Maybe Text -> Text -> Result Text)
sh] -> ([Text], Maybe Text -> Text -> Result Text)
-> Maybe Text -> Text -> Result Text
forall a b. (a, b) -> b
snd ([Text], Maybe Text -> Text -> Result Text)
sh Maybe Text
option Text
link
_ -> String -> Result Text
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (Text -> Text -> String
forall r. FormatType r => Text -> r
format "there's more than one shortcut named '{}'" Text
name)
where
givenShortcut :: ([Text], Shortcut) -> Bool
givenShortcut :: ([Text], Maybe Text -> Text -> Result Text) -> Bool
givenShortcut (names :: [Text]
names, _) = Text
name Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
names