Skip to content

Commit

Permalink
Clean up ToVirtualPath semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 10, 2024
1 parent 15eccef commit eb7af9f
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 48 deletions.
2 changes: 1 addition & 1 deletion core/audio/src/clips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl AudioClip {
}

/// Creates a new audio clip from the given WAV file.
pub fn from_wav_path(path: &impl ToVirtualPath) -> Result<Self, ClipError> {
pub fn from_wav_path(path: impl ToVirtualPath) -> Result<Self, ClipError> {
let path = path.to_virtual_path();
let stream = path.open_input_stream().map_err(|_| ClipError::FailedToCreate)?;
let bytes = stream.to_buffer().map_err(|_| ClipError::FailedToCreate)?;
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/abstractions/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<T> AssetRef<T> {

/// Creates an asset from a virtual path.
#[inline]
pub fn from_path(path: &impl ToVirtualPath) -> Self {
pub fn from_path(path: impl ToVirtualPath) -> Self {
Self {
id: AssetId::Path(path.to_virtual_path()),
_marker: std::marker::PhantomData,
Expand Down
12 changes: 6 additions & 6 deletions core/common/src/io/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait Serialize: Sized {
}

/// Serializes the type to a binary file.
fn to_binary_path(&self, path: &impl ToVirtualPath) -> Result<(), StreamError> {
fn to_binary_path(&self, path: impl ToVirtualPath) -> Result<(), StreamError> {
Self::to_format_path::<BinaryFormat>(self, path)
}

Expand All @@ -41,7 +41,7 @@ pub trait Serialize: Sized {
}

/// Serializes the type to a JSON file.
fn to_json_path(&self, path: &impl ToVirtualPath) -> Result<(), StreamError> {
fn to_json_path(&self, path: impl ToVirtualPath) -> Result<(), StreamError> {
Self::to_format_path::<JsonFormat>(self, path)
}

Expand All @@ -66,7 +66,7 @@ pub trait Serialize: Sized {
}

/// Serializes the type to a path with a specific format.
fn to_format_path<F: Format + Default>(&self, path: &impl ToVirtualPath) -> Result<(), StreamError> {
fn to_format_path<F: Format + Default>(&self, path: impl ToVirtualPath) -> Result<(), StreamError> {
let path = path.to_virtual_path();
let mut format = F::default();
let mut stream = path.open_output_stream()?;
Expand All @@ -86,7 +86,7 @@ pub trait Deserialize: Sized {
}

/// Deserializes the type from a binary path.
fn from_binary_path(path: &impl ToVirtualPath) -> Result<Self, StreamError> {
fn from_binary_path(path: impl ToVirtualPath) -> Result<Self, StreamError> {
Self::from_format_path::<BinaryFormat>(path)
}

Expand All @@ -101,7 +101,7 @@ pub trait Deserialize: Sized {
}

/// Deserializes the type from a JSON path.
fn from_json_path(path: &impl ToVirtualPath) -> Result<Self, StreamError> {
fn from_json_path(path: impl ToVirtualPath) -> Result<Self, StreamError> {
Self::from_format_path::<JsonFormat>(path)
}

Expand All @@ -122,7 +122,7 @@ pub trait Deserialize: Sized {
}

/// Deserializes the type from a path with a specific format.
fn from_format_path<F: Format + Default>(path: &impl ToVirtualPath) -> Result<Self, StreamError> {
fn from_format_path<F: Format + Default>(path: impl ToVirtualPath) -> Result<Self, StreamError> {
let path = path.to_virtual_path();
let mut format = F::default();
let mut stream = path.open_input_stream()?;
Expand Down
8 changes: 4 additions & 4 deletions core/common/src/io/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub trait FromStream: Sized {
type Error: From<StreamError> = StreamError;

/// Imports the type from a path.
fn from_path(path: &impl ToVirtualPath) -> Result<Self, Self::Error> {
fn from_path(path: impl ToVirtualPath) -> Result<Self, Self::Error> {
let path = path.to_virtual_path();
let mut stream = path.open_input_stream().map_err(|_| StreamError::GeneralFailure)?;

Self::from_stream(&mut stream)
}

/// Imports the type from a path asynchronously.
async fn from_path_async(path: &impl ToVirtualPath) -> Result<Self, Self::Error> {
async fn from_path_async(path: impl ToVirtualPath) -> Result<Self, Self::Error> {
let path = path.to_virtual_path();
let mut stream = path.open_input_stream().map_err(|_| StreamError::GeneralFailure)?;

Expand Down Expand Up @@ -58,15 +58,15 @@ pub trait ToStream: Sized {
type Error: From<StreamError> = StreamError;

/// Exports the type to a path.
fn to_path(&self, path: &impl ToVirtualPath) -> Result<(), Self::Error> {
fn to_path(&self, path: impl ToVirtualPath) -> Result<(), Self::Error> {
let path = path.to_virtual_path();
let mut stream = path.open_output_stream().map_err(|_| StreamError::GeneralFailure)?;

self.to_stream(&mut stream)
}

/// Exports the type to a path asynchronously.
async fn to_path_async(&self, path: &impl ToVirtualPath) -> Result<(), Self::Error> {
async fn to_path_async(&self, path: impl ToVirtualPath) -> Result<(), Self::Error> {
let path = path.to_virtual_path();
let mut stream = path.open_output_stream().map_err(|_| StreamError::GeneralFailure)?;

Expand Down
22 changes: 3 additions & 19 deletions core/common/src/io/virtualfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,41 +216,25 @@ impl std::fmt::Display for VirtualPath {
/// Represents a type that can be converted into a [`VirtualPath`].
pub trait ToVirtualPath {
/// Converts the type into a [`VirtualPath`].
fn to_virtual_path(&self) -> VirtualPath;
fn to_virtual_path(self) -> VirtualPath;
}

/// Allow virtual paths to be converted into themselves.
impl ToVirtualPath for VirtualPath {
#[inline]
fn to_virtual_path(&self) -> VirtualPath {
fn to_virtual_path(self) -> VirtualPath {
self.clone()
}
}

/// Allow string references to be converted into [`VirtualPath`] instances.
impl<R: AsRef<str>> ToVirtualPath for R {
#[inline]
fn to_virtual_path(&self) -> VirtualPath {
fn to_virtual_path(self) -> VirtualPath {
VirtualPath::new(self.as_ref())
}
}

/// Allow string references to be converted into [`VirtualPath`] instances.
impl From<&str> for VirtualPath {
#[inline]
fn from(value: &str) -> Self {
VirtualPath::new(value)
}
}

/// Allow string references to be converted into [`VirtualPath`] instances.
impl From<String> for VirtualPath {
#[inline]
fn from(value: String) -> Self {
VirtualPath::new(&value)
}
}

/// A potential error that can occur when interacting with a [`FileSystem`].
#[derive(Debug)]
pub enum FileSystemError {
Expand Down
4 changes: 2 additions & 2 deletions core/common/src/io/virtualfs/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl FileSystem for LocalFileSystem {
let path = entry.path();

if path.is_file() {
results.push(VirtualPath::from(path.to_string_lossy().as_ref()));
results.push(VirtualPath::new(path.to_string_lossy().as_ref()));
}
}

Expand All @@ -48,7 +48,7 @@ impl FileSystem for LocalFileSystem {
let path = entry.path();

if path.is_dir() {
results.push(VirtualPath::from(path.to_string_lossy().as_ref()));
results.push(VirtualPath::new(path.to_string_lossy().as_ref()));
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/common/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl LuaScriptEngine {
}

/// Loads the given script and executes it.
pub fn load_exec(&self, path: &impl ToVirtualPath) -> LuaResult<()> {
pub fn load_exec(&self, path: impl ToVirtualPath) -> LuaResult<()> {
let script = path
.to_virtual_path()
.read_all_text()
Expand All @@ -60,7 +60,7 @@ impl LuaScriptEngine {
}

/// Asynchronously loads the given script and evaluates it.
pub async fn load_exec_async(&self, path: &impl ToVirtualPath) -> LuaResult<()> {
pub async fn load_exec_async(&self, path: impl ToVirtualPath) -> LuaResult<()> {
let script = path
.to_virtual_path()
.read_all_text_async()
Expand All @@ -71,7 +71,7 @@ impl LuaScriptEngine {
}

/// Loads the given script and evaluates it.
pub fn load_eval<R: for<'lua> FromLua<'lua>>(&self, path: &impl ToVirtualPath) -> LuaResult<R> {
pub fn load_eval<R: for<'lua> FromLua<'lua>>(&self, path: impl ToVirtualPath) -> LuaResult<R> {
let script = path
.to_virtual_path()
.read_all_text()
Expand All @@ -81,7 +81,7 @@ impl LuaScriptEngine {
}

/// Asynchronously loads the given script and evaluates it.
pub async fn load_eval_async<R: for<'lua> FromLua<'lua>>(&self, path: &impl ToVirtualPath) -> LuaResult<R> {
pub async fn load_eval_async<R: for<'lua> FromLua<'lua>>(&self, path: impl ToVirtualPath) -> LuaResult<R> {
let script = path
.to_virtual_path()
.read_all_text_async()
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/maths/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<P: Pixel> ColorPalette<P> {
}

/// Loads a palette from the given file path.
pub fn from_path(path: &impl ToVirtualPath) -> Result<Self, ColorPaletteError> {
pub fn from_path(path: impl ToVirtualPath) -> Result<Self, ColorPaletteError> {
let path = path.to_virtual_path();
let stream = path
.open_input_stream()
Expand Down
2 changes: 1 addition & 1 deletion core/graphics/src/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<P: Pixel> Image<P> {
}

/// Loads an image from the given path.
pub fn from_path(path: &impl ToVirtualPath) -> Result<Self, ImageError> {
pub fn from_path(path: impl ToVirtualPath) -> Result<Self, ImageError> {
let path = path.to_virtual_path();
let mut stream = path.open_input_stream().map_err(ImageError::IoError)?;

Expand Down
2 changes: 1 addition & 1 deletion core/graphics/src/materials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct Material {

impl Material {
/// Constructs a new material for the [`ShaderProgram`] at the given path.
pub fn from_shader_path<S: ShaderLanguage>(path: &impl ToVirtualPath) -> Result<Self, ShaderError> {
pub fn from_shader_path<S: ShaderLanguage>(path: impl ToVirtualPath) -> Result<Self, ShaderError> {
Ok(Self::from_shader_program(&ShaderProgram::from_path::<S>(path)?))
}

Expand Down
4 changes: 2 additions & 2 deletions core/graphics/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ShaderProgram {
}

/// Loads a [`ShaderProgram`] from the given [`VirtualPath`] code.
pub fn from_path<S: ShaderLanguage>(path: &impl ToVirtualPath) -> Result<Self, ShaderError> {
pub fn from_path<S: ShaderLanguage>(path: impl ToVirtualPath) -> Result<Self, ShaderError> {
let path = path.to_virtual_path();
let mut stream = path.open_input_stream().map_err(|_| ShaderError::FailedToLoad)?;

Expand Down Expand Up @@ -139,7 +139,7 @@ impl ShaderProgram {
}

/// Reloads the [`ShaderProgram`] from a file at the given virtual path.
pub fn load_from_path<S: ShaderLanguage>(&self, path: &impl ToVirtualPath) -> Result<(), ShaderError> {
pub fn load_from_path<S: ShaderLanguage>(&self, path: impl ToVirtualPath) -> Result<(), ShaderError> {
let path = path.to_virtual_path();
let mut stream = path.open_input_stream().map_err(|_| ShaderError::FailedToLoad)?;

Expand Down
2 changes: 1 addition & 1 deletion core/graphics/src/shaders/lang/glsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl ShaderProgram {
}

/// Loads a [`ShaderProgram`] from the given raw GLSL shader code file.
pub fn from_glsl_path(path: &impl ToVirtualPath) -> Result<Self, ShaderError> {
pub fn from_glsl_path(path: impl ToVirtualPath) -> Result<Self, ShaderError> {
Self::from_path::<GLSL>(path)
}

Expand Down
2 changes: 1 addition & 1 deletion core/graphics/src/shaders/lang/shady.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl ShaderProgram {
}

/// Loads a [`ShaderProgram`] from the given raw shady shader code file.
pub fn from_shady_path(path: &impl ToVirtualPath) -> Result<Self, ShaderError> {
pub fn from_shady_path(path: impl ToVirtualPath) -> Result<Self, ShaderError> {
Self::from_path::<Shady>(path)
}

Expand Down
2 changes: 1 addition & 1 deletion core/graphics/src/sprites/aseprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ mod tests {

#[test]
fn it_should_load_a_simple_aseprite_file() {
let file = AsepriteFile::from_path(&"assets/test.ase").unwrap();
let file = AsepriteFile::from_path("assets/test.ase").unwrap();

println!("{:#?}", file);

Expand Down
2 changes: 1 addition & 1 deletion core/graphics/src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Texture {
}

/// Loads a texture from the given path.
pub fn from_path(path: &impl ToVirtualPath) -> Result<Self, TextureError> {
pub fn from_path(path: impl ToVirtualPath) -> Result<Self, TextureError> {
let image = Image::<Color32>::from_path(path).map_err(TextureError::InvalidImage)?;

Self::from_image(&image)
Expand Down
2 changes: 1 addition & 1 deletion editor/src/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Project {

/// The root [`VirtualPath`] for the project.
pub fn root_path(&self) -> VirtualPath {
self.details.path.to_virtual_path()
self.details.path.clone().to_virtual_path()
}

/// Reads the [`Version`] of the project from the settings file.
Expand Down
2 changes: 1 addition & 1 deletion examples/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
&Mat4::orthographic_lh(0.0, 1024.0, 768.0, 0.0, -1.0, 1.0),
);

let texture = Texture::from_path(&"assets/sprites/bunny.png").unwrap();
let texture = Texture::from_path("assets/sprites/bunny.png").unwrap();

while window.update() {
graphics().clear_color_buffer(Color::WHITE);
Expand Down

0 comments on commit eb7af9f

Please sign in to comment.