BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / text / utils.rs

utils.rs

eef6c0a4 History
utils.rs 4.7 KiB Download Raw
1use std::sync::Arc;
2
3use data_url::DataUrl;
4use gpui::{Image, ImageFormat};
5
6const NUMBERED_PREFIXES_1: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
7const NUMBERED_PREFIXES_2: &str = "abcdefghijklmnopqrstuvwxyz";
8
9const BULLETS: [&str; 5] = ["•", "◦", "▪", "‣", "⁃"];
10
11/// Returns an ordered-list item's ordinal, defaulting omitted starts to one.
12pub(super) fn ordered_list_ordinal(start: Option<u32>, ix: usize) -> u32 {
13 start
14 .unwrap_or(1)
15 .saturating_add(u32::try_from(ix).unwrap_or(u32::MAX))
16}
17
18/// Returns the prefix for a list item.
19pub(super) fn list_item_prefix(
20 ix: usize,
21 start: Option<u32>,
22 ordered: bool,
23 depth: usize,
24) -> String {
25 if ordered {
26 let ordinal = ordered_list_ordinal(start, ix);
27 if depth == 0 || start == Some(0) {
28 return format!("{ordinal}. ");
29 }
30
31 let alpha_ix = ordinal.saturating_sub(1) as usize;
32 if depth == 1 {
33 return format!(
34 "{}. ",
35 NUMBERED_PREFIXES_1
36 .chars()
37 .nth(alpha_ix % NUMBERED_PREFIXES_1.len())
38 .unwrap()
39 );
40 } else {
41 return format!(
42 "{}. ",
43 NUMBERED_PREFIXES_2
44 .chars()
45 .nth(alpha_ix % NUMBERED_PREFIXES_2.len())
46 .unwrap()
47 );
48 }
49 } else {
50 let depth = depth.min(BULLETS.len() - 1);
51 let bullet = BULLETS[depth];
52 return format!("{} ", bullet);
53 }
54}
55
56/// Decodes a `data:` URL whose mime type names an image format GPUI can
57/// decode. Anything else (another scheme, a non-image body, malformed
58/// base64) yields `None`, and the caller keeps the URL URI-backed so GPUI's
59/// own loader reports the failure.
60pub(super) fn data_url_image(url: &str) -> Option<Arc<Image>> {
61 let data_url = DataUrl::process(url).ok()?;
62 let mime = data_url.mime_type();
63 let format = ImageFormat::from_mime_type(&format!("{}/{}", mime.type_, mime.subtype))?;
64 let (bytes, _fragment) = data_url.decode_to_vec().ok()?;
65 Some(Arc::new(Image::from_bytes(format, bytes)))
66}
67
68#[cfg(test)]
69mod tests {
70 use gpui::ImageFormat;
71
72 use crate::text::utils::{data_url_image, list_item_prefix};
73
74 #[test]
75 fn test_data_url_image() {
76 fn image(url: &str) -> (ImageFormat, Vec<u8>) {
77 let image = data_url_image(url)
78 .unwrap_or_else(|| panic!("expected an embedded image for {url:?}"));
79 (image.format(), image.bytes().to_vec())
80 }
81
82 assert_eq!(
83 image("data:image/png;base64,iVBORw0KGgo="),
84 (ImageFormat::Png, b"\x89PNG\r\n\x1a\n".to_vec())
85 );
86 // Legacy mime aliases and the percent-encoded (non-base64) body form.
87 assert_eq!(
88 image("data:image/jpg;base64,/9j/4A=="),
89 (ImageFormat::Jpeg, b"\xff\xd8\xff\xe0".to_vec())
90 );
91 assert_eq!(
92 image("data:image/svg+xml,%3Csvg%3E%3C/svg%3E"),
93 (ImageFormat::Svg, b"<svg></svg>".to_vec())
94 );
95
96 assert!(data_url_image("https://example.com/logo.png").is_none());
97 assert!(data_url_image("data:text/plain;base64,aGVsbG8=").is_none());
98 assert!(data_url_image("data:image/png;base64,not*base64").is_none());
99 assert!(data_url_image("data:image/png").is_none());
100 }
101
102 #[test]
103 fn test_list_item_prefix() {
104 assert_eq!(list_item_prefix(0, Some(1), true, 0), "1. ");
105 assert_eq!(list_item_prefix(1, Some(1), true, 0), "2. ");
106 assert_eq!(list_item_prefix(2, Some(1), true, 0), "3. ");
107 assert_eq!(list_item_prefix(10, Some(1), true, 0), "11. ");
108 assert_eq!(list_item_prefix(0, Some(3), true, 0), "3. ");
109 assert_eq!(list_item_prefix(1, Some(3), true, 0), "4. ");
110 assert_eq!(list_item_prefix(0, Some(1), true, 1), "A. ");
111 assert_eq!(list_item_prefix(1, Some(1), true, 1), "B. ");
112 assert_eq!(list_item_prefix(0, Some(4), true, 1), "D. ");
113 assert_eq!(list_item_prefix(1, Some(4), true, 1), "E. ");
114 assert_eq!(list_item_prefix(0, Some(1), true, 2), "a. ");
115 assert_eq!(list_item_prefix(1, Some(1), true, 2), "b. ");
116 assert_eq!(list_item_prefix(6, Some(1), true, 2), "g. ");
117 assert_eq!(list_item_prefix(0, Some(0), true, 1), "0. ");
118 assert_eq!(list_item_prefix(1, Some(0), true, 1), "1. ");
119 assert_eq!(list_item_prefix(0, None, false, 0), "• ");
120 assert_eq!(list_item_prefix(0, None, false, 1), "◦ ");
121 assert_eq!(list_item_prefix(0, None, false, 2), "▪ ");
122 assert_eq!(list_item_prefix(0, None, false, 3), "‣ ");
123 assert_eq!(list_item_prefix(0, None, false, 4), "⁃ ");
124 }
125}