aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/template.cpp
blob: 2734387508dd0430ca2206263d200df5062a4970 (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "jinja2cpp/template.h"
#include "template_impl.h"

#include <fmt/format.h>

#include <fstream>
#include <sstream>

namespace jinja2
{
bool operator==(const Template& lhs, const Template& rhs)
{
    return lhs.IsEqual(rhs);
}

bool operator==(const TemplateW& lhs, const TemplateW& rhs)
{
    return lhs.IsEqual(rhs);
}

template<typename CharT>
auto GetImpl(std::shared_ptr<ITemplateImpl> impl)
{
    return static_cast<TemplateImpl<CharT>*>(impl.get());
}

Template::Template(TemplateEnv* env)
    : m_impl(new TemplateImpl<char>(env))
{

}

Template::~Template() = default;

Result<void> Template::Load(const char* tpl, std::string tplName)
{
    std::string t(tpl);
    auto result = GetImpl<char>(m_impl)->Load(std::move(t), std::move(tplName));
    return !result ? Result<void>() : nonstd::make_unexpected(std::move(result.get()));
}

Result<void> Template::Load(const std::string& str, std::string tplName)
{
    auto result = GetImpl<char>(m_impl)->Load(str, std::move(tplName));
    return !result ? Result<void>() : nonstd::make_unexpected(std::move(result.get()));
}

Result<void> Template::Load(std::istream& stream, std::string tplName)
{
    std::string t;

    while (stream.good() && !stream.eof())
    {
        char buff[0x10000];
        stream.read(buff, sizeof(buff));
        auto read = stream.gcount();
        if (read)
            t.append(buff, buff + read);
    }

    auto result = GetImpl<char>(m_impl)->Load(std::move(t), std::move(tplName));
    return !result ? Result<void>() : nonstd::make_unexpected(std::move(result.get()));
}

Result<void> Template::LoadFromFile(const std::string& fileName)
{
    std::ifstream file(fileName);

    if (!file.good())
        return Result<void>();

    return Load(file, fileName);
}

Result<void> Template::Render(std::ostream& os, const jinja2::ValuesMap& params)
{
    std::string buffer;
    auto result = GetImpl<char>(m_impl)->Render(buffer, params);

    if (!result)
        os.write(buffer.data(), buffer.size());

    return !result ? Result<void>() : nonstd::make_unexpected(std::move(result.get()));
}

Result<std::string> Template::RenderAsString(const jinja2::ValuesMap& params)
{
    std::string buffer;
    auto result = GetImpl<char>(m_impl)->Render(buffer, params);
    return !result ? Result<std::string>(std::move(buffer)) : Result<std::string>(nonstd::make_unexpected(std::move(result.get())));;
}

Result<GenericMap> Template::GetMetadata()
{
    return GetImpl<char>(m_impl)->GetMetadata();
}

Result<MetadataInfo<char>> Template::GetMetadataRaw()
{
    return GetImpl<char>(m_impl)->GetMetadataRaw();
}

bool Template::IsEqual(const Template& other) const
{
    return m_impl == other.m_impl;
}

TemplateW::TemplateW(TemplateEnv* env)
    : m_impl(new TemplateImpl<wchar_t>(env))
{

}

TemplateW::~TemplateW() = default;

ResultW<void> TemplateW::Load(const wchar_t* tpl, std::string tplName)
{
    std::wstring t(tpl);
    auto result = GetImpl<wchar_t>(m_impl)->Load(t, std::move(tplName));
    return !result ? ResultW<void>() : nonstd::make_unexpected(std::move(result.get()));
}

ResultW<void> TemplateW::Load(const std::wstring& str, std::string tplName)
{
    auto result = GetImpl<wchar_t>(m_impl)->Load(str, std::move(tplName));
    return !result ? ResultW<void>() : nonstd::make_unexpected(std::move(result.get()));
}

ResultW<void> TemplateW::Load(std::wistream& stream, std::string tplName)
{
    std::wstring t;

    while (stream.good() && !stream.eof())
    {
        wchar_t buff[0x10000];
        stream.read(buff, sizeof(buff));
        auto read = stream.gcount();
        if (read)
            t.append(buff, buff + read);
    }

    auto result = GetImpl<wchar_t>(m_impl)->Load(t, std::move(tplName));
    return !result ? ResultW<void>() : nonstd::make_unexpected(std::move(result.get()));
}

ResultW<void> TemplateW::LoadFromFile(const std::string& fileName)
{
    std::wifstream file(fileName);

    if (!file.good())
        return ResultW<void>();

    return Load(file, fileName);
}

ResultW<void> TemplateW::Render(std::wostream& os, const jinja2::ValuesMap& params)
{
    std::wstring buffer;
    auto result = GetImpl<wchar_t>(m_impl)->Render(buffer, params);
    if (!result)
        os.write(buffer.data(), buffer.size());
    return !result ? ResultW<void>() : ResultW<void>(nonstd::make_unexpected(std::move(result.get())));
}

ResultW<std::wstring> TemplateW::RenderAsString(const jinja2::ValuesMap& params)
{
    std::wstring buffer;
    auto result = GetImpl<wchar_t>(m_impl)->Render(buffer, params);

    return !result ? buffer : ResultW<std::wstring>(nonstd::make_unexpected(std::move(result.get())));
}

ResultW<GenericMap> TemplateW::GetMetadata()
{
    return GenericMap();
    // GetImpl<wchar_t>(m_impl)->GetMetadata();
}

ResultW<MetadataInfo<wchar_t>> TemplateW::GetMetadataRaw()
{
    return MetadataInfo<wchar_t>();
    // GetImpl<wchar_t>(m_impl)->GetMetadataRaw();
    ;
}
bool TemplateW::IsEqual(const TemplateW& other) const
{
    return m_impl == other.m_impl;
}

} // namespace jinja2