aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/result.cpp
blob: c00025f11dd48ea31f8d263ca95144a676c0c3b5 (plain)
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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "result.h"

#include "utilstr.h"

namespace Utils {

const Result<> ResultOk;

static QString messageForCode(ResultSpecialErrorCode code)
{
    switch (code) {
    case ResultAssert:
        return Tr::tr("Internal error: %1.");
    case ResultUnimplemented:
        return Tr::tr("Not implemented error: %1.");
    default:
        return Tr::tr("Unknown error: %1.");
    }
}

ResultError::ResultError(const QString &errorMessage)
    : m_error(errorMessage)
{}

ResultError::ResultError(ResultSpecialErrorCode code, const QString &errorMessage)
    : m_error(messageForCode(code).arg(
          errorMessage.isEmpty() ? Tr::tr("Unknown reason.") : errorMessage))
{}

Result<> makeResult(bool ok, const QString &errorMessage)
{
    if (ok)
        return ResultOk;
    return ResultError(errorMessage);
}

} // Utils