yaml

moon add myfreess/yaml@0.1.0
Download zip
Author
Version
0.1.0
License
MIT
Last updated
11 months ago
Downloads
23
README

#YAML

A comprehensive YAML parsing and stringifying library for MoonBit, supporting YAML 1.2.x and common YAML 1.1 features.

This library is ported from Deno std/yaml (js-yaml v3.13.1) and provides full-featured YAML processing capabilities including multiple schema types, flow and block styles, and custom parsing options.

#Features

  • Parse YAML strings to MoonBit values
  • Stringify MoonBit values to YAML
  • Multiple schema types (failsafe, json, core, default)
  • Flow and block styles support
  • Custom parsing and stringification options
  • Comprehensive error handling

#
RepresentFn

typealias (T, StyleVariant?) -> String as RepresentFn[T]

Type alias for represent function

#
YamlParseError

type YamlParseError

Suberror type for YAML parsing

#
DumperState

type DumperState

#
KindType

pub enum KindType {
Sequence
Scalar
Mapping
}

Kind of YAML node
impl Eq for KindType
impl Show for KindType

#
ParseOptions

pub struct ParseOptions {
schema : String
allow_duplicate_keys : Bool
on_warning : (YamlError) -> Unit?
}

Parse options for YAML parsing

#
ParseOptions::default

fn ParseOptions::default() -> ParseOptions

Default parse options

#
ParseOptions::new

Create new parse options (alias for default)

#
ParserState

type ParserState

YAML Parser implementation Simplified version based on js-yaml v3.13.1

#
Position

pub struct Position {
line : Int
column : Int
index : Int
}

Position tracking in input
impl Eq for Position
impl Show for Position

#
Schema

pub struct Schema {
implicit_types : Array[YamlType[YamlValue]]
explicit_types : Array[YamlType[YamlValue]]
type_map : TypeMap
}

YAML Schema containing implicit and explicit types

#
SchemaType

pub enum SchemaType {
FailSafe
Json
Core
Default
Extended
}

Schema type for different YAML standards
impl Eq for SchemaType
impl Show for SchemaType

#
StringifyOptions

pub struct StringifyOptions {
indent : Int
array_indent : Bool
skip_invalid : Bool
flow_level : Int
styles : Map[String, StyleVariant]
schema : String
sort_keys : Bool
line_width : Int
use_anchors : Bool
compat_mode : Bool
condense_flow : Bool
}

Stringify options for YAML serialization

#
StringifyOptions::default

Default stringify options

#
StyleVariant

pub enum StyleVariant {
Lowercase
Uppercase
Camelcase
Decimal
Binary
Octal
Hexadecimal
Literal
Folded
Plain
DoubleQuoted
SingleQuoted
}

Style variations for stringify
impl Eq for StyleVariant

#
TypeMap

pub struct TypeMap {
fallback : Map[String, YamlType[YamlValue]]
mapping : Map[String, YamlType[YamlValue]]
scalar : Map[String, YamlType[YamlValue]]
sequence : Map[String, YamlType[YamlValue]]
}

Type map for organizing types by kind

#
YamlError

pub enum YamlError {
ParseError(String, Int, Int)
TypeError(String)
SyntaxError(String)
ResolveError(String)
ConstructError(String)
}

Error types for YAML processing
impl Eq for YamlError
impl Show for YamlError

#
YamlType

pub struct YamlType[T] {
tag : String
kind : KindType
predicate : (YamlValue) -> Bool
represent : (T, StyleVariant?) -> String?
default_style : StyleVariant?
resolve : (YamlValue) -> Bool
construct : (YamlValue) -> T
}

YAML type definition structure Generic type T represents the actual MoonBit type this YAML type handles

#
YamlValue

pub enum YamlValue {
Null
Bool(Bool)
Int(Int)
Float(Double)
String(String)
Array(Array[YamlValue])
Object(Map[String, YamlValue])
}

Represents any YAML value
impl Eq for YamlValue
impl Show for YamlValue

#
AMPERSAND

let AMPERSAND : Int

#
ASTERISK

let ASTERISK : Int

#
BACKSLASH

let BACKSLASH : Int

#
BOM

let BOM : Int

Character code constants

#
CARRIAGE_RETURN

let CARRIAGE_RETURN : Int

#
COLON

let COLON : Int

#
COMMA

let COMMA : Int

#
COMMERCIAL_AT

let COMMERCIAL_AT : Int

#
DOT

let DOT : Int

#
DOUBLE_QUOTE

let DOUBLE_QUOTE : Int

#
EXCLAMATION

let EXCLAMATION : Int

#
GRAVE_ACCENT

let GRAVE_ACCENT : Int

#
GREATER_THAN

let GREATER_THAN : Int

#
LEFT_CURLY_BRACKET

let LEFT_CURLY_BRACKET : Int

#
LEFT_SQUARE_BRACKET

let LEFT_SQUARE_BRACKET : Int

#
LINE_FEED

let LINE_FEED : Int

