forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYamlWrapper.h
More file actions
168 lines (124 loc) · 4.61 KB
/
Copy pathYamlWrapper.h
File metadata and controls
168 lines (124 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include <yaml.h>
#include "winget/Yaml.h"
#include "AppInstallerLanguageUtilities.h"
#include "AppInstallerSHA256.h"
#include <iostream>
#include <string_view>
#include <vector>
namespace AppInstaller::YAML::Wrapper
{
// A libyaml yaml_document_t.
// A parsed document, created by the Parser.
struct Document
{
// Initializes the document.
Document(bool init = false);
Document(const Document&) = delete;
Document& operator=(const Document&) = delete;
Document(Document&&) noexcept = default;
Document& operator=(Document&&) noexcept = delete;
~Document();
yaml_document_t* operator&() { return &m_document; }
// Indicates that the document should not be deleted, as
// it has been handed off to the emitter.
void Detach() { m_token = false; }
// Determines whether the document has a root node.
bool HasRoot();
// Gets the root node of the document, if it has one.
Node GetRoot();
// Adds a scalar node to the document.
int AddScalar(std::string_view value, ScalarStyle style = ScalarStyle::Any);
// Adds a sequence node to the document.
int AddSequence();
// Adds a mapping node to the document.
int AddMapping();
// Appends a node to the end of the sequence.
void AppendSequenceItem(int sequence, int item);
// Adds a pair to the mapping.
void AppendMappingPair(int mapping, int key, int value);
private:
// Gets the node referenced by the index.
yaml_node_t* GetNode(yaml_node_item_t index);
DestructionToken m_token;
yaml_document_t m_document;
};
// A libyaml yaml_parser_t.
// The core parser construct for reading bytes directly.
struct Parser
{
Parser(std::string_view input);
Parser(std::istream& input, Utility::SHA256::HashBuffer* hashOut = nullptr);
Parser(const Parser&) = delete;
Parser& operator=(const Parser&) = delete;
Parser(Parser&&) noexcept = default;
Parser& operator=(Parser&&) noexcept = delete;
~Parser();
yaml_parser_t* operator&() { return &m_parser; }
// Loads the next document from the input, if one exists.
Document Load();
private:
// Determines the type of encoding in use, transforming the input as necessary.
void PrepareInput();
DestructionToken m_token;
yaml_parser_t m_parser;
std::string m_input;
};
// A libyaml yaml_event_t.
// The generic event type for.
struct Event
{
Event(const Event&) = delete;
Event& operator=(const Event&) = delete;
Event(Event&&) noexcept = default;
Event& operator=(Event&&) noexcept = delete;
~Event();
yaml_event_t* operator&() { return &m_event; }
// Indicates that the event should not be deleted, as
// it has been handed off to the emitter.
void Detach() { m_token = false; }
// Event creation functions.
static Event StreamStart();
static Event StreamEnd();
static Event DocumentStart();
static Event DocumentEnd();
static Event SequenceStart();
static Event SequenceEnd();
static Event MappingStart();
static Event MappingEnd();
private:
Event() = default;
DestructionToken m_token;
yaml_event_t m_event = {};
};
// A libyaml yaml_emitter_t.
// Allows YAML to be written out.
struct Emitter
{
Emitter(std::ostream& output);
Emitter(const Emitter&) = delete;
Emitter& operator=(const Emitter&) = delete;
Emitter(Emitter&&) noexcept = default;
Emitter& operator=(Emitter&&) noexcept = delete;
~Emitter();
yaml_emitter_t* operator&() { return &m_emitter; }
// Emits and event.
void Emit(Event& event);
void Emit(Event&& event);
// Dumps a document to the emitter.
void Dump(Document& document);
// Flushes the emitter.
void Flush();
private:
static int StreamWriteHandler(
void* data,
unsigned char* buffer,
size_t size);
void ThrowError();
DestructionToken m_token;
yaml_emitter_t m_emitter;
std::ostream* m_outputStream = nullptr;
};
}