Rework loading images from files (#7088)
This PR is a follow-up to #7084, where I noted that I wasn't satisfied with using `SharedUri` to represent both URIs and paths on the local filesystem: > I'm still not entirely happy with this naming, as the file paths that we can store in here are not _really_ URIs, as they are lacking a protocol. > > I want to explore changing `SharedUri` / `SharedUrl` back to alway storing a URL and treat local filepaths differently, as it seems we're conflating two different concerns under the same umbrella, at the moment. `SharedUri` has now been reverted to just containing a `SharedString` with a URI. `ImageSource` now has a new `File` variant that is used to load an image from a `PathBuf`. Release Notes: - N/A
This commit is contained in:
parent
6d4fe8098b
commit
2980f0508c
12 changed files with 163 additions and 166 deletions
|
@ -1,65 +1,25 @@
|
|||
use std::ops::{Deref, DerefMut};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
|
||||
use crate::SharedString;
|
||||
|
||||
/// A URI stored in a [`SharedString`].
|
||||
#[derive(PartialEq, Eq, Hash, Clone)]
|
||||
pub enum SharedUri {
|
||||
/// A path to a local file.
|
||||
File(SharedString),
|
||||
/// A URL to a remote resource.
|
||||
Network(SharedString),
|
||||
}
|
||||
|
||||
impl SharedUri {
|
||||
/// Creates a [`SharedUri`] pointing to a local file.
|
||||
pub fn file<S: Into<SharedString>>(s: S) -> Self {
|
||||
Self::File(s.into())
|
||||
}
|
||||
|
||||
/// Creates a [`SharedUri`] pointing to a remote resource.
|
||||
pub fn network<S: Into<SharedString>>(s: S) -> Self {
|
||||
Self::Network(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SharedUri {
|
||||
fn default() -> Self {
|
||||
Self::Network(SharedString::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SharedUri {
|
||||
type Target = SharedString;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match self {
|
||||
Self::File(s) => s,
|
||||
Self::Network(s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for SharedUri {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
match self {
|
||||
Self::File(s) => s,
|
||||
Self::Network(s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// A [`SharedString`] containing a URI.
|
||||
#[derive(Deref, DerefMut, Default, PartialEq, Eq, Hash, Clone)]
|
||||
pub struct SharedUri(SharedString);
|
||||
|
||||
impl std::fmt::Debug for SharedUri {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::File(s) => write!(f, "File({:?})", s),
|
||||
Self::Network(s) => write!(f, "Network({:?})", s),
|
||||
}
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SharedUri {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.as_ref())
|
||||
write!(f, "{}", self.0.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Into<SharedString>> From<T> for SharedUri {
|
||||
fn from(value: T) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue