use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
const PATH_SEGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'<')
.add(b'>')
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}')
.add(b'/')
.add(b'%')
.add(b':')
.add(b';')
.add(b'=')
.add(b'@')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'^')
.add(b'|')
.add(b'!')
.add(b'$')
.add(b'&')
.add(b'\'')
.add(b'(')
.add(b')')
.add(b'*')
.add(b'+')
.add(b',');
#[allow(dead_code)] pub(crate) fn encode_path_segment(segment: &str) -> String {
utf8_percent_encode(segment, PATH_SEGMENT_ENCODE_SET).to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_symbol_unchanged() {
assert_eq!(encode_path_segment("AAPL"), "AAPL");
}
#[test]
fn dot_separated_ticker_unchanged() {
assert_eq!(encode_path_segment("BRK.B"), "BRK.B");
}
#[test]
fn question_mark_is_encoded() {
assert_eq!(encode_path_segment("FOO?bar"), "FOO%3Fbar");
}
#[test]
fn hash_is_encoded() {
assert_eq!(encode_path_segment("FOO#bar"), "FOO%23bar");
}
#[test]
fn slash_is_encoded() {
assert_eq!(encode_path_segment("a/b"), "a%2Fb");
}
#[test]
fn space_is_encoded() {
assert_eq!(encode_path_segment("a b"), "a%20b");
}
#[test]
fn dot_dot_is_preserved_literally() {
assert_eq!(encode_path_segment(".."), "..");
}
#[test]
fn dot_dot_slash_is_encoded() {
assert_eq!(encode_path_segment("../foo"), "..%2Ffoo");
}
}