8000 fix(folders): avoid utf-8 panic when inferring namespace by Elliot-32 · Pull Request #409 · hyprlab/hylki · GitHub
[go: up one dir, main page]

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17715,6 +17715,10 @@ impl AppModel {
let Some(folders) = self.folders.get(&account_id) else {
return String::new();
};
Self::infer_folder_namespace(folders)
}

fn infer_folder_namespace(folders: &[Folder]) -> String {
let delim = folders
.iter()
.find_map(|f| f.path.chars().find(|c| matches!(c, '/' | '.' | '\\')))
Expand All @@ -17724,7 +17728,9 @@ impl AppModel {
folders.iter().filter(|f| f.kind == FolderKind::Custom).collect();
if !customs.is_empty()
&& customs.iter().all(|f| {
f.path.len() >= root.len() && f.path[..root.len()].eq_ignore_ascii_case(&root)
f.path
.get(..root.len())
.is_some_and(|prefix| prefix.eq_ignore_ascii_case(&root))
})
{
root
Expand Down Expand Up @@ -23224,6 +23230,48 @@ mod tests {
assert_eq!(super::parse_mid("mailto:a@b.c"), None);
}

#[test]
fn folder_namespace_handles_utf8_paths() {
use crate::models::{Folder, FolderKind};
let custom = |path: &str| Folder {
id: 1,
account_id: 1,
name: path.to_string(),
path: path.to_string(),
kind: FolderKind::Custom,
unread: 0,
};
// Byte six falls inside a character in these top-level paths.
for path in ["abcd摘要", "a收摘要", "📧摘要", "é日語"] {
assert_eq!(super::AppModel::infer_folder_namespace(&[custom(path)]), "", "{path}");
}
for (path, expected) in [
("INBOX/中文", "INBOX/"),
("inbox/中文", "INBOX/"),
("INBOX.中文", "INBOX."),
("INBOX\\中文", "INBOX\\"),
("INBOX/Projects/Nested", "INBOX/"),
("Other/中文", ""),
("收件摘要", ""),
("INBOX", ""),
("", ""),
] {
assert_eq!(super::AppModel::infer_folder_namespace(&[custom(path)]), expected, "{path}");
}
assert_eq!(
super::AppModel::infer_folder_namespace(&[custom("INBOX/中文"), custom("abcd摘要")]),
""
);
assert_eq!(super::AppModel::infer_folder_namespace(&[]), "");
let mut inbox = custom("INBOX");
inbox.kind = FolderKind::Inbox;
assert_eq!(super::AppModel::infer_folder_namespace(&[inbox.clone()]), "");
assert_eq!(
super::AppModel::infer_folder_namespace(&[inbox, custom("INBOX/中文")]),
"INBOX/"
);
}

#[test]
fn folder_roles_override_detection_and_demote_the_old_holder() {
use crate::models::{Folder, FolderKind};
Expand Down
0