Skip to content

Commit

Permalink
feat: add util to mask url
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Jul 22, 2024
1 parent e984511 commit 34999e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/slash_commands/crate_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ use serenity::all::{

use tracing::info;

use crate::utils::ToSnakeCase;
use crate::utils::{mask_url, ToSnakeCase};

const MESSAGE: &str = r#"## [@crate_name@](https://crates.io/crates/@crate_name@)
@description@
@keywords@
@description@@keywords@
@last_version@
@stable_version@
@last_version@@stable_version@
@doc@
@repo@
@web@"#;
Expand Down Expand Up @@ -112,6 +110,11 @@ pub async fn run(client: &Client, options: &[CommandDataOption]) -> String {
}
}

fn maskurl(u: String) -> String {
let masked = mask_url(&u);
format!("[{masked}]({u})")
}

async fn fetch_crate(
client: &Client,
search_type: TypeSearch,
Expand Down Expand Up @@ -180,15 +183,15 @@ async fn fetch_crate(
&(if max_stable_version == newest_version {
String::new()
} else {
format!("Última Version Estable: {max_stable_version}")
format!("\nÚltima Version Estable: {max_stable_version}")
}),
)
.replace(
"@keywords@",
&(if keywords.is_empty() {
String::new()
} else {
format!("-# {}", keywords.join(" | "))
format!("\n-# {}", keywords.join(" | "))
}),
);

Expand All @@ -198,23 +201,29 @@ async fn fetch_crate(
"@doc@",
&format!(
"Documentación: {}",
documentation.as_deref().unwrap_or("None")
documentation.map(maskurl).as_deref().unwrap_or("None")
),
)
.replace(
"@repo@",
&format!("Repositorio: {}", repository.as_deref().unwrap_or("None")),
&format!(
"Repositorio: {}",
repository.map(maskurl).as_deref().unwrap_or("None")
),
)
.replace(
"@web@",
&format!("Página Web: {}", homepage.as_deref().unwrap_or("None")),
&format!(
"Página Web: {}",
homepage.map(maskurl).as_deref().unwrap_or("None")
),
),
TypeSearch::Docs => res
.replace(
"@doc@",
&format!(
"Documentación: {}",
documentation.as_deref().unwrap_or("None")
documentation.map(maskurl).as_deref().unwrap_or("None")
),
)
.replace("@repo@", "")
Expand Down
17 changes: 17 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,20 @@ where
buffer
}
}

const MASKS: [(&str, &str); 4] = [
("https://github.com/", "github:"),
("https://gitlab.com/", "gitlab:"),
("https://docs.rs/", "docs.rs:"),
("https://crates.io/crates", "crates:"),
];

pub fn mask_url(url: &str) -> String {
for (mask, repl) in &MASKS {
if url.starts_with(mask) {
return url.replace(mask, repl);
}
}

url.replace("https://", "").replace("http://", "")
}

0 comments on commit 34999e6

Please sign in to comment.