-- | Finds and returns a @Section@ that matches the given query.
findSection :: MonadBooks m => Text -> m Section
findSection name
    | name `eq` "Reading"    = return Reading
    | name `eq` "To Be Read" = return ToBeRead
    | otherwise              = findOther
  where
    eq = (==) `on` tokens
    findOther = do
        matches <- gets $ Set.toAscList
                        . Set.filter match
                        . Set.map bookSection
        case matches of
            [o] -> return o
            _   -> throwError [st|Unable to find such a section.|]
      where
        match (Other x) = name `eq` x
        match  _        = False

--------------------------------------------------------------------------------
-- Formatting

-- | Formats a single book in a nice readable format.
findSection :: MonadBooks m => Text -> m Section
findSection name
    | name `eq` "Reading"    = return Reading
    | name `eq` "To Be Read" = return ToBeRead
    | otherwise              = findOther
  where
    eq = (==) `on` tokens
    findOther = do
        matches <- gets $ Set.toAscList
                        . Set.filter match
                        . Set.map bookSection
        case matches of
            [o] -> return o
            _   -> throwError [st|Unable to find such a section.|]
      where
        match (Other x) = name `eq` x
        match  _        = False

--------------------------------------------------------------------------------
-- Formatting

-- | Formats a single book in a nice readable format.
findSection name
    | name `eq` "Reading"    = return Reading
    | name `eq` "To Be Read" = return ToBeRead
    | otherwise              = findOther
  where
    eq = (==) `on` tokens
    findOther = do
        matches <- gets $ Set.toAscList
                        . Set.filter match
                        . Set.map bookSection
        case matches of
            [o] -> return o
            _   -> throwError [st|Unable to find such a section.|]
      where
        match (Other x) = name `eq` x
        match  _        = False

--------------------------------------------------------------------------------
-- Formatting

-- | Formats a single book in a nice readable format.