#
MINUS

let MINUS : Int

#
PERCENT

let PERCENT : Int

#
PLUS

let PLUS : Int

#
QUESTION

let QUESTION : Int

#
RIGHT_CURLY_BRACKET

let RIGHT_CURLY_BRACKET : Int

#
RIGHT_SQUARE_BRACKET

let RIGHT_SQUARE_BRACKET : Int

#
SHARP

let SHARP : Int

#
SINGLE_QUOTE

let SINGLE_QUOTE : Int

#
SMALLER_THAN

let SMALLER_THAN : Int

#
SPACE

let SPACE : Int

#
TAB

let TAB : Int

#
VERTICAL_LINE

let VERTICAL_LINE : Int

#
binary_type

fn binary_type() -> YamlType[String]

Binary type handler Represents binary data encoded in base64

#
bool_type

fn bool_type() -> YamlType[Bool]

Boolean type handler

#
calculate_yaml_size

fn calculate_yaml_size(value : YamlValue) -> Int

Calculate size (number of elements) in YAML value

#
clone_yaml_value

fn clone_yaml_value(value : YamlValue) -> YamlValue

Clone a YamlValue

#
codepoint_to_string

fn codepoint_to_string(codepoint : Int) -> String

Convert Unicode code point to string

#
core_schema

fn core_schema() -> Schema

Standard YAML's core schema Functionally the same as JSON schema

#
count_leading_whitespace

fn count_leading_whitespace(s : String) -> Int

Count leading whitespace characters

#
decimal_char_to_number

fn decimal_char_to_number(c : Int) -> Int

Convert decimal character to number

#
default_schema

fn default_schema() -> Schema

Default YAML schema (not in YAML specification) Supports: core + binary, omap, pairs, set For now, same as core until we implement additional types

#
determine_quote_style

fn determine_quote_style(s : String) -> StyleVariant

Smart quote detection for strings

#
escape_yaml_string

fn escape_yaml_string(s : String) -> String

Escape special characters for YAML output

#
extended_schema

fn extended_schema() -> Schema

Extended YAML schema (not in YAML specification) Supports: default + regexp, undefined For now, same as default until we implement additional types

#
failsafe_schema

fn failsafe_schema() -> Schema

Standard YAML's failsafe schema Supports: generic mappings, generic sequences, generic strings

#
flatten_yaml_object

fn flatten_yaml_object(value : YamlValue, prefix? : String) -> Map[String, YamlValue]

Flatten nested YAML object to dot notation

#
float_type

fn float_type() -> YamlType[Double]

Float type handler

#
get_all_keys

fn get_all_keys(value : YamlValue, prefix? : String) -> Array[String]

Get all keys from a YAML object (including nested)

#
get_schema

fn get_schema(name : String) -> Schema

Get schema by name

#
get_value_at_path

fn get_value_at_path(value : YamlValue, path : String) -> YamlValue?

Get value at path (dot notation)

#
get_yaml_type_name

fn get_yaml_type_name(value : YamlValue) -> String

Get the type name of a YAML value

#
hex_char_to_number

fn hex_char_to_number(c : Int) -> Int

Convert hex character to number

#
int_type

fn int_type() -> YamlType[Int]

Integer type handler

#
is_decimal_digit

fn is_decimal_digit(c : Int) -> Bool

Check if character is a valid decimal digit

#
is_document_end

fn is_document_end(line : String) -> Bool

Check if string is a YAML document end (...)

#
is_document_start

fn is_document_start(line : String) -> Bool

Check if string is a valid YAML document start (---)

#
is_empty_line

fn is_empty_line(line : String) -> Bool

Check if a line is empty or contains only whitespace

#
is_eol

fn is_eol(c : Int) -> Bool

Check if character is end-of-line

#
is_flow_indicator

fn is_flow_indicator(c : Int) -> Bool

Check if character is a flow indicator

#
is_hex_digit

fn is_hex_digit(c : Int) -> Bool

Check if character is a valid hexadecimal digit

#
is_negative_zero

fn is_negative_zero(f : Double) -> Bool

#
is_numeric_string

fn is_numeric_string(s : String) -> Bool

Check if string looks like a number

#
is_object

fn is_object(value : YamlValue) -> Bool

#
is_plain_object

fn is_plain_object(value : YamlValue) -> Bool

#
is_printable

fn is_printable(c : Int) -> Bool

Check if character is printable

#
is_whitespace_or_eol

fn is_whitespace_or_eol(c : Int) -> Bool

Check if character is whitespace or end-of-line

#
is_yaml_empty

fn is_yaml_empty(value : YamlValue) -> Bool

Check if YAML value is empty

#
json_schema

fn json_schema() -> Schema

Standard YAML's JSON schema Supports: failsafe + nulls, booleans, integers, floats

#
map_type

fn map_type() -> YamlType[Map[String, YamlValue]]

Mapping type handler

#
merge_type

fn merge_type() -> YamlType[YamlValue]

