Skip to content

Commit

Permalink
Add deserializer types and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vicroms committed Dec 13, 2024
1 parent 4d8eb9b commit 80cc2ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
7 changes: 7 additions & 0 deletions include/vcpkg/base/message-data.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ DECLARE_MESSAGE(ADefaultFeature, (), "", "a default feature")
DECLARE_MESSAGE(ABoolean, (), "", "a boolean")
DECLARE_MESSAGE(ABuiltinRegistry, (), "", "a builtin registry")
DECLARE_MESSAGE(AConfigurationObject, (), "", "a configuration object")
DECLARE_MESSAGE(ACpuArchitecture, (), "", "a CPU architecture")
DECLARE_MESSAGE(ADependency, (), "", "a dependency")
DECLARE_MESSAGE(ADependencyFeature, (), "", "a feature of a dependency")
DECLARE_MESSAGE(ADemandObject,
(),
"'demands' are a concept in the schema of a JSON file the user can edit",
"a demand object")
DECLARE_MESSAGE(AString, (), "", "a string")
DECLARE_MESSAGE(ASha512, (), "", "a SHA512 hex string")
DECLARE_MESSAGE(ADateVersionString, (), "", "a date version string")
DECLARE_MESSAGE(AddArtifactOnlyOne, (msg::command_line), "", "'{command_line}' can only add one artifact at a time.")
DECLARE_MESSAGE(AddCommandFirstArg, (), "", "The first parameter to add must be 'artifact' or 'port'.")
Expand Down Expand Up @@ -1798,6 +1800,10 @@ DECLARE_MESSAGE(InvalidArchitecture,
(msg::value),
"{value} is what the user entered that we did not understand",
"invalid architecture: {value}")
DECLARE_MESSAGE(InvalidArchitectureValue,
(msg::value, msg::expected),
"{value} is what the user entered that we did not understand",
"Invalid architecture: {value}. Expected one of: {expected}.")
DECLARE_MESSAGE(InvalidArgument, (), "", "invalid argument")
DECLARE_MESSAGE(
InvalidArgumentRequiresAbsolutePath,
Expand Down Expand Up @@ -1920,6 +1926,7 @@ DECLARE_MESSAGE(InvalidOptionForRemove,
"'remove' is a command that should not be changed.",
"'remove' accepts either libraries or '--outdated'")
DECLARE_MESSAGE(InvalidPortVersonName, (msg::path), "", "Found invalid port version file name: `{path}`.")
DECLARE_MESSAGE(InvalidSha512, (msg::sha), "", "Invalid SHA512: {sha}")
DECLARE_MESSAGE(InvalidSharpInVersion, (), "", "invalid character '#' in version text")
DECLARE_MESSAGE(InvalidSharpInVersionDidYouMean,
(msg::value),
Expand Down
22 changes: 8 additions & 14 deletions src/vcpkg/base/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1571,11 +1571,7 @@ namespace vcpkg::Json

const FeatureNameDeserializer FeatureNameDeserializer::instance;

LocalizedString ArchitectureDeserializer::type_name() const
{
// TODO: Add type name
return msg::format(msgAFeatureName);
}
LocalizedString ArchitectureDeserializer::type_name() const { return msg::format(msgACpuArchitecture); }

Optional<std::string> ArchitectureDeserializer::visit_string(Json::Reader& r, StringView sv) const
{
Expand All @@ -1599,27 +1595,25 @@ namespace vcpkg::Json
return sv.to_string();
}

r.add_generic_error(type_name(), msg::format(msgInvalidArchitecture, msg::value = sv));
r.add_generic_error(type_name(),
msg::format(msgInvalidArchitectureValue,
msg::value = sv,
msg::expected = Strings::join(",", known_architectures)));
return nullopt;
}

const ArchitectureDeserializer ArchitectureDeserializer::instance;

LocalizedString Sha512Deserializer::type_name() const
{
// TODO: Add type name
return msg::format(msgAFeatureName);
}
LocalizedString Sha512Deserializer::type_name() const { return msg::format(msgASha512); }

Optional<std::string> Sha512Deserializer::visit_string(Json::Reader&, StringView sv) const
Optional<std::string> Sha512Deserializer::visit_string(Json::Reader& r, StringView sv) const
{
if (sv.size() == 128 && std::all_of(sv.begin(), sv.end(), ParserBase::is_hex_digit))
{
return sv.to_string();
}

// TODO: Add error
// r.add_generic_error(type_name(), msg::format(msgInvalidSha512Length, msg::value = sv));
r.add_generic_error(type_name(), msg::format(msgInvalidSha512, msg::sha = sv));
return nullopt;
}

Expand Down

0 comments on commit 80cc2ab

Please sign in to comment.