1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 11:24:58 +00:00
serenity/Userland/DevTools/HackStudio/ProjectConfig.cpp
Nico Weber 9cfd7a299c Userland: Use allocation-failure safe functions where it's easy
I went through all callers of adopt_own() and replaced them with
try_make<>() if possible or adopt_nonnull_own_or_enomem() else
in cases where it was easy (i.e. in functions already returning
ErrorOr).

No intended behavior change.
2023-02-12 22:54:28 +01:00

41 lines
1 KiB
C++

/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ProjectConfig.h"
#include <AK/NonnullOwnPtr.h>
#include <LibCore/Stream.h>
namespace HackStudio {
ProjectConfig::ProjectConfig(JsonObject config)
: m_config(move(config))
{
}
ErrorOr<NonnullOwnPtr<ProjectConfig>> ProjectConfig::try_load_project_config(DeprecatedString path)
{
auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
auto file_contents = TRY(file->read_until_eof());
auto json = TRY(JsonValue::from_string(file_contents));
if (!json.is_object())
return Error::from_string_literal("The topmost JSON element is not an object");
return try_make<ProjectConfig>(json.as_object());
}
NonnullOwnPtr<ProjectConfig> ProjectConfig::create_empty()
{
JsonObject empty {};
return adopt_own(*new ProjectConfig(empty));
}
Optional<DeprecatedString> ProjectConfig::read_key(DeprecatedString key_name) const
{
return m_config.get_deprecated_string(key_name);
}
}