Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
yaml_roundtrip.h
1#ifndef SOURCEMETA_CORE_YAML_ROUNDTRIP_H_
2#define SOURCEMETA_CORE_YAML_ROUNDTRIP_H_
3
4#ifndef SOURCEMETA_CORE_YAML_EXPORT
5#include <sourcemeta/core/yaml_export.h>
6#endif
7
8#include <sourcemeta/core/json.h>
9#include <sourcemeta/core/jsonpointer.h>
10
11#include <cstdint> // std::uint8_t, std::size_t
12#include <optional> // std::optional
13#include <string> // std::string
14#include <unordered_map> // std::unordered_map
15#include <vector> // std::vector
16
17namespace sourcemeta::core {
18
19// Exporting symbols that depends on the standard C++ library is considered
20// safe
21// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
22#if defined(_MSC_VER)
23#pragma warning(push)
24#pragma warning(disable : 4251 4275)
25#endif
26
30class SOURCEMETA_CORE_YAML_EXPORT YAMLRoundTrip {
31public:
33 enum class ScalarStyle : std::uint8_t {
37 SingleQuoted,
39 DoubleQuoted,
41 Literal,
43 Folded
44 };
45
47 enum class CollectionStyle : std::uint8_t {
49 Block,
51 Flow
52 };
53
55 enum class Chomping : std::uint8_t {
57 Clip,
59 Strip,
61 Keep
62 };
63
65 struct NodeStyle {
67 std::optional<ScalarStyle> scalar;
69 std::optional<CollectionStyle> collection;
71 std::optional<Chomping> chomping;
73 std::size_t explicit_indent{0};
77 std::optional<std::string> block_content;
79 std::optional<std::string> plain_content;
81 std::optional<std::string> quoted_content;
84 std::optional<JSON> content_value;
86 std::optional<std::string> anchor;
88 std::optional<std::string> tag;
91 std::optional<JSON::Type> tag_type;
93 bool tag_before_anchor{false};
95 std::vector<std::string> comments_before;
97 std::optional<std::string> comment_inline;
99 std::optional<std::string> comment_on_indicator;
101 bool compact_flow{false};
103 bool padded_flow{false};
111 std::optional<std::size_t> sequence_size;
112 };
113
115 std::unordered_map<Pointer, NodeStyle, Pointer::Hasher> styles;
117 std::unordered_map<Pointer, std::string, Pointer::Hasher> aliases;
119 std::unordered_map<Pointer, ScalarStyle, Pointer::Hasher> key_styles;
121 std::unordered_map<Pointer, std::string, Pointer::Hasher> key_quoted_contents;
126 std::vector<std::string> document_prefix;
132 std::optional<std::string> document_start_comment;
134 std::optional<std::string> document_end_comment;
136 std::vector<std::string> leading_comments;
138 std::vector<std::string> post_start_comments;
140 std::vector<std::string> pre_end_comments;
142 std::vector<std::string> trailing_comments;
144 std::size_t indent_width{2};
146 bool byte_order_mark{false};
149 bool carriage_returns{false};
150};
151
152#if defined(_MSC_VER)
153#pragma warning(pop)
154#endif
155
156} // namespace sourcemeta::core
157
158#endif
@ Plain
Definition oauth_pkce.h:25
bool carriage_returns
Definition yaml_roundtrip.h:149
std::unordered_map< Pointer, std::string, Pointer::Hasher > key_quoted_contents
The original quoted content for each mapping key.
Definition yaml_roundtrip.h:121
CollectionStyle
The presentation style of a collection.
Definition yaml_roundtrip.h:47
bool byte_order_mark
Whether the document begins with a byte order mark.
Definition yaml_roundtrip.h:146
std::vector< std::string > pre_end_comments
The comments preceding the document end marker.
Definition yaml_roundtrip.h:140
std::vector< std::string > post_start_comments
The comments following the document start marker.
Definition yaml_roundtrip.h:138
std::unordered_map< Pointer, NodeStyle, Pointer::Hasher > styles
The recorded formatting for each node by pointer.
Definition yaml_roundtrip.h:115
std::unordered_map< Pointer, std::string, Pointer::Hasher > aliases
The alias name for each node referencing an anchor.
Definition yaml_roundtrip.h:117
std::optional< std::string > document_start_comment
The comment on the document start marker.
Definition yaml_roundtrip.h:132
Chomping
The chomping behavior applied to a block scalar.
Definition yaml_roundtrip.h:55
std::size_t indent_width
The indentation width used when emitting the document.
Definition yaml_roundtrip.h:144
std::vector< std::string > trailing_comments
The comments following the document.
Definition yaml_roundtrip.h:142
bool explicit_document_end
Whether the document ends with an explicit end marker.
Definition yaml_roundtrip.h:130
std::vector< std::string > document_prefix
Definition yaml_roundtrip.h:126
std::unordered_map< Pointer, ScalarStyle, Pointer::Hasher > key_styles
The scalar style for each mapping key by pointer.
Definition yaml_roundtrip.h:119
std::vector< std::string > leading_comments
The comments preceding the document.
Definition yaml_roundtrip.h:136
bool explicit_document_start
Whether the document begins with an explicit start marker.
Definition yaml_roundtrip.h:128
std::optional< std::string > document_end_comment
The comment on the document end marker.
Definition yaml_roundtrip.h:134
ScalarStyle
The presentation style of a scalar value.
Definition yaml_roundtrip.h:33
Definition yaml_roundtrip.h:30
Holds the formatting details recorded for a single node.
Definition yaml_roundtrip.h:65
std::optional< std::string > comment_inline
The comment on the same line as the node.
Definition yaml_roundtrip.h:97
std::optional< std::string > anchor
The anchor name attached to the node.
Definition yaml_roundtrip.h:86
bool padded_flow
Whether the flow collection is padded with a space inside its delimiters.
Definition yaml_roundtrip.h:103
std::optional< std::string > comment_on_indicator
The comment attached to a block scalar indicator.
Definition yaml_roundtrip.h:99
std::optional< JSON > content_value
Definition yaml_roundtrip.h:84
std::optional< std::string > quoted_content
The original quoted scalar content.
Definition yaml_roundtrip.h:81
bool tag_before_anchor
Whether the tag precedes the anchor.
Definition yaml_roundtrip.h:93
std::size_t explicit_indent
The explicit block scalar indentation width.
Definition yaml_roundtrip.h:73
bool indent_before_chomping
Whether the indentation indicator precedes the chomping indicator.
Definition yaml_roundtrip.h:75
std::optional< JSON::Type > tag_type
Definition yaml_roundtrip.h:91
std::optional< CollectionStyle > collection
The collection presentation style.
Definition yaml_roundtrip.h:69
std::optional< std::size_t > sequence_size
Definition yaml_roundtrip.h:111
std::optional< std::string > block_content
The original block scalar content.
Definition yaml_roundtrip.h:77
std::vector< std::string > comments_before
The comments preceding the node.
Definition yaml_roundtrip.h:95
bool unindented_sequence
Definition yaml_roundtrip.h:107
std::optional< Chomping > chomping
The chomping behavior for a block scalar.
Definition yaml_roundtrip.h:71
std::optional< std::string > plain_content
The original plain scalar content.
Definition yaml_roundtrip.h:79
std::optional< std::string > tag
The tag attached to the node, as it was written.
Definition yaml_roundtrip.h:88
std::optional< ScalarStyle > scalar
The scalar presentation style.
Definition yaml_roundtrip.h:67
bool compact_flow
Whether the flow collection uses compact formatting.
Definition yaml_roundtrip.h:101