1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
mod section {
mod header {
mod unvalidated {
use crate::parse::section::unvalidated::Key;
#[test]
fn section_name_only() {
assert_eq!(
Key::parse("core").unwrap(),
Key {
section_name: "core",
subsection_name: None
}
);
}
#[test]
fn section_name_and_subsection() {
assert_eq!(
Key::parse("core.bare").unwrap(),
Key {
section_name: "core",
subsection_name: Some("bare".into())
}
);
}
#[test]
fn section_name_and_subsection_with_separators() {
assert_eq!(
Key::parse("remote.https:///home/user.git").unwrap(),
Key {
section_name: "remote",
subsection_name: Some("https:///home/user.git".into())
}
);
}
}
mod write_to {
use std::borrow::Cow;
use crate::parse::section;
fn header(name: &str, subsection: impl Into<Option<(&'static str, &'static str)>>) -> section::Header<'_> {
let name = section::Name(Cow::Borrowed(name.into()));
if let Some((separator, subsection_name)) = subsection.into() {
section::Header {
name,
separator: Some(Cow::Borrowed(separator.into())),
subsection_name: Some(Cow::Borrowed(subsection_name.into())),
}
} else {
section::Header {
name,
separator: None,
subsection_name: None,
}
}
}
#[test]
fn legacy_subsection_format_does_not_use_escapes() {
let invalid = header("invalid", Some((".", "\\ \"")));
assert_eq!(
invalid.to_bstring(),
"[invalid.\\ \"]",
"no escaping happens for legacy subsections"
);
assert!(invalid.is_legacy());
}
#[test]
fn subsections_escape_two_characters_only() {
let invalid = header("invalid", Some((" ", "\\ \"\npost newline")));
assert_eq!(
invalid.to_bstring(),
"[invalid \"\\\\ \\\"\npost newline\"]",
"newlines are actually invalid in subsection, but they are possible due to unvalidated instance creation"
);
assert!(!invalid.is_legacy());
}
}
}
}
pub(crate) mod util {
//! This module is only included for tests, and contains common unit test helper
//! functions.
use std::{borrow::Cow, convert::TryFrom};
use crate::parse::{section, Comment, Event};
pub fn into_events(events: Vec<Event<'_>>) -> section::Events<'_> {
events.into()
}
pub fn section_header(
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> section::Header<'_> {
let name = section::Name::try_from(name).unwrap();
if let Some((separator, subsection_name)) = subsection.into() {
section::Header {
name,
separator: Some(Cow::Borrowed(separator.into())),
subsection_name: Some(Cow::Borrowed(subsection_name.into())),
}
} else {
section::Header {
name,
separator: None,
subsection_name: None,
}
}
}
pub(crate) fn name_event(name: &'static str) -> Event<'static> {
Event::SectionKey(section::Key(Cow::Borrowed(name.into())))
}
pub(crate) fn value_event(value: &'static str) -> Event<'static> {
Event::Value(Cow::Borrowed(value.into()))
}
pub(crate) fn value_not_done_event(value: &'static str) -> Event<'static> {
Event::ValueNotDone(Cow::Borrowed(value.into()))
}
pub(crate) fn value_done_event(value: &'static str) -> Event<'static> {
Event::ValueDone(Cow::Borrowed(value.into()))
}
pub(crate) fn newline_event() -> Event<'static> {
newline_custom_event("\n")
}
pub(crate) fn newline_custom_event(value: &'static str) -> Event<'static> {
Event::Newline(Cow::Borrowed(value.into()))
}
pub(crate) fn whitespace_event(value: &'static str) -> Event<'static> {
Event::Whitespace(Cow::Borrowed(value.into()))
}
pub(crate) fn comment_event(tag: char, msg: &'static str) -> Event<'static> {
Event::Comment(comment(tag, msg))
}
pub(crate) fn comment(comment_tag: char, comment: &'static str) -> Comment<'static> {
Comment {
tag: comment_tag as u8,
text: Cow::Borrowed(comment.into()),
}
}
pub(crate) const fn fully_consumed<T>(t: T) -> (&'static [u8], T) {
(&[], t)
}
}
|