Skip to content

Commit

Permalink
Format files
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored and sxyazi committed Jun 14, 2024
1 parent 4473262 commit cfe35cc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 108 deletions.
3 changes: 2 additions & 1 deletion yazi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ pub mod theme;
mod validation;
pub mod which;

use crate::schemes::Schemes;
pub use layout::*;
pub(crate) use pattern::*;
pub(crate) use preset::*;
pub use priority::*;
use yazi_shared::fs::SCHEMES;

use crate::schemes::Schemes;

static MERGED_YAZI: RoCell<String> = RoCell::new();
static MERGED_KEYMAP: RoCell<String> = RoCell::new();
static MERGED_THEME: RoCell<String> = RoCell::new();
Expand Down
11 changes: 5 additions & 6 deletions yazi-config/src/schemes/schemes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use anyhow::Result;
use serde::{Deserialize, Deserializer};
use std::collections::HashMap;

use crate::MERGED_YAZI;

Expand All @@ -11,9 +12,7 @@ pub struct Schemes {
}

impl Default for Schemes {
fn default() -> Self {
toml::from_str(&MERGED_YAZI).unwrap()
}
fn default() -> Self { toml::from_str(&MERGED_YAZI).unwrap() }
}

impl Schemes {
Expand Down Expand Up @@ -47,8 +46,8 @@ impl<'de> Deserialize<'de> for Schemes {
/// Scheme in configuration file.
#[derive(Debug, Deserialize)]
pub struct Scheme {
pub name: String,
pub name: String,
#[serde(rename = "type")]
pub typ: String,
pub typ: String,
pub config: HashMap<String, String>,
}
40 changes: 11 additions & 29 deletions yazi-shared/src/fs/file.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
use std::ops::Add;
use std::time::{Duration, SystemTime};
use std::{cell::Cell, ffi::OsStr, fs::Metadata, ops::Deref};
use std::{cell::Cell, ffi::OsStr, fs::Metadata, ops::{Add, Deref}, time::{Duration, SystemTime}};

use anyhow::{anyhow, Result};
use tokio::fs;

use crate::fs::SCHEMES;
use crate::{
fs::{Cha, ChaKind, Url},
theme::IconCache,
};
use crate::{fs::{Cha, ChaKind, Url, SCHEMES}, theme::IconCache};

#[derive(Clone, Debug, Default)]
pub struct File {
pub url: Url,
pub cha: Cha,
pub url: Url,
pub cha: Cha,
pub link_to: Option<Url>,
pub icon: Cell<IconCache>,
pub icon: Cell<IconCache>,
}

impl Deref for File {
type Target = Cha;

#[inline]
fn deref(&self) -> &Self::Target {
&self.cha
}
fn deref(&self) -> &Self::Target { &self.cha }
}

impl File {
Expand Down Expand Up @@ -102,30 +94,20 @@ impl File {
}

#[inline]
pub fn from_dummy(url: &Url) -> Self {
Self { url: url.to_owned(), ..Default::default() }
}
pub fn from_dummy(url: &Url) -> Self { Self { url: url.to_owned(), ..Default::default() } }
}

impl File {
// --- Url
#[inline]
pub fn url(&self) -> Url {
self.url.clone()
}
pub fn url(&self) -> Url { self.url.clone() }

#[inline]
pub fn name(&self) -> Option<&OsStr> {
self.url.file_name()
}
pub fn name(&self) -> Option<&OsStr> { self.url.file_name() }

#[inline]
pub fn stem(&self) -> Option<&OsStr> {
self.url.file_stem()
}
pub fn stem(&self) -> Option<&OsStr> { self.url.file_stem() }

#[inline]
pub fn parent(&self) -> Option<Url> {
self.url.parent_url()
}
pub fn parent(&self) -> Option<Url> { self.url.parent_url() }
}
7 changes: 4 additions & 3 deletions yazi-shared/src/fs/schemes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::RoCell;
use std::{collections::HashMap, str::FromStr};

use anyhow::{anyhow, Result};
use opendal::Operator;
use std::collections::HashMap;
use std::str::FromStr;

use crate::RoCell;

/// SCHEMES is the read only cell of schemes that already loaded.
pub static SCHEMES: RoCell<Schemes> = RoCell::new();
Expand Down
94 changes: 25 additions & 69 deletions yazi-shared/src/fs/url.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
ffi::{OsStr, OsString},
fmt::{Debug, Display, Formatter},
ops::{Deref, DerefMut},
path::{Path, PathBuf},
};
use std::{ffi::{OsStr, OsString}, fmt::{Debug, Display, Formatter}, ops::{Deref, DerefMut}, path::{Path, PathBuf}};

use percent_encoding::{percent_decode_str, percent_encode, AsciiSet, CONTROLS};
use serde::{Deserialize, Serialize};
Expand All @@ -13,8 +8,8 @@ const ENCODE_SET: &AsciiSet = &CONTROLS.add(b'#');
#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Url {
scheme: UrlScheme,
path: PathBuf,
frag: String,
path: PathBuf,
frag: String,
}

