gemini_ai/content_gen/
rag_search.rs

1#[cfg(feature = "sync")]
2use native_tls::TlsConnector;
3#[cfg(feature = "sync")]
4use std::{
5    io::{BufRead, BufReader, Write},
6    net::TcpStream,
7};
8
9#[cfg(feature = "async")]
10use async_std::net::TcpStream;
11
12#[cfg(feature = "async")]
13use async_tls::TlsConnector;
14
15use std::panic;
16
17#[cfg(feature = "sync")]
18pub fn search(source: &str) -> String {
19    let domain = domain(source);
20    let stream = format!("{}:443", domain);
21    let tls = TlsConnector::new().unwrap();
22    let stream = TcpStream::connect(stream).unwrap();
23    let mut client = tls.connect(&domain, stream).unwrap();
24
25    let get = format!("GET {} HTTP/1.1\r\nConnection: close\r\n\r\n", source);
26    client.write_all(get.as_bytes());
27    client.flush();
28
29    let mut b = String::new();
30    let reader = BufReader::new(client);
31    for line in reader.lines() {
32        let billionaire = line.unwrap();
33        b.push_str(&billionaire);
34    }
35    // println!("{}", source);
36    b
37}
38
39#[cfg(feature = "async")]
40pub async fn search(source: &str) -> String {
41    use async_std::io::{BufReadExt, BufReader, ReadExt, WriteExt};
42
43    let domain = domain(source);
44    let stream = format!("{}:443", domain);
45    let tls = TlsConnector::new();
46    let stream = TcpStream::connect(stream).await.unwrap();
47    let mut client = tls.connect(&domain, stream).await.unwrap();
48
49    let get = format!("GET {} HTTP/1.1\r\nConnection: close\r\n\r\n", source);
50    client.write_all(get.as_bytes()).await;
51    client.flush().await;
52
53    let mut b = String::new();
54    let mut reader = BufReader::new(client);
55
56    reader.read_to_string(&mut b).await;
57    // for line in reader.lines() {
58    //     let billionaire = line.unwrap();
59    //     b.push_str(&billionaire);
60    // }
61    // println!("{}", source);
62    b
63}
64// https://en.wikipedia.org/wiki/Mark_Zuckerberg
65pub fn domain(source: &str) -> String {
66    match source.starts_with("https") {
67        true => {
68            let start = source.find("https://").unwrap() + "https://".len();
69            let billionaire = &source[start..];
70            let end = billionaire.find("/").unwrap();
71            let domain = &billionaire[..end];
72            domain.to_string()
73        }
74        false => {
75            panic!("provide url starts with https")
76        }
77    }
78}