module Bricks.UnquotedString
(
Str'Unquoted (..)
, str'tryUnquoted
, str'unquoted'orThrow
, str'canRenderUnquoted
, char'canRenderUnquoted
) where
import Bricks.Keyword
import Bricks.Internal.Prelude
import Bricks.Internal.Text (Text)
import qualified Bricks.Internal.Text as Text
import qualified Data.Char as Char
import qualified Data.List as List
import Prelude (error)
newtype Str'Unquoted = Str'Unquoted'Unsafe { str'unquotedToStatic :: Text }
instance Show Str'Unquoted
where
showsPrec _ x = ("unquoted " <>) . shows (str'unquotedToStatic x)
str'tryUnquoted :: Text -> Maybe Str'Unquoted
str'tryUnquoted x =
if str'canRenderUnquoted x then Just (Str'Unquoted'Unsafe x) else Nothing
str'unquoted'orThrow :: Text -> Str'Unquoted
str'unquoted'orThrow x =
if str'canRenderUnquoted x then Str'Unquoted'Unsafe x else
error $ "String " <> show x <> " cannot render unquoted"
str'canRenderUnquoted :: Text -> Bool
str'canRenderUnquoted x =
Text.all char'canRenderUnquoted x
&& not (Text.null x)
&& List.all ((/= x) . keywordText) keywords
char'canRenderUnquoted :: Char -> Bool
char'canRenderUnquoted c =
Char.isLetter c || c == '-' || c == '_'