#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand All @@ -29,51 +24,35 @@ pub enum UrlScheme {
impl Deref for Url {
type Target = PathBuf;

fn deref(&self) -> &Self::Target {
&self.path
}
fn deref(&self) -> &Self::Target { &self.path }
}

impl DerefMut for Url {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.path
}
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.path }
}

impl Debug for Url {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path.display())
}
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.path.display()) }
}

impl From<PathBuf> for Url {
fn from(path: PathBuf) -> Self {
Self { path, ..Default::default() }
}
fn from(path: PathBuf) -> Self { Self { path, ..Default::default() } }
}

impl From<&PathBuf> for Url {
fn from(path: &PathBuf) -> Self {
Self::from(path.clone())
}
fn from(path: &PathBuf) -> Self { Self::from(path.clone()) }
}

impl From<&Path> for Url {
fn from(path: &Path) -> Self {
Self::from(path.to_path_buf())
}
fn from(path: &Path) -> Self { Self::from(path.to_path_buf()) }
}

impl From<String> for Url {
fn from(path: String) -> Self {
Self::from(path.as_str())
}
fn from(path: String) -> Self { Self::from(path.as_str()) }
}

impl From<&String> for Url {
fn from(path: &String) -> Self {
Self::from(path.as_str())
}
fn from(path: &String) -> Self { Self::from(path.as_str()) }
}

impl From<&str> for Url {
Expand Down Expand Up @@ -107,21 +86,15 @@ impl From<&str> for Url {
}

impl AsRef<Url> for Url {
fn as_ref(&self) -> &Url {
self
}
fn as_ref(&self) -> &Url { self }
}

impl AsRef<Path> for Url {
fn as_ref(&self) -> &Path {
&self.path
}
fn as_ref(&self) -> &Path { &self.path }
}

impl AsRef<OsStr> for Url {
fn as_ref(&self) -> &OsStr {
self.path.as_os_str()
}
fn as_ref(&self) -> &OsStr { self.path.as_os_str() }
}

impl Display for Url {
Expand Down Expand Up @@ -175,9 +148,7 @@ impl Url {
}

#[inline]
pub fn into_os_string(self) -> OsString {
self.path.into_os_string()
}
pub fn into_os_string(self) -> OsString { self.path.into_os_string() }

#[cfg(unix)]
#[inline]
Expand All @@ -189,14 +160,10 @@ impl Url {
impl Url {
// --- Scheme
#[inline]
pub fn is_regular(&self) -> bool {
self.scheme == UrlScheme::Regular
}
pub fn is_regular(&self) -> bool { self.scheme == UrlScheme::Regular }

#[inline]
pub fn to_regular(&self) -> Self {
self.clone().into_regular()
}
pub fn to_regular(&self) -> Self { self.clone().into_regular() }

#[inline]
pub fn into_regular(mut self) -> Self {
Expand All @@ -205,14 +172,10 @@ impl Url {
}

#[inline]
pub fn is_search(&self) -> bool {
self.scheme == UrlScheme::Search
}
pub fn is_search(&self) -> bool { self.scheme == UrlScheme::Search }

#[inline]
pub fn to_search(&self, frag: String) -> Self {
self.clone().into_search(frag)
}
pub fn to_search(&self, frag: String) -> Self { self.clone().into_search(frag) }

#[inline]
pub fn into_search(mut self, frag: String) -> Self {
Expand All @@ -222,14 +185,10 @@ impl Url {
}

#[inline]
pub fn is_archive(&self) -> bool {
self.scheme == UrlScheme::Archive
}
pub fn is_archive(&self) -> bool { self.scheme == UrlScheme::Archive }

#[inline]
pub fn to_archive(&self) -> Self {
self.clone().into_archive()
}
pub fn to_archive(&self) -> Self { self.clone().into_archive() }

#[inline]
pub fn into_archive(mut self) -> Self {
Expand All @@ -239,7 +198,8 @@ impl Url {

/// Check and fetch remote scheme.
///
/// Returns None if current url is not remote, otherwise returns `Some(scheme)`
/// Returns None if current url is not remote, otherwise returns
/// `Some(scheme)`
#[inline]
pub fn is_remote(&self) -> Option<&str> {
match &self.scheme {
Expand All @@ -250,15 +210,11 @@ impl Url {

// --- Path
#[inline]
pub fn set_path(&mut self, path: PathBuf) {
self.path = path;
}
pub fn set_path(&mut self, path: PathBuf) { self.path = path; }

// --- Frag
#[inline]
pub fn frag(&self) -> &str {
&self.frag
}
pub fn frag(&self) -> &str { &self.frag }
}

impl From<&str> for UrlScheme {
Expand Down

0 comments on commit cfe35cc

Please sign in to comment.