Safe Haskell | None |
---|---|
Language | Haskell2010 |
Strings compliant with Michelson constraints.
When writting a Michelson contract, you can only mention characters with
codes from [32 .. 126]
range in string literals. Same restriction applies
to string literals passed to alphanet.sh
.
However, Michelson allows some control sequences: "n"
. You have to write
it exactly in this form, and internally it will be transformed to line feed
character (this behaviour can be observed when looking at Pack
ed data).
See tests for examples of good and bad strings.
Synopsis
- newtype MText = MTextUnsafe {}
- mkMText :: Text -> Either Text MText
- mkMTextUnsafe :: HasCallStack => Text -> MText
- mkMTextCut :: Text -> MText
- writeMText :: MText -> Text
- takeMText :: Int -> MText -> MText
- dropMText :: Int -> MText -> MText
- isMChar :: Char -> Bool
- qqMText :: String -> Either Text String
- mt :: QuasiQuoter
- type family DoNotUseTextError where ...
- symbolToMText :: forall name. KnownSymbol name => MText
- labelToMText :: KnownSymbol name => Label name -> MText
- mtextHeadToUpper :: HasCallStack => MText -> MText
Documentation
Michelson string value.
This is basically a mere text with limits imposed by the language: https://tezos.gitlab.io/whitedoc/michelson.html#constants Although, this document seems to be not fully correct, and thus we applied constraints deduced empirically.
You construct an item of this type using one of the following ways:
- With QuasyQuotes when need to create a string literal.
>>>
[mt|Some text|]
MTextUnsafe { unMText = "Some text" }
- With
mkMText
when constructing from a runtime text value. - With
mkMTextUnsafe
orMTextUnsafe
when absolutelly sure that given string does not violate invariants. - With
mkMTextCut
when not sure about text contents and want to make it compliant with Michelson constraints.
Instances
mkMText :: Text -> Either Text MText Source #
Wrap a Haskell text into MText
, performing necessary checks.
You can use e.g. '\n'
character directly in supplied argument,
but attempt to use other bad characters like '\r'
will cause failure.
mkMTextUnsafe :: HasCallStack => Text -> MText Source #
Contruct MText
from a Haskell text, failing if provided Haskell text
is invalid Michelson string.
mkMTextCut :: Text -> MText Source #
Construct MText
from a Haskell text, eliminating all characters which
should not appear in Michelson strings.
Characters which can be displayed normally via escaping are preserved.
writeMText :: MText -> Text Source #
Print MText
for Michelson code, with all unusual characters escaped.
Misc
mt :: QuasiQuoter Source #
QuasyQuoter for constructing Michelson strings.
Validity of result will be checked at compile time. Note:
- slash must be escaped
- newline character must appear as '\n'
- use quotes as is
- other special characters are not allowed.
type family DoNotUseTextError where ... Source #
A type error asking to use MText
instead of ErrorMessage
.
DoNotUseTextError = TypeError (Text "`Text` is not isomorphic to Michelson strings," :$$: Text "consider using `MText` type instead") |
symbolToMText :: forall name. KnownSymbol name => MText Source #
Create a MText
from type-level string.
We assume that no unicode characters are used in plain Haskell code, so unless special tricky manipulations are used this should be safe.
labelToMText :: KnownSymbol name => Label name -> MText Source #
Create a MText
from label.
We assume that no unicode characters are used in plain Haskell code, so unless special tricky manipulations are used this should be safe.
mtextHeadToUpper :: HasCallStack => MText -> MText Source #
Leads first character of text to upper case.
For empty text this will throw an error.