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));
}
[[nodiscard]] Optional<String> Resource::filesystem_path() const
[[nodiscard]] String Resource::filesystem_path() const
{
return ResourceImplementation::the().filesystem_path(*this);
}
@ -97,4 +97,5 @@ ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
{
return FixedMemoryStream(data());
}
}

View file

@ -18,6 +18,7 @@
#include <LibCore/MappedFile.h>
namespace Core {
class Resource : public RefCounted<Resource> {
public:
static ErrorOr<NonnullRefPtr<Resource>> load_from_filesystem(StringView);
@ -28,7 +29,7 @@ public:
[[nodiscard]] String uri() const;
[[nodiscard]] String filename() const;
[[nodiscard]] Optional<String> filesystem_path() const;
[[nodiscard]] String filesystem_path() const;
[[nodiscard]] ByteBuffer clone_data() const;
[[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);
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())
children.append(MUST(String::from_deprecated_string(it.next_path())));
return children;
}
Optional<String> ResourceImplementation::filesystem_path(Resource const& resource)
String ResourceImplementation::filesystem_path(Resource const& resource)
{
if (resource.m_scheme == Resource::Scheme::Resource)
return filesystem_path_for_resource_scheme(resource.m_path);

View file

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

View file

@ -11,6 +11,7 @@
#include <LibCore/ResourceImplementationFile.h>
namespace Core {
ResourceImplementationFile::ResourceImplementationFile(String 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> 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())
children.append(MUST(String::from_deprecated_string(it.next_path())));
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()));
}

View file

@ -12,15 +12,17 @@
#include <LibCore/ResourceImplementation.h>
namespace Core {
class ResourceImplementationFile : public ResourceImplementation {
public:
explicit ResourceImplementationFile(String base_directory);
virtual ErrorOr<NonnullRefPtr<Resource>> load_from_resource_scheme_uri(StringView) 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:
String m_base_directory;
};
}