Skip to main content

happy_cracking/crypto/
url.rs

1use anyhow::Result;
2use clap::Subcommand;
3
4#[derive(Subcommand)]
5pub enum UrlAction {
6    #[command(about = "URL encode")]
7    Encode {
8        #[arg(help = "Input text")]
9        input: String,
10    },
11    #[command(about = "URL decode")]
12    Decode {
13        #[arg(help = "URL encoded string")]
14        input: String,
15    },
16}
17
18pub fn run(action: UrlAction) -> Result<()> {
19    match action {
20        UrlAction::Encode { input } => {
21            println!("{}", encode(&input));
22        }
23        UrlAction::Decode { input } => {
24            println!("{}", decode(&input)?);
25        }
26    }
27    Ok(())
28}
29
30pub fn encode(input: &str) -> String {
31    urlencoding::encode(input).into_owned()
32}
33
34pub fn decode(input: &str) -> Result<String> {
35    urlencoding::decode(input.trim())
36        .map(|s| s.into_owned())
37        .map_err(|e| anyhow::anyhow!("Failed to decode URL: {}", e))
38}