Generalize disk file creation logic
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
28
virtweb_backend/src/utils/file_size_utils.rs
Normal file
28
virtweb_backend/src/utils/file_size_utils.rs
Normal file
@ -0,0 +1,28 @@
|
||||
#[derive(
|
||||
serde::Serialize, serde::Deserialize, Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
|
||||
)]
|
||||
pub struct FileSize(usize);
|
||||
|
||||
impl FileSize {
|
||||
pub const fn from_bytes(size: usize) -> Self {
|
||||
Self(size)
|
||||
}
|
||||
|
||||
pub const fn from_mb(mb: usize) -> Self {
|
||||
Self(mb * 1000 * 1000)
|
||||
}
|
||||
|
||||
pub const fn from_gb(gb: usize) -> Self {
|
||||
Self(gb * 1000 * 1000 * 1000)
|
||||
}
|
||||
|
||||
/// Get file size as bytes
|
||||
pub fn as_bytes(&self) -> usize {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Get file size as megabytes
|
||||
pub fn as_mb(&self) -> usize {
|
||||
self.0 / (1000 * 1000)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user