Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
FileSystemIteratorErrorFormatter.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Common/ResultErrorFormatter.h"
5#include "FileSystemIteratorError.h"
6
7namespace SC
8{
11
12namespace detail
13{
14inline ResultErrorFormat formatFileSystemIteratorErrorWithContext(FileSystemIteratorError error,
16 uint32_t nativeError, uint16_t depth,
17 Span<char> output)
18{
19 ResultErrorFormatter formatter(output);
20 switch (error)
21 {
22 case FileSystemIteratorError::NotInitialized: formatter.append("File system iterator is not initialized"); break;
23 case FileSystemIteratorError::InvalidRecursionState:
24 formatter.append("Directory recursion is not valid in the current state");
25 break;
26 case FileSystemIteratorError::UnsupportedPathEncoding: formatter.append("Path encoding is not supported"); break;
27 case FileSystemIteratorError::PathTooLong: formatter.append("Directory path is too long"); break;
28 case FileSystemIteratorError::RecursionLimitExceeded:
29 formatter.append("Directory recursion storage is exhausted");
30 break;
31 case FileSystemIteratorError::PathResolutionFailed: formatter.append("Failed to resolve directory path"); break;
32 case FileSystemIteratorError::OpenDirectoryFailed: formatter.append("Failed to open directory"); break;
33 case FileSystemIteratorError::DirectoryEnumerationFailed: formatter.append("Failed to enumerate directory"); break;
34 default: return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
35 }
36
37 if (detail != FileSystemIteratorErrorDetail::None)
38 {
39 formatter.append(" (detail: ");
40 switch (detail)
41 {
42 case FileSystemIteratorErrorDetail::WindowsResolveLogicalPath:
43 formatter.append("Windows resolve logical path");
44 break;
45 case FileSystemIteratorErrorDetail::BuildPath: formatter.append("build traversal path"); break;
46 case FileSystemIteratorErrorDetail::PosixOpen: formatter.append("POSIX open directory"); break;
47 case FileSystemIteratorErrorDetail::PosixFdOpenDir:
48 formatter.append("POSIX initialize directory enumeration");
49 break;
50 case FileSystemIteratorErrorDetail::WindowsFindFirstFile:
51 formatter.append("Windows begin directory enumeration");
52 break;
53 case FileSystemIteratorErrorDetail::PushRecursionState: formatter.append("push recursion state"); break;
54 case FileSystemIteratorErrorDetail::PosixReadDir: formatter.append("POSIX read directory entry"); break;
55 case FileSystemIteratorErrorDetail::WindowsFindNextFile:
56 formatter.append("Windows read directory entry");
57 break;
58 default: return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
59 }
60 if (nativeError != 0)
61 {
62 formatter.append(", native error: ");
63 formatter.append(static_cast<uint64_t>(nativeError));
64 }
65 formatter.append(", depth: ");
66 formatter.append(static_cast<uint64_t>(depth));
67 formatter.append(")");
68 }
69 else if (nativeError != 0)
70 {
71 formatter.append(" (native error: ");
72 formatter.append(static_cast<uint64_t>(nativeError));
73 formatter.append(", depth: ");
74 formatter.append(static_cast<uint64_t>(depth));
75 formatter.append(")");
76 }
77 else if (error == FileSystemIteratorError::RecursionLimitExceeded)
78 {
79 formatter.append(" (depth: ");
80 formatter.append(static_cast<uint64_t>(depth));
81 formatter.append(")");
82 }
83 return formatter.finish();
84}
85} // namespace detail
86
88inline ResultErrorFormat formatFileSystemIteratorError(FileSystemIteratorError error, Span<char> output)
89{
90 return detail::formatFileSystemIteratorErrorWithContext(error, FileSystemIteratorErrorDetail::None, 0, 0, output);
91}
92
94inline ResultErrorFormat formatFileSystemIteratorError(Result result, Span<char> output)
95{
96 if (result)
97 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
98 if (result.category() != FileSystemIteratorResultCategory)
99 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
100 return formatFileSystemIteratorError(static_cast<FileSystemIteratorError>(result.errorValue()), output);
101}
102
104inline ResultErrorFormat formatFileSystemIteratorError(ResultFileSystemIterator result, Span<char> output)
105{
106 if (result)
107 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
108 if (result.result.category() != FileSystemIteratorResultCategory)
109 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
110 return detail::formatFileSystemIteratorErrorWithContext(
111 static_cast<FileSystemIteratorError>(result.result.errorValue()), result.detail, result.nativeError,
112 result.depth, output);
113}
114
116} // namespace SC
FileSystemIteratorError
Stable error codes returned by the FileSystemIterator library.
Definition FileSystemIteratorError.h:13
FileSystemIteratorErrorDetail
Stable detail identifying the backend stage of a FileSystemIterator failure.
Definition FileSystemIteratorError.h:26
ResultErrorFormat formatFileSystemIteratorError(FileSystemIteratorError error, Span< char > output)
Formats a canonical English FileSystemIterator diagnostic into caller-owned storage.
Definition FileSystemIteratorErrorFormatter.h:88
FileSystemIterator result retaining optional native, backend-stage, and directory-depth details.
Definition FileSystemIteratorError.h:48