ProjectConfig.cpp 1.0 KB

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