gemini_client_api/gemini/
utils.rs1use super::types::request::*;
2use crate::utils::{self, MatchedFiles};
3use regex::Regex;
4
5pub struct MarkdownToParts<'a> {
6 base64s: Vec<Option<MatchedFiles>>,
7 markdown: &'a str,
8}
9impl<'a> MarkdownToParts<'a> {
10 pub async fn from_regex(
20 markdown: &'a str,
21 regex: Regex,
22 guess_mime_type: fn(url: &str) -> String,
23 ) -> Self {
24 Self {
25 base64s: utils::get_file_base64s(markdown, regex, guess_mime_type).await,
26 markdown,
27 }
28 }
29 pub async fn new(markdown: &'a str, guess_mime_type: fn(url: &str) -> String) -> Self {
37 let image_regex = Regex::new(r"(?s)!\[.*?].?\((.*?)\)").unwrap();
38 Self {
39 base64s: utils::get_file_base64s(markdown, image_regex, guess_mime_type).await,
40 markdown,
41 }
42 }
43 pub fn process(mut self) -> Vec<Part> {
44 let mut parts: Vec<Part> = Vec::new();
45 let mut removed_length = 0;
46 for file in self.base64s {
47 if let Some(file) = file {
48 let end = file.index + file.length - removed_length;
49 let text = &self.markdown[..end];
50 parts.push(Part::text(text.to_string()));
51 parts.push(Part::inline_data(InlineData::new(
52 file.mime_type,
53 file.base64,
54 )));
55
56 self.markdown = &self.markdown[end..];
57 removed_length += end;
58 }
59 }
60 if self.markdown.len() != 0 {
61 parts.push(Part::text(self.markdown.to_string()));
62 }
63 parts
64 }
65}