Skip to content

Commit

Permalink
Add NanaBox::FromGuestType and NanaBox::ToGuestType.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed Nov 19, 2024
1 parent 8fadd5c commit 882f6cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
21 changes: 5 additions & 16 deletions NanaBox/ConfigurationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include <Mile.Helpers.Base.h>

#include <NanaBox.Configuration.Parser.h>

namespace NanaBox
{
namespace ResolutionType
Expand All @@ -30,12 +32,6 @@ namespace NanaBox

namespace NanaBox
{
NLOHMANN_JSON_SERIALIZE_ENUM(NanaBox::GuestType, {
{ NanaBox::GuestType::Unknown, "Unknown" },
{ NanaBox::GuestType::Windows, "Windows" },
{ NanaBox::GuestType::Linux, "Linux" }
})

NLOHMANN_JSON_SERIALIZE_ENUM(NanaBox::UefiConsoleMode, {
{ NanaBox::UefiConsoleMode::Disabled, "Disabled" },
{ NanaBox::UefiConsoleMode::Default, "Default" },
Expand Down Expand Up @@ -1320,15 +1316,8 @@ NanaBox::VirtualMachineConfiguration NanaBox::DeserializeConfiguration(
throw std::exception("Invalid Version");
}

try
{
Result.GuestType =
RootJson.at("GuestType").get<NanaBox::GuestType>();
}
catch (...)
{

}
Result.GuestType = NanaBox::ToGuestType(
Mile::Json::GetSubKey(RootJson, "GuestType"));

Result.Name = Mile::Json::ToString(
Mile::Json::GetSubKey(RootJson, "Name"),
Expand Down Expand Up @@ -1513,7 +1502,7 @@ std::string NanaBox::SerializeConfiguration(
nlohmann::json RootJson;
RootJson["Type"] = "VirtualMachine";
RootJson["Version"] = Configuration.Version;
RootJson["GuestType"] = Configuration.GuestType;
RootJson["GuestType"] = NanaBox::FromGuestType(Configuration.GuestType);
RootJson["Name"] = Configuration.Name;
RootJson["ProcessorCount"] = Configuration.ProcessorCount;
RootJson["MemorySize"] = Configuration.MemorySize;
Expand Down
32 changes: 32 additions & 0 deletions NanaBox/NanaBox.Configuration.Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,35 @@
*/

#include "NanaBox.Configuration.Parser.h"

nlohmann::json NanaBox::FromGuestType(
NanaBox::GuestType const& Value)
{
if (NanaBox::GuestType::Windows == Value)
{
return "Windows";
}
else if (NanaBox::GuestType::Linux == Value)
{
return "Linux";
}

return "Unknown";
}

NanaBox::GuestType NanaBox::ToGuestType(
nlohmann::json const& Value)
{
std::string RawValue = Mile::Json::ToString(Value);

if (0 == std::strcmp(RawValue.c_str(), "Windows"))
{
return NanaBox::GuestType::Windows;
}
else if (0 == std::strcmp(RawValue.c_str(), "Linux"))
{
return NanaBox::GuestType::Linux;
}

return NanaBox::GuestType::Unknown;
}
6 changes: 6 additions & 0 deletions NanaBox/NanaBox.Configuration.Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@

#include <NanaBox.Configuration.Specification.h>

#include <Mile.Json.h>

namespace NanaBox
{
nlohmann::json FromGuestType(
GuestType const& Value);

GuestType ToGuestType(
nlohmann::json const& Value);
}

#endif // !NANABOX_CONFIGURATION_PARSER

0 comments on commit 882f6cc

Please sign in to comment.