ProjectConfig.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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/Stream.h>
  9. namespace HackStudio {
  10. ProjectConfig::ProjectConfig(JsonObject config)
  11. : m_config(move(config))
  12. {
  13. }
  14. ErrorOr<NonnullOwnPtr<ProjectConfig>> ProjectConfig::try_load_project_config(DeprecatedString path)
  15. {
  16. auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
  17. auto file_contents = TRY(file->read_until_eof());
  18. auto json = TRY(JsonValue::from_string(file_contents));
  19. if (!json.is_object())
  20. return Error::from_string_literal("The topmost JSON element is not an object");
  21. return adopt_own(*new ProjectConfig(json.as_object()));
  22. }
  23. NonnullOwnPtr<ProjectConfig> ProjectConfig::create_empty()
  24. {
  25. JsonObject empty {};
  26. return adopt_own(*new ProjectConfig(empty));
  27. }
  28. Optional<DeprecatedString> ProjectConfig::read_key(DeprecatedString key_name) const
  29. {
  30. return m_config.get_deprecated_string(key_name);
  31. }
  32. }