Status.h
Go to the documentation of this file.
1 /****
2  * ConfigDB/Status.h
3  *
4  * Copyright 2024 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the ConfigDB Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <WString.h>
23 #include <IFS/Error.h>
24 
25 #define CONFIGDB_ERROR_MAP(XX) \
26  XX(OK) \
27  XX(FormatError) \
28  XX(UpdateConflict) \
29  XX(FileError)
30 
31 #define CONFIGDB_FORMAT_ERROR_MAP(XX) \
32  XX(BadSyntax) \
33  XX(BadType) \
34  XX(BadSelector) \
35  XX(BadIndex) \
36  XX(BadProperty) \
37  XX(NotInSchema) \
38  XX(UpdateConflict)
39 
40 namespace ConfigDB
41 {
42 enum class Error {
43 #define XX(err) err,
45 #undef XX
46 };
47 
48 enum class FormatError {
49 #define XX(err) err,
51 #undef XX
52 };
53 
57 struct Status {
58  struct Code {
59  int fileError{};
61  };
62 
65 
70  static Status fileError(int errorCode)
71  {
72  return Status{Error::FileError, {.fileError = errorCode}};
73  }
74 
79  {
80  return Status{Error::FormatError, {.formatError = err}};
81  }
82 
87  {
89  code.formatError = err;
90  return *this;
91  }
92 
93  explicit operator bool() const
94  {
95  return error == Error::OK;
96  }
97 
98  String toString() const;
99 
100  size_t printTo(Print& p) const
101  {
102  return p.print(toString());
103  }
104 };
105 
106 } // namespace ConfigDB
107 
109 {
110  return status.toString();
111 }
112 
String toString(ConfigDB::Status status)
Definition: Status.h:108
#define CONFIGDB_ERROR_MAP(XX)
Definition: Status.h:25
#define CONFIGDB_FORMAT_ERROR_MAP(XX)
Definition: Status.h:31
Provides formatted output to stream.
Definition: Print.h:37
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:103
The String class.
Definition: WString.h:133
Definition: Array.h:26
FormatError
Definition: Status.h:48
Error
Definition: Status.h:42
Definition: Status.h:58
int fileError
Definition: Status.h:59
FormatError formatError
Definition: Status.h:60
Stores import/export stream status information.
Definition: Status.h:57
Status & operator=(FormatError err)
Set status to format error.
Definition: Status.h:86
Code code
Definition: Status.h:64
static Status fileError(int errorCode)
Create a file error status structure.
Definition: Status.h:70
Error error
Definition: Status.h:63
String toString() const
static Status formatError(FormatError err)
Create a format error status structure.
Definition: Status.h:78
size_t printTo(Print &p) const
Definition: Status.h:100