Serenity Operating System
at master 41 lines 1.0 kB view raw
1/* 2 * Copyright (c) 2022, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "ProjectConfig.h" 8#include <AK/NonnullOwnPtr.h> 9#include <LibCore/File.h> 10 11namespace HackStudio { 12 13ProjectConfig::ProjectConfig(JsonObject config) 14 : m_config(move(config)) 15{ 16} 17 18ErrorOr<NonnullOwnPtr<ProjectConfig>> ProjectConfig::try_load_project_config(DeprecatedString path) 19{ 20 auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read)); 21 auto file_contents = TRY(file->read_until_eof()); 22 23 auto json = TRY(JsonValue::from_string(file_contents)); 24 if (!json.is_object()) 25 return Error::from_string_literal("The topmost JSON element is not an object"); 26 27 return try_make<ProjectConfig>(json.as_object()); 28} 29 30NonnullOwnPtr<ProjectConfig> ProjectConfig::create_empty() 31{ 32 JsonObject empty {}; 33 return adopt_own(*new ProjectConfig(empty)); 34} 35 36Optional<DeprecatedString> ProjectConfig::read_key(DeprecatedString key_name) const 37{ 38 return m_config.get_deprecated_string(key_name); 39} 40 41}