Echo Writes Code

transcoding.hpp

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
#ifndef CRUCIBLE_WINDOWS_TRANSCODING_HPP
#define CRUCIBLE_WINDOWS_TRANSCODING_HPP

#include "crucible/core/constant_error.inl"
#include "crucible/core/error_chain.inl"
#include "crucible/core/result.inl"
#include "crucible/windows/windows_error.inl"

#include <string>

namespace crucible::windows::transcoding {
  struct MultiByteToWideCharFailed final : windows_error::WindowsError<MultiByteToWideCharFailed> {
    static constexpr char const function[] = "MultiByteToWideChar";
  };

  struct WideCharToMultiByteFailed final : windows_error::WindowsError<WideCharToMultiByteFailed> {
    static constexpr char const function[] = "WideCharToMultiByte";
  };

  struct OutputTruncated final : core::constant_error::ConstantError<OutputTruncated> {
    static constexpr char const message[] = "Output was truncated";
  };

  struct TranscodingError final : core::error_chain::ErrorChain<TranscodingError, MultiByteToWideCharFailed, WideCharToMultiByteFailed, OutputTruncated> {
    using ErrorChain::ErrorChain;

    static constexpr char const message[] = "Transcoding error";
  };

  template<typename T>
  using TranscodingResult = core::result::Result<TranscodingError, T>;

  [[nodiscard]]
  auto transcode_utf8_to_utf16(std::string const &utf8) -> TranscodingResult<std::wstring>;

  [[nodiscard]]
  auto transcode_utf16_to_utf8(std::wstring const &utf16) -> TranscodingResult<std::string>;
}

#endif // CRUCIBLE_WINDOWS_TRANSCODING_HPP