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(disable : 4251 4275)
24#endif
25
29class SOURCEMETA_CORE_YAML_EXPORT YAMLRoundTrip {
30public:
32 enum class ScalarStyle : std::uint8_t {
36 SingleQuoted,
38 DoubleQuoted,
40 Literal,
42 Folded
43 };
44
46 enum class CollectionStyle : std::uint8_t {
48 Block,
50 Flow
51 };
52
54 enum class Chomping : std::uint8_t {
56 Clip,
58 Strip,
60 Keep
61 };
62
64 struct NodeStyle {
66 std::optional<ScalarStyle> scalar;
68 std::optional<CollectionStyle> collection;
70 std::optional<Chomping> chomping;
72 std::size_t explicit_indent{0};
76 std::optional<std::string> block_content;
78 std::optional<std::string> plain_content;
80 std::optional<std::string> quoted_content;
83 std::optional<JSON> content_value;
85 std::optional<std::string> anchor;
87 std::vector<std::string> comments_before;
89 std::optional<std::string> comment_inline;
91 std::optional<std::string> comment_on_indicator;
93 bool compact_flow{false};
94 };
95
97 std::unordered_map<Pointer, NodeStyle, Pointer::Hasher> styles;
99 std::unordered_map<Pointer, std::string, Pointer::Hasher> aliases;
101 std::unordered_map<Pointer, ScalarStyle, Pointer::Hasher> key_styles;
103 std::unordered_map<Pointer, std::string, Pointer::Hasher> key_quoted_contents;
109 std::optional<std::string> document_start_comment;
111 std::optional<std::string> document_end_comment;
113 std::vector<std::string> leading_comments;
115 std::vector<std::string> post_start_comments;
117 std::vector<std::string> pre_end_comments;
119 std::vector<std::string> trailing_comments;
121 std::size_t indent_width{2};
122};
123
124#if defined(_MSC_VER)
125#pragma warning(default : 4251 4275)
126#endif
127
128} // namespace sourcemeta::core
129
130#endif
@ Plain
Definition oauth_pkce.h:25
std::unordered_map< Pointer, std::string, Pointer::Hasher > key_quoted_contents
The original quoted content for each mapping key.
Definition yaml_roundtrip.h:103
CollectionStyle
The presentation style of a collection.
Definition yaml_roundtrip.h:46
std::vector< std::string > pre_end_comments
The comments preceding the document end marker.
Definition yaml_roundtrip.h:117
std::vector< std::string > post_start_comments
The comments following the document start marker.
Definition yaml_roundtrip.h:115
std::unordered_map< Pointer, NodeStyle, Pointer::Hasher > styles
The recorded formatting for each node by pointer.
Definition yaml_roundtrip.h:97
std::unordered_map< Pointer, std::string, Pointer::Hasher > aliases
The alias name for each node referencing an anchor.
Definition yaml_roundtrip.h:99
std::optional< std::string > document_start_comment
The comment on the document start marker.
Definition yaml_roundtrip.h:109
Chomping
The chomping behavior applied to a block scalar.
Definition yaml_roundtrip.h:54
std::size_t indent_width
The indentation width used when emitting the document.
Definition yaml_roundtrip.h:121
std::vector< std::string > trailing_comments
The comments following the document.
Definition yaml_roundtrip.h:119
bool explicit_document_end
Whether the document ends with an explicit end marker.
Definition yaml_roundtrip.h:107
std::unordered_map< Pointer, ScalarStyle, Pointer::Hasher > key_styles
The scalar style for each mapping key by pointer.
Definition yaml_roundtrip.h:101
std::vector< std::string > leading_comments
The comments preceding the document.
Definition yaml_roundtrip.h:113
bool explicit_document_start
Whether the document begins with an explicit start marker.
Definition yaml_roundtrip.h:105
std::optional< std::string > document_end_comment
The comment on the document end marker.
Definition yaml_roundtrip.h:111
ScalarStyle
The presentation style of a scalar value.
Definition yaml_roundtrip.h:32
Definition yaml_roundtrip.h:29
Holds the formatting details recorded for a single node.
Definition yaml_roundtrip.h:64
std::optional< std::string > comment_inline
The comment on the same line as the node.
Definition yaml_roundtrip.h:89
std::optional< std::string > anchor
The anchor name attached to the node.
Definition yaml_roundtrip.h:85
std::optional< std::string > comment_on_indicator
The comment attached to a block scalar indicator.
Definition yaml_roundtrip.h:91
std::optional< JSON > content_value
Definition yaml_roundtrip.h:83
std::optional< std::string > quoted_content
The original quoted scalar content.
Definition yaml_roundtrip.h:80
std::size_t explicit_indent
The explicit block scalar indentation width.
Definition yaml_roundtrip.h:72
bool indent_before_chomping
Whether the indentation indicator precedes the chomping indicator.
Definition yaml_roundtrip.h:74
std::optional< CollectionStyle > collection
The collection presentation style.
Definition yaml_roundtrip.h:68
std::optional< std::string > block_content
The original block scalar content.
Definition yaml_roundtrip.h:76
std::vector< std::string > comments_before
The comments preceding the node.
Definition yaml_roundtrip.h:87
std::optional< Chomping > chomping
The chomping behavior for a block scalar.
Definition yaml_roundtrip.h:70
std::optional< std::string > plain_content
The original plain scalar content.
Definition yaml_roundtrip.h:78
std::optional< ScalarStyle > scalar
The scalar presentation style.
Definition yaml_roundtrip.h:66
bool compact_flow
Whether the flow collection uses compact formatting.
Definition yaml_roundtrip.h:93