Branch data Line data Source code
1 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
2 : : // SPDX-FileCopyrightText: 2018-2020 Canonical, Ltd
3 : :
4 : : #pragma once
5 : :
6 : : #include <config.h>
7 : :
8 : : #include <format>
9 : : #include <string>
10 : : #include <type_traits>
11 : : #include <utility> // for forward
12 : :
13 : : #include <glib.h>
14 : :
15 : : #include <mozilla/Result.h>
16 : : #include <mozilla/ResultVariant.h> // IWYU pragma: keep (see Result::Impl)
17 : :
18 : : #include "gjs/auto.h"
19 : :
20 : : namespace mozilla::detail {
21 : : template <typename V, typename E, PackingStrategy Strategy>
22 : : class ResultImplementation;
23 : : }
24 : :
25 : : // Auto pointer type for GError, as well as a Result type that can be used as a
26 : : // type-safe return type for fallible GNOME-platform operations.
27 : : // To indicate success, return Ok{}, and to indicate failure, return Err(error)
28 : : // or Err(error.release()) if using AutoError.
29 : : // When calling a function that returns GErrorResult inside another function
30 : : // that returns GErrorResult, you can use the MOZ_TRY() macro as if it were the
31 : : // Rust ? operator, to bail out on error even if the GErrorResult's success
32 : : // types are different.
33 : :
34 : : // COMPAT: We use Mozilla's Result type because std::expected does not appear in
35 : : // the standard library until C++23.
36 : :
37 : : namespace Gjs {
38 : :
39 : 14897 : struct AutoError : AutoPointer<GError, GError, g_error_free> {
40 : : using BaseType::BaseType;
41 : : using BaseType::operator=;
42 : :
43 : : constexpr BaseType::ConstPtr* operator&() // NOLINT(runtime/operator)
44 : : const {
45 : : return out();
46 : : }
47 : 130317 : constexpr BaseType::Ptr* operator&() { // NOLINT(runtime/operator)
48 : 130317 : return out();
49 : : }
50 : :
51 : : template <typename... Args>
52 : 59 : AutoError(GQuark domain, int code, std::format_string<Args...> fmt,
53 : 59 : Args&&... args) {
54 : 126 : std::string message = std::format(fmt, std::forward<Args>(args)...);
55 : 59 : g_set_error_literal(out(), domain, code, message.c_str());
56 : 59 : }
57 : : };
58 : :
59 : : template <>
60 : : struct SmartPointer<GError> : AutoError {
61 : : using AutoError::AutoError;
62 : : using AutoError::operator=;
63 : : using AutoError::operator&;
64 : : };
65 : :
66 : : template <typename T = mozilla::Ok>
67 : : using GErrorResult = mozilla::Result<T, AutoError>;
68 : :
69 : : // Used below in the formatter
70 : : namespace detail {
71 : : template <typename T>
72 : : concept HasPointerGet =
73 : : requires(T p) { requires std::is_pointer_v<decltype(p.get())>; };
74 : : } // namespace detail
75 : :
76 : : } // namespace Gjs
77 : :
78 : : namespace mozilla::detail {
79 : : // Custom packing for GErrorResult<>
80 : : template <>
81 : : class SelectResultImpl<Ok, Gjs::AutoError> {
82 : : public:
83 : : class Type {
84 : : Gjs::AutoError m_value;
85 : :
86 : : public:
87 : 17697 : explicit constexpr Type(Ok) : m_value() {}
88 : 53 : explicit constexpr Type(GError* error) : m_value(error) {}
89 : 1849 : constexpr Type(Type&& other) : m_value(other.m_value.release()) {}
90 : 104 : Type& operator=(Type&& other) {
91 : 104 : m_value = other.m_value.release();
92 : 104 : return *this;
93 : : }
94 : 37435 : [[nodiscard]] constexpr bool isOk() const { return !m_value; }
95 : : [[nodiscard]] static constexpr Ok inspect() { return {}; }
96 : 1681 : [[nodiscard]] static constexpr Ok unwrap() { return {}; }
97 : : [[nodiscard]]
98 : 8 : constexpr const GError* inspectErr() const {
99 : 8 : return m_value.get();
100 : : }
101 : 45 : [[nodiscard]] Gjs::AutoError unwrapErr() { return m_value.release(); }
102 : : };
103 : : };
104 : :
105 : : // Packing for any other pointer. Unlike in SpiderMonkey, GLib-allocated
106 : : // pointers may not be aligned, so their bottom bit cannot be used for a flag.
107 : : template <typename T>
108 : : class SelectResultImpl<T*, Gjs::AutoError> {
109 : : public:
110 : : using Type = ResultImplementation<T*, Gjs::AutoError,
111 : : PackingStrategy::PackedVariant>;
112 : : };
113 : :
114 : : } // namespace mozilla::detail
115 : :
116 : : // Formatters
117 : :
118 : : template <>
119 : : struct std::formatter<Gjs::AutoError> : std::formatter<const char*> {
120 : 12715 : auto format(const Gjs::AutoError& err, std::format_context& cx) const {
121 [ - + ]: 12715 : if (!err)
122 : 0 : return formatter<const char*>::format("(null error)", cx);
123 : 12715 : return formatter<const char*>::format(err->message, cx);
124 : : }
125 : : };
126 : :
127 : : template <typename T>
128 : : struct std::formatter<Gjs::GErrorResult<T>> : std::formatter<std::string> {
129 : 7 : auto format(const Gjs::GErrorResult<T>& result,
130 : : std::format_context& cx) const {
131 [ + + ]: 7 : if (result.isOk()) {
132 : : if constexpr (std::is_same_v<T, mozilla::Ok>) {
133 : 3 : return std::formatter<std::string>::format("Ok", cx);
134 : : } else if constexpr (std::is_pointer_v<T>) {
135 : 1 : return std::formatter<std::string>::format(
136 : 2 : std::format("Ok({})", static_cast<void*>(result.inspect())),
137 : 1 : cx);
138 : : } else if constexpr (Gjs::detail::HasPointerGet<T>) {
139 : 0 : return std::formatter<std::string>::format(
140 : 0 : std::format("Ok({})",
141 : 0 : static_cast<void*>(result.inspect().get())),
142 : 0 : cx);
143 : : } else {
144 : 1 : return std::formatter<std::string>::format(
145 : 2 : std::format("Ok({})", result.inspect()), cx);
146 : : }
147 : : }
148 : 16 : return std::formatter<std::string>::format(result.inspectErr()->message,
149 : 4 : cx);
150 : : }
151 : : };
|