yazi_shared/url/
encode.rs1use std::fmt::{self, Display};
2
3use percent_encoding::{CONTROLS, percent_encode};
4
5use crate::url::Url;
6
7#[derive(Clone, Copy)]
8pub struct Encode<'a>(pub Url<'a>);
9
10impl Display for Encode<'_> {
11 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12 use crate::scheme::Encode as E;
13
14 let loc = percent_encode(self.0.loc().encoded_bytes(), CONTROLS);
15 match self.0 {
16 Url::Regular(_) => write!(f, "regular~://{loc}"),
17 Url::Search { domain, .. } => {
18 write!(f, "search~://{}{}/{loc}", E::domain(domain), E::ports((*self).into()))
19 }
20 Url::Archive { domain, .. } => {
21 write!(f, "archive~://{}{}/{loc}", E::domain(domain), E::ports((*self).into()))
22 }
23 Url::Sftp { domain, .. } => {
24 write!(f, "sftp~://{}{}/{loc}", E::domain(domain), E::ports((*self).into()))
25 }
26 }
27 }
28}