-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathArgumentValidation.h
More file actions
67 lines (48 loc) · 1.84 KB
/
ArgumentValidation.h
File metadata and controls
67 lines (48 loc) · 1.84 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
ArgumentValidation.h
Abstract:
Declaration of argument validation functions.
--*/
#pragma once
#include "Exceptions.h"
#include "ContainerModel.h"
#include <string>
#include <vector>
#include <charconv>
#include <format>
#include <wslc.h>
#include <string.hpp>
using namespace wsl::windows::wslc::models;
namespace wsl::windows::wslc::validation {
template <typename T>
void ValidateIntegerFromString(const std::vector<std::wstring>& values, const std::wstring& argName)
{
for (const auto& value : values)
{
std::ignore = GetIntegerFromString<T>(value, argName);
}
}
template <typename T>
T GetIntegerFromString(const std::wstring& value, const std::wstring& argName = {})
{
std::string narrowValue = wsl::windows::common::string::WideToMultiByte(value);
T convertedValue{};
const char* begin = narrowValue.c_str();
const char* end = begin + narrowValue.size();
auto result = std::from_chars(begin, end, convertedValue);
// Reject conversion errors and partial parses (e.g. "1.5", "9abc")
if (result.ec != std::errc() || result.ptr != end)
{
throw ArgumentException(std::format(L"Invalid {} argument value: {}", argName, value));
}
return convertedValue;
}
void ValidateWSLCSignalFromString(const std::vector<std::wstring>& values, const std::wstring& argName);
WSLCSignal GetWSLCSignalFromString(const std::wstring& input, const std::wstring& argName = {});
void ValidateFormatTypeFromString(const std::vector<std::wstring>& values, const std::wstring& argName);
FormatType GetFormatTypeFromString(const std::wstring& input, const std::wstring& argName = {});
void ValidateVolumeMount(const std::vector<std::wstring>& values);
void ValidateCidFile(const std::wstring& cidFile);
} // namespace wsl::windows::wslc::validation