-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file metadata to AssetIo (#2123)
This is a replacement for #2106 This adds a `Metadata` struct which contains metadata information about a file, at the moment only the file type. It also adds a `get_metadata` to `AssetIo` trait and an `asset_io` accessor method to `AssetServer` and `LoadContext` I am not sure about the changes in `AndroidAssetIo ` and `WasmAssetIo`.
- Loading branch information
Showing
8 changed files
with
158 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::convert::{TryFrom, TryInto}; | ||
|
||
/// A enum representing a type of file. | ||
#[non_exhaustive] | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
pub enum FileType { | ||
Directory, | ||
File, | ||
} | ||
|
||
impl FileType { | ||
#[inline] | ||
pub const fn is_dir(&self) -> bool { | ||
matches!(self, Self::Directory) | ||
} | ||
|
||
#[inline] | ||
pub const fn is_file(&self) -> bool { | ||
matches!(self, Self::File) | ||
} | ||
} | ||
|
||
impl TryFrom<std::fs::FileType> for FileType { | ||
type Error = std::io::Error; | ||
|
||
fn try_from(file_type: std::fs::FileType) -> Result<Self, Self::Error> { | ||
if file_type.is_dir() { | ||
Ok(Self::Directory) | ||
} else if file_type.is_file() { | ||
Ok(Self::File) | ||
} else { | ||
Err(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
"unknown file type", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
/// Metadata information about a file. | ||
/// | ||
/// This structure is returned from the [`AssetIo::get_metadata`](crate::AssetIo) method. | ||
#[derive(Debug, Clone)] | ||
pub struct Metadata { | ||
file_type: FileType, | ||
} | ||
|
||
impl Metadata { | ||
pub fn new(file_type: FileType) -> Self { | ||
Self { file_type } | ||
} | ||
|
||
#[inline] | ||
pub const fn file_type(&self) -> FileType { | ||
self.file_type | ||
} | ||
|
||
#[inline] | ||
pub const fn is_dir(&self) -> bool { | ||
self.file_type.is_dir() | ||
} | ||
|
||
#[inline] | ||
pub const fn is_file(&self) -> bool { | ||
self.file_type.is_file() | ||
} | ||
} | ||
|
||
impl TryFrom<std::fs::Metadata> for Metadata { | ||
type Error = std::io::Error; | ||
|
||
fn try_from(metadata: std::fs::Metadata) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
file_type: metadata.file_type().try_into()?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters