1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:57:43 +00:00

PixelPaint: Use ErrorOr<T> for Image and Layer creation helpers

This commit is contained in:
Andreas Kling 2021-11-07 11:22:35 +01:00
parent 9268ed9605
commit 801d46d02c
7 changed files with 76 additions and 113 deletions

View file

@ -16,12 +16,12 @@
namespace PixelPaint {
Result<void, String> ProjectLoader::try_load_from_fd_and_close(int fd, StringView path)
ErrorOr<void> ProjectLoader::try_load_from_fd_and_close(int fd, StringView path)
{
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::No);
if (file->has_error())
return String { file->error_string() };
return Error::from_errno(file->error());
auto contents = file->read_all();
@ -29,18 +29,11 @@ Result<void, String> ProjectLoader::try_load_from_fd_and_close(int fd, StringVie
if (!json_or_error.has_value()) {
m_is_raw_image = true;
auto file_or_error = MappedFile::map_from_fd_and_close(fd, path);
if (file_or_error.is_error())
return String::formatted("Unable to mmap file {}", file_or_error.error());
auto mapped_file = TRY(MappedFile::map_from_fd_and_close(fd, path));
auto& mapped_file = *file_or_error.value();
// FIXME: Find a way to avoid the memory copy here.
auto bitmap = Image::try_decode_bitmap(mapped_file.bytes());
if (!bitmap)
return String { "Unable to decode image"sv };
auto image = Image::try_create_from_bitmap(bitmap.release_nonnull());
if (!image)
return String { "Unable to allocate Image"sv };
auto bitmap = TRY(Image::try_decode_bitmap(mapped_file->bytes()));
auto image = TRY(Image::try_create_from_bitmap(move(bitmap)));
image->set_path(path);
m_image = image;
@ -49,12 +42,8 @@ Result<void, String> ProjectLoader::try_load_from_fd_and_close(int fd, StringVie
close(fd);
auto& json = json_or_error.value().as_object();
auto image_or_error = Image::try_create_from_pixel_paint_json(json);
auto image = TRY(Image::try_create_from_pixel_paint_json(json));
if (image_or_error.is_error())
return image_or_error.release_error();
auto image = image_or_error.release_value();
image->set_path(path);
if (json.has("guides"))