#![warn(
missing_docs,
missing_debug_implementations,
private_interfaces,
private_bounds,
rustdoc::private_intra_doc_links
)]
pub mod desktop;
pub mod entry;
pub mod entry_group;
pub mod error;
pub mod iter;
pub mod key;
#[inline]
pub(crate) fn format_comment(comment: &mut String, string: &str) {
for line in string.lines() {
let is_blank = line.trim().is_empty();
if comment.is_empty() {
if is_blank {
comment.push('\n');
} else {
comment.push_str("# ");
comment.push_str(line);
}
continue;
}
if is_blank {
if !comment.ends_with('\n') {
comment.push('\n');
}
comment.push('\n');
continue;
}
if !comment.ends_with('\n') {
comment.push('\n');
}
comment.push_str("# ");
comment.push_str(line);
}
if string.ends_with('\n') && !comment.ends_with('\n') {
comment.push('\n');
}
}
#[cfg(test)]
mod tests_format_comment {
use super::*;
#[test]
fn test_comment_empty_new_line() {
let mut comment = String::new();
format_comment(&mut comment, "\nTest line");
assert_eq!("\n# Test line", comment);
}
#[test]
fn test_comment_empty_not_new_line() {
let mut comment = String::new();
let string = "Test format comment first line";
format_comment(&mut comment, string);
assert_eq!("# Test format comment first line", comment);
}
#[test]
fn test_comment_not_empty_end_not_new_line() {
let mut comment = String::new();
let string = "Test format comment first line";
format_comment(&mut comment, string);
assert_eq!("# Test format comment first line", comment);
let second_line = "Test format comment second line";
format_comment(&mut comment, second_line);
assert_eq!(
"# Test format comment first line\n# Test format comment second line",
comment
);
}
#[test]
fn test_comment_not_empty_end_new_line() {
let mut comment = String::new();
format_comment(&mut comment, "\n");
assert_eq!("\n", comment);
let line = "Test format comment line";
format_comment(&mut comment, line);
assert_eq!("\n# Test format comment line", comment);
let mut comment = String::new();
format_comment(&mut comment, "Test format comment first line\n");
assert_eq!("# Test format comment first line\n", comment);
let second_line = "Test format comment second line";
format_comment(&mut comment, second_line);
assert_eq!(
"# Test format comment first line\n# Test format comment second line",
comment
);
}
#[test]
fn test_format_two_new_line() {
let mut comment = String::new();
let string = "Test format comment first line\nTest format comment second line\n\n";
format_comment(&mut comment, string);
assert_eq!(
"# Test format comment first line\n# Test format comment second line\n\n",
comment
);
let mut comment = String::new();
let first_string =
"\n\nTest format comment first line\n\nTest format comment second line\n\n";
format_comment(&mut comment, first_string);
let second_string = "\n\nTest format comment second string, first line\n\nTest format comment second, second line\n\n";
format_comment(&mut comment, second_string);
assert_eq!(
"\n\n# Test format comment first line\n\n# Test format comment second line\n\n\
\n\n# Test format comment second string, first line\n\n# Test format comment second, second line\n\n",
comment
);
}
#[test]
fn test_format_comment_table_driven() {
let test_cases: Vec<(&str, &str)> = vec![
("Simple comment", "# Simple comment"),
("Line 1\nLine 2", "# Line 1\n# Line 2"),
("Line 1\nLine 2\n", "# Line 1\n# Line 2\n"),
("!@$%^&*()", "# !@$%^&*()"),
("key=value#test", "# key=value#test"),
("[test] {data} <xml>", "# [test] {data} <xml>"),
(
"/path/to/file\\windows\\path",
"# /path/to/file\\windows\\path",
),
(
"Name=Application:Version:1.0",
"# Name=Application:Version:1.0",
),
(
"name=\"value\" and 'quoted'",
"# name=\"value\" and 'quoted'",
),
(
"item1;item2;item3, and more",
"# item1;item2;item3, and more",
),
(
"Calculate: 2+2=4, 5-3=2, 3*2=6, 8/2=4",
"# Calculate: 2+2=4, 5-3=2, 3*2=6, 8/2=4",
),
("my_variable-name", "# my_variable-name"),
("100% complete @everyone", "# 100% complete @everyone"),
("What? Why! How?", "# What? Why! How?"),
("~home `command`", "# ~home `command`"),
("^start .end", "# ^start .end"),
(
"process1 | process2 && process3",
"# process1 | process2 && process3",
),
("Café résumé naïve", "# Café résumé naïve"),
("Ελληνικά γράμματα", "# Ελληνικά γράμματα"),
("Русский текст", "# Русский текст"),
("النص العربي", "# النص العربي"),
("中文文本", "# 中文文本"),
("ひらがなカタカナ", "# ひらがなカタカナ"),
("한국어 텍스트", "# 한국어 텍스트"),
("טקסט עברי", "# טקסט עברי"),
(
"Test ✓ Pass ✗ Fail 🚀 Rocket",
"# Test ✓ Pass ✗ Fail 🚀 Rocket",
),
(
"English 日本語 Русский العربية",
"# English 日本語 Русский العربية",
),
("e̊ (combining ring above)", "# e̊ (combining ring above)"),
(
"…ellipsis « quote » guillemets",
"# …ellipsis « quote » guillemets",
),
("First line\n", "# First line\n"),
("Line 1\nLine 2\nLine 3\n", "# Line 1\n# Line 2\n# Line 3\n"),
("\n", "\n"),
("\n\n", "\n\n"),
("\nContent", "\n# Content"),
("Content\n\n", "# Content\n\n"),
("Line 1\n\nLine 2", "# Line 1\n\n# Line 2"),
("Start\n\n\nEnd", "# Start\n\n\n# End"),
("A\n\nB\n", "# A\n\n# B\n"),
("\n \n\t\n", "\n\n\n"),
("Title\n\nAuthor\n\nDesc", "# Title\n\n# Author\n\n# Desc"),
(
"Item 1\n\nЦифра 2\n\n三項目",
"# Item 1\n\n# Цифра 2\n\n# 三項目",
),
(
"Application launcher\nVersion: 1.0",
"# Application launcher\n# Version: 1.0",
),
(
"Name: Firefox\nExec: /usr/bin/firefox\nIcon: firefox",
"# Name: Firefox\n# Exec: /usr/bin/firefox\n# Icon: firefox",
),
(
"Homepage: https://example.com/path?query=1&other=2\nPath: /usr/share/icons",
"# Homepage: https://example.com/path?query=1&other=2\n# Path: /usr/share/icons",
),
(
"key1=value1\nkey2=value2;value3;value4",
"# key1=value1\n# key2=value2;value3;value4",
),
(
"Categories=Network;WebBrowser\nActions=NewWindow;NewPrivateWindow;",
"# Categories=Network;WebBrowser\n# Actions=NewWindow;NewPrivateWindow;",
),
(
"Name=MyApp\nVersion=2.0\n\nAuthor=Me\nLicense=MIT",
"# Name=MyApp\n# Version=2.0\n\n# Author=Me\n# License=MIT",
),
(
"Name[en_US]=American App\nName[de_DE]=Deutsches Programm",
"# Name[en_US]=American App\n# Name[de_DE]=Deutsches Programm",
),
(
"Icon-URL: file:///home/user/Pictures/icon%20with%20spaces.png",
"# Icon-URL: file:///home/user/Pictures/icon%20with%20spaces.png",
),
(" ", "\n"),
(" ", "\n"),
("\t", "\n"),
("\t\t\t", "\n"),
(" \t \t ", "\n"),
(
"This is a very long comment line that contains many characters and should \
be preserved exactly as-is without truncation or modification in any way \
because the spec requires uninterpreted content",
"# This is a very long comment line that contains many characters and should \
be preserved exactly as-is without truncation or modification in any way \
because the spec requires uninterpreted content",
),
("", ""),
(" \n ", "\n\n"),
];
for (input, expected) in test_cases {
let mut comment = String::new();
format_comment(&mut comment, input);
assert_eq!(
expected, comment,
"Failed for input: {:?}\nExpected: {:?}\nGot: {:?}",
input, expected, comment
);
}
}
#[test]
fn test_format_comment_cumulative_calls() {
let mut comment = String::new();
format_comment(&mut comment, "Line 1");
assert_eq!("# Line 1", comment);
format_comment(&mut comment, "Line 2");
assert_eq!("# Line 1\n# Line 2", comment);
format_comment(&mut comment, "");
assert_eq!("# Line 1\n# Line 2", comment);
format_comment(&mut comment, "Line 3");
assert_eq!("# Line 1\n# Line 2\n# Line 3", comment);
}
#[test]
fn test_format_comment_utf8_preservation() {
let mut comment = String::new();
format_comment(&mut comment, "Café ☕");
assert_eq!("# Café ☕", comment);
format_comment(&mut comment, "日本語テキスト");
assert_eq!("# Café ☕\n# 日本語テキスト", comment);
let bytes = comment.as_bytes();
assert!(
bytes.len() > "# Caf Japanese ".len(),
"UTF-8 content appears corrupted"
);
}
#[test]
fn test_format_comment_specification_invariants() {
let mut comment = String::new();
format_comment(&mut comment, "Test\nMultiple\n");
for line in comment.lines() {
if !line.is_empty() {
assert!(
line.starts_with("# "),
"Non-blank line should start with '# ': {:?}",
line
);
}
}
assert!(
comment.ends_with('\n'),
"Comment should end with newline since input did"
);
let mut comment2 = String::new();
format_comment(&mut comment2, "No trailing newline");
assert!(
!comment2.ends_with('\n'),
"Comment should not end with newline since input didn't"
);
}
#[test]
fn test_format_comment_multiline_utf8_round_trip() {
let input = "Title: 应用程序\nVersion: 2.0\n\nAuthor: José García\nStatus: ✓ Complete";
let mut comment = String::new();
format_comment(&mut comment, input);
let lines: Vec<&str> = comment.lines().collect();
assert!(
lines.len() >= 5,
"Should have at least 5 lines (including blank)"
);
for (i, line) in lines.iter().enumerate() {
if !line.is_empty() {
assert!(
line.starts_with("# "),
"Line {} should start with '# ': {:?}",
i + 1,
line
);
}
}
assert!(
comment.contains("应用程序"),
"Chinese characters should be preserved"
);
assert!(comment.contains("2.0"), "Version should be preserved");
assert!(
comment.contains("José García"),
"Accented characters should be preserved"
);
assert!(
comment.contains("✓"),
"Checkmark symbol should be preserved"
);
}
#[test]
fn test_format_comment_special_characters_not_escaped() {
let special_inputs = vec![
"URL: http://example.com?foo=bar&baz=qux",
"Regex: ^[a-zA-Z0-9]+$",
"Path: C:\\Program Files\\App\\",
"Math: 2+2=4, sqrt(16)=4",
"Logic: if (x > 5 && y < 10) { action(); }",
"Quotes: 'single' and \"double\"",
"Brackets: (round) [square] {curly} <angle>",
"Symbols: @mention #hashtag $variable %percent ^exponent",
];
for input in special_inputs {
let mut comment = String::new();
format_comment(&mut comment, input);
let content = comment.strip_prefix("# ").unwrap_or("");
assert_eq!(
input, content,
"Special characters should not be escaped or modified"
);
}
}
#[test]
fn test_format_comment_blank_line_preservation() {
let test_cases = vec![
("\n", "\n", "Single leading blank line"),
("\n\n", "\n\n", "Multiple leading blank lines"),
("text\n\nmore", "# text\n\n# more", "Blank line in middle"),
(
"a\n\n\nb",
"# a\n\n\n# b",
"Multiple consecutive blank lines",
),
(
"x\n\n\n\ny",
"# x\n\n\n\n# y",
"Four consecutive blank lines",
),
];
for (input, expected, description) in test_cases {
let mut comment = String::new();
format_comment(&mut comment, input);
assert_eq!(expected, comment, "Failed: {}", description);
}
}
}