userver: userver/formats/parse/boost_uuid.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
boost_uuid.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/parse/boost_uuid.hpp
4/// @brief boost::uuids::uuid parser for any format
5/// @ingroup userver_universal userver_formats_parse
6
7#include <optional>
8#include <string>
9
10#include <boost/uuid/uuid.hpp>
11
12#include <userver/formats/common/meta.hpp>
13#include <userver/formats/parse/to.hpp>
14#include <userver/utils/boost_uuid4.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace formats::parse {
19
20/**
21 * Valid uuid strings:
22 * 0123456789abcdef0123456789abcdef
23 * 01234567-89ab-cdef-0123-456789abcdef
24 * {01234567-89ab-cdef-0123-456789abcdef}
25 * {0123456789abcdef0123456789abcdef}
26 */
27template <typename Value>
29 const Value& value, To<boost::uuids::uuid>) {
30 std::optional<std::string> str;
31 try {
32 str = value.template As<std::string>();
33 return utils::BoostUuidFromString(*str);
34 } catch (const std::exception& e) {
35 if (!!str) {
36 throw typename Value::ParseException(
37 "'" + *str + "' cannot be parsed to `boost::uuids::uuid`");
38 } else {
39 throw typename Value::ParseException(
40 "Only strings can be parsed as boost uuid");
41 }
42 }
43}
44
45} // namespace formats::parse
46
47USERVER_NAMESPACE_END