1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

LibCore: Do not return an Optional from Resource:::filesystem_path

This API never returns OptionalNone, and all callers already assume as
much.
This commit is contained in:
Timothy Flynn 2023-11-05 08:05:22 -05:00 committed by Andreas Kling
parent 2a1fc96650
commit 98a82565cd
6 changed files with 16 additions and 9 deletions

View file

@ -53,7 +53,7 @@ ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
return MUST(String::formatted("{}://{}", m_scheme == Scheme::Resource ? "resource"sv : "file"sv, m_path)); return MUST(String::formatted("{}://{}", m_scheme == Scheme::Resource ? "resource"sv : "file"sv, m_path));
} }
[[nodiscard]] Optional<String> Resource::filesystem_path() const [[nodiscard]] String Resource::filesystem_path() const
{ {
return ResourceImplementation::the().filesystem_path(*this); return ResourceImplementation::the().filesystem_path(*this);
} }
@ -97,4 +97,5 @@ ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
{ {
return FixedMemoryStream(data()); return FixedMemoryStream(data());
} }
} }

View file

@ -18,6 +18,7 @@
#include <LibCore/MappedFile.h> #include <LibCore/MappedFile.h>
namespace Core { namespace Core {
class Resource : public RefCounted<Resource> { class Resource : public RefCounted<Resource> {
public: public:
static ErrorOr<NonnullRefPtr<Resource>> load_from_filesystem(StringView); static ErrorOr<NonnullRefPtr<Resource>> load_from_filesystem(StringView);
@ -28,7 +29,7 @@ public:
[[nodiscard]] String uri() const; [[nodiscard]] String uri() const;
[[nodiscard]] String filename() const; [[nodiscard]] String filename() const;
[[nodiscard]] Optional<String> filesystem_path() const; [[nodiscard]] String filesystem_path() const;
[[nodiscard]] ByteBuffer clone_data() const; [[nodiscard]] ByteBuffer clone_data() const;
[[nodiscard]] ByteBuffer release_data() &&; [[nodiscard]] ByteBuffer release_data() &&;

View file

@ -70,14 +70,14 @@ Vector<String> ResourceImplementation::child_names(Resource const& resource)
VERIFY(resource.m_scheme == Resource::Scheme::File); VERIFY(resource.m_scheme == Resource::Scheme::File);
Vector<String> children; Vector<String> children;
Core::DirIterator it(resource.filesystem_path().release_value().to_deprecated_string(), Core::DirIterator::SkipParentAndBaseDir); Core::DirIterator it(resource.filesystem_path().to_deprecated_string(), Core::DirIterator::SkipParentAndBaseDir);
while (it.has_next()) while (it.has_next())
children.append(MUST(String::from_deprecated_string(it.next_path()))); children.append(MUST(String::from_deprecated_string(it.next_path())));
return children; return children;
} }
Optional<String> ResourceImplementation::filesystem_path(Resource const& resource) String ResourceImplementation::filesystem_path(Resource const& resource)
{ {
if (resource.m_scheme == Resource::Scheme::Resource) if (resource.m_scheme == Resource::Scheme::Resource)
return filesystem_path_for_resource_scheme(resource.m_path); return filesystem_path_for_resource_scheme(resource.m_path);

View file

@ -11,11 +11,12 @@
#include <LibCore/Resource.h> #include <LibCore/Resource.h>
namespace Core { namespace Core {
class ResourceImplementation { class ResourceImplementation {
public: public:
ErrorOr<NonnullRefPtr<Resource>> load_from_uri(StringView); ErrorOr<NonnullRefPtr<Resource>> load_from_uri(StringView);
Vector<String> child_names(Resource const&); Vector<String> child_names(Resource const&);
Optional<String> filesystem_path(Resource const&); String filesystem_path(Resource const&);
virtual ~ResourceImplementation() = default; virtual ~ResourceImplementation() = default;
@ -25,7 +26,7 @@ public:
protected: protected:
virtual ErrorOr<NonnullRefPtr<Resource>> load_from_resource_scheme_uri(StringView) = 0; virtual ErrorOr<NonnullRefPtr<Resource>> load_from_resource_scheme_uri(StringView) = 0;
virtual Vector<String> child_names_for_resource_scheme(Resource const&) = 0; virtual Vector<String> child_names_for_resource_scheme(Resource const&) = 0;
virtual Optional<String> filesystem_path_for_resource_scheme(String const&) = 0; virtual String filesystem_path_for_resource_scheme(String const&) = 0;
static bool is_directory(StringView filesystem_path); static bool is_directory(StringView filesystem_path);
@ -33,4 +34,5 @@ protected:
static NonnullRefPtr<Resource> make_resource(String full_path, ByteBuffer); static NonnullRefPtr<Resource> make_resource(String full_path, ByteBuffer);
static NonnullRefPtr<Resource> make_directory_resource(String full_path); static NonnullRefPtr<Resource> make_directory_resource(String full_path);
}; };
} }

View file

@ -11,6 +11,7 @@
#include <LibCore/ResourceImplementationFile.h> #include <LibCore/ResourceImplementationFile.h>
namespace Core { namespace Core {
ResourceImplementationFile::ResourceImplementationFile(String base_directory) ResourceImplementationFile::ResourceImplementationFile(String base_directory)
: m_base_directory(move(base_directory)) : m_base_directory(move(base_directory))
{ {
@ -33,14 +34,14 @@ ErrorOr<NonnullRefPtr<Resource>> ResourceImplementationFile::load_from_resource_
Vector<String> ResourceImplementationFile::child_names_for_resource_scheme(Resource const& resource) Vector<String> ResourceImplementationFile::child_names_for_resource_scheme(Resource const& resource)
{ {
Vector<String> children; Vector<String> children;
Core::DirIterator it(resource.filesystem_path().release_value().to_deprecated_string(), Core::DirIterator::SkipParentAndBaseDir); Core::DirIterator it(resource.filesystem_path().to_deprecated_string(), Core::DirIterator::SkipParentAndBaseDir);
while (it.has_next()) while (it.has_next())
children.append(MUST(String::from_deprecated_string(it.next_path()))); children.append(MUST(String::from_deprecated_string(it.next_path())));
return children; return children;
} }
Optional<String> ResourceImplementationFile::filesystem_path_for_resource_scheme(String const& relative_path) String ResourceImplementationFile::filesystem_path_for_resource_scheme(String const& relative_path)
{ {
return MUST(String::from_deprecated_string(LexicalPath::join(m_base_directory, relative_path).string())); return MUST(String::from_deprecated_string(LexicalPath::join(m_base_directory, relative_path).string()));
} }

View file

@ -12,15 +12,17 @@
#include <LibCore/ResourceImplementation.h> #include <LibCore/ResourceImplementation.h>
namespace Core { namespace Core {
class ResourceImplementationFile : public ResourceImplementation { class ResourceImplementationFile : public ResourceImplementation {
public: public:
explicit ResourceImplementationFile(String base_directory); explicit ResourceImplementationFile(String base_directory);
virtual ErrorOr<NonnullRefPtr<Resource>> load_from_resource_scheme_uri(StringView) override; virtual ErrorOr<NonnullRefPtr<Resource>> load_from_resource_scheme_uri(StringView) override;
virtual Vector<String> child_names_for_resource_scheme(Resource const&) override; virtual Vector<String> child_names_for_resource_scheme(Resource const&) override;
virtual Optional<String> filesystem_path_for_resource_scheme(String const&) override; virtual String filesystem_path_for_resource_scheme(String const&) override;
private: private:
String m_base_directory; String m_base_directory;
}; };
} }