1use teaql_tool_core::{Result, TeaQLToolError};
2use url::Url;
3use percent_encoding::{utf8_percent_encode, percent_decode_str, NON_ALPHANUMERIC};
4
5#[derive(Debug, Clone)]
6pub struct UrlTool;
7
8impl UrlTool {
9 pub fn new() -> Self { Self }
10
11 pub fn parse(&self, url: &str) -> Result<Url> {
12 Url::parse(url).map_err(|e| TeaQLToolError::ParseError(e.to_string()))
13 }
14
15 pub fn encode(&self, text: &str) -> String {
16 utf8_percent_encode(text, NON_ALPHANUMERIC).to_string()
17 }
18
19 pub fn decode(&self, text: &str) -> Result<String> {
20 percent_decode_str(text).decode_utf8().map(|s| s.into_owned()).map_err(|e| TeaQLToolError::ParseError(e.to_string()))
21 }
22}
23
24impl Default for UrlTool {
25 fn default() -> Self { Self::new() }
26}