pinecone_sdk/utils/
user_agent.rs1use regex::Regex;
2
3fn build_source_tag(source_tag: &String) -> String {
5 let re = Regex::new(r"[^a-z0-9_: ]").unwrap();
11 let lowercase_tag = source_tag.to_lowercase();
12 let tag = re.replace_all(&lowercase_tag, "");
13 return tag
14 .trim()
15 .split(' ')
16 .filter(|s| !s.is_empty())
17 .collect::<Vec<&str>>()
18 .join("_");
19}
20
21pub fn get_user_agent(source_tag: Option<&str>) -> String {
23 let mut user_agent = format!("lang=rust; pinecone-rust-client={}", "0.1.0");
24 if let Some(st) = source_tag {
25 user_agent.push_str(&format!(
26 "; source_tag={}",
27 build_source_tag(&st.to_string())
28 ));
29 }
30 return user_agent;
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36 use tokio;
37
38 #[tokio::test]
39 async fn test_build_source_tag() {
40 let source_tag = " Hello World!! ".to_string();
41 assert_eq!(build_source_tag(&source_tag), "hello_world");
42 }
43
44 #[tokio::test]
45 async fn test_build_source_tag_special_chars() {
46 let source_tag = " Hello World__:_!@#@# ".to_string();
47 assert_eq!(build_source_tag(&source_tag), "hello_world__:_");
48 }
49
50 #[tokio::test]
51 async fn test_no_source_tag() {
52 assert_eq!(
53 get_user_agent(None),
54 "lang=rust; pinecone-rust-client=0.1.0"
55 );
56 }
57
58 #[tokio::test]
59 async fn test_with_source_tag() {
60 assert_eq!(
61 get_user_agent(Some("tag")),
62 "lang=rust; pinecone-rust-client=0.1.0; source_tag=tag"
63 );
64 }
65}