userver: userver/formats/parse/boost_uuid.hpp Source File
Loading...
Searching...
No Matches
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 std::optional<std::string> str;
30 try {
31 str = value.template As<std::string>();
32 return utils::BoostUuidFromString(*str);
33 } catch (const std::exception& e) {
34 if (!!str) {
35 throw typename Value::ParseException("'" + *str + "' cannot be parsed to `boost::uuids::uuid`");
36 } else {
37 throw typename Value::ParseException("Only strings can be parsed as boost uuid");
38 }
39 }
40}
41
42} // namespace formats::parse
43
44USERVER_NAMESPACE_END