Merge type handler Handles YAML merge keys (<<)

#
merge_yaml_objects

fn merge_yaml_objects(target : YamlValue, source : YamlValue) -> YamlValue

Merge two YAML objects (deep merge)

#
needs_quoting

fn needs_quoting(s : String) -> Bool

Check if string needs quoting in YAML

#
normalize_line_endings

fn normalize_line_endings(input : String) -> String

Normalize line endings to LF

#
null_type

fn null_type() -> YamlType[Unit]

Null type handler

#
omap_type

fn omap_type() -> YamlType[Array[(String, YamlValue)]]

Ordered map (omap) type handler Represents an ordered mapping as an array of key-value pairs

#
pad_string

fn pad_string(s : String, width : Int) -> String

Pad string to specified width with spaces

#
pairs_type

fn pairs_type() -> YamlType[Array[(String, YamlValue)]]

Pairs type handler Similar to omap but allows duplicate keys

#
parse

fn parse(content : String, options? : ParseOptions) -> YamlValue raise YamlParseError

Parse a YAML string and return the parsed value

#
parse_all

fn parse_all(content : String, options? : ParseOptions) -> Array[YamlValue] raise YamlParseError

Parse multiple YAML documents from a single string Returns an array of parsed documents

#
process_folded_string

fn process_folded_string(s : String) -> String

Process folded string (YAML >) Folds newlines into spaces while preserving paragraph breaks

#
process_literal_string

fn process_literal_string(s : String) -> String

Process literal string (YAML |) Preserves line breaks exactly as written

#
resolve_scalar

fn resolve_scalar(data : String, schema : Schema) -> YamlValue

Resolve a scalar value according to schema

#
sanitize_input

fn sanitize_input(input : String) -> String

Sanitize input string for parsing Handles BOM, line endings, and ensures proper termination

#
seq_type

fn seq_type() -> YamlType[Array[YamlValue]]

Sequence type handler

#
set_type

fn set_type() -> YamlType[Array[String]]

Set type handler Represents a set as a mapping with null values

#
set_value_at_path

fn set_value_at_path(value : YamlValue, path : String, new_value : YamlValue) -> YamlValue

Set value at path (dot notation)

#
split_lines

fn split_lines(input : String) -> Array[String]

Split string into lines, preserving line ending information

#
str_type

fn str_type() -> YamlType[String]

String type handler

#
string_ends_with

fn string_ends_with(s : String, suffix : String) -> Bool

Check if string ends with suffix

#
stringify

fn stringify(data : YamlValue, options? : StringifyOptions) -> String

Convert a value to YAML string

#
suggest_block_style

fn suggest_block_style(s : String) -> StyleVariant

Determine if string should use literal (|) or folded (>) style

#
timestamp_type

fn timestamp_type() -> YamlType[String]

Timestamp type handler Represents date/time values

#
trim_whitespace

fn trim_whitespace(s : String) -> String

Trim leading and trailing whitespace

#
unescape_yaml_string

fn unescape_yaml_string(s : String) -> String

Unescape YAML string (decode escape sequences)

#
unflatten_yaml_object

fn unflatten_yaml_object(flat : Map[String, YamlValue]) -> YamlValue

Unflatten dot notation back to nested YAML object

#
wrap_text

fn wrap_text(text : String, width : Int) -> Array[String]

Wrap text to specified line width

#
yaml_array

fn yaml_array(values : Array[YamlValue]) -> YamlValue

Create a YAML array from values

#
yaml_object

fn yaml_object(pairs : Array[(String, YamlValue)]) -> YamlValue

Create a YAML object from key-value pairs

#
yaml_value_is_null

fn yaml_value_is_null(value : YamlValue) -> Bool

Check if YamlValue is null

#
yaml_value_to_array

fn yaml_value_to_array(value : YamlValue) -> Array[YamlValue]?

Convert YamlValue to Array if possible

#
yaml_value_to_bool

fn yaml_value_to_bool(value : YamlValue) -> Bool?

Convert YamlValue to Bool if possible

#
yaml_value_to_debug_string

fn yaml_value_to_debug_string(value : YamlValue) -> String

Get string representation of a YAML value for debugging

#
yaml_value_to_double

fn yaml_value_to_double(value : YamlValue) -> Double?

Convert YamlValue to Double if possible

#
yaml_value_to_int

fn yaml_value_to_int(value : YamlValue) -> Int?

Convert YamlValue to Int if possible

#
yaml_value_to_json_string

fn yaml_value_to_json_string(value : YamlValue) -> String

Convert YAML value to JSON-like string representation

#
yaml_value_to_object

fn yaml_value_to_object(value : YamlValue) -> Map[String, YamlValue]?

Convert YamlValue to Object if possible

#
yaml_value_to_string

fn yaml_value_to_string(value : YamlValue) -> String?

Convert YamlValue to String if possible

#
yaml_values_equal

fn yaml_values_equal(a : YamlValue, b : YamlValue) -> Bool

Deep equality check for YAML values

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io