yamlet
Safe HaskellNone
LanguageGHC2021

Yamlet.Schema

Description

The core schema of YAML 1.2.2: the rules that give a scalar its value.

A decoder applies these rules to every scalar. A program that writes YAML can use them to check how a plain scalar reads back, e.g. 9.10 is a number, not a string.

Synopsis

Documentation

resolvePlain :: Text -> Value Source #

The value of a plain scalar without a tag. Not every such scalar is a string, e.g. null, true, 12, 0x1F and 1.5e3 are not. Quoted and block scalars are always strings.

A float whose exponent in scientific notation is beyond the range from -1000 to 1000, e.g. 1e1001, 10e1000 or 1e-1001, becomes infinity or zero, as a double does. The decoders reject such a number, because its value is not exact.

>>> map resolvePlain ["", "true", "0x1F", "1.5e3", ".inf", "yes", "9.10.3"]
[Null,Bool True,Int 31,Float (Finite 1500.0),Float Infinity,String "yes",String "9.10.3"]

resolveTagged :: Text -> Text -> Maybe Value Source #

The value of a scalar with the given resolved tag, e.g. tag:yaml.org,2002:int. Return Nothing if the text is not valid for a tag of the core schema. A scalar with another tag is a string.

A float beyond the limit becomes infinity or zero, as in resolvePlain.

>>> [resolveTagged floatTag "1", resolveTagged intTag "abc", resolveTagged seqTag "x", resolveTagged "!point" "1"]
[Just (Float (Finite 1.0)),Nothing,Nothing,Just (String "1")]

isPlainString :: Text -> Bool Source #

A plain scalar with the text is a string, e.g. 9.10.3 is a string, but 9.10 and true are not. The check ignores the syntax, so e.g. a: b passes. For both checks, use isPlainSafe.

>>> map isPlainString ["9.10.3", "9.10", "true", "a: b"]
[True,False,False,True]

isPlainSafe :: Text -> Bool Source #

The string reads back as the same string if it is a plain scalar in the block style, as a value or as a key. A key without ? must also have at most 1024 characters, which the check does not count. In a flow collection the characters ,[]{} need quotes too, so the check does not apply there.

>>> map isPlainSafe ["a:b", "a: b", "- a", "a #b", "9.10"]
[True,False,False,False,False]

isPlainPortable :: Text -> Bool Source #

As isPlainSafe, and common YAML 1.1 parsers also read the plain scalar as a string, e.g. not yes as a boolean or 12:30 as a number. The encoder writes a string without quotes only if it passes this check.

>>> map isPlainPortable ["a:b", "yes", "12:30", "2024-01-01", "9.10.3"]
[True,False,False,False,True]