Enum git_config::parse::Event
source · pub enum Event<'a> {
Comment(Comment<'a>),
SectionHeader(Header<'a>),
SectionKey(Key<'a>),
Value(Cow<'a, BStr>),
Newline(Cow<'a, BStr>),
ValueNotDone(Cow<'a, BStr>),
ValueDone(Cow<'a, BStr>),
Whitespace(Cow<'a, BStr>),
KeyValueSeparator,
}
Expand description
Syntactic events that occurs in the config. Despite all these variants
holding a Cow
instead over a simple reference, the parser will only emit
borrowed Cow
variants.
The Cow
is used here for ease of inserting new, typically owned events as used
in the File
struct when adding values, allowing a mix of owned and borrowed
values.
Variants§
Comment(Comment<'a>)
A comment with a comment tag and the comment itself. Note that the
comment itself may contain additional whitespace and comment markers
at the beginning, like # comment
or ; comment
.
SectionHeader(Header<'a>)
A section header containing the section name and a subsection, if it
exists. For instance, remote "origin"
is parsed to remote
as section
name and origin
as subsection name.
SectionKey(Key<'a>)
A name to a value in a section, like url
in remote.origin.url
.
Value(Cow<'a, BStr>)
A completed value. This may be any single-line string, including the empty string if an implicit boolean value is used. Note that these values may contain spaces and any special character. This value is also unprocessed, so it it may contain double quotes that should be normalized before interpretation.
Newline(Cow<'a, BStr>)
Represents any token used to signify a newline character. On Unix
platforms, this is typically just \n
, but can be any valid newline
sequence. Multiple newlines (such as \n\n
) will be merged as a single
newline event containing a string of multiple newline characters.
ValueNotDone(Cow<'a, BStr>)
Any value that isn’t completed. This occurs when the value is continued
onto the next line by ending it with a backslash.
A Newline
event is guaranteed after, followed by
either a ValueDone, a Whitespace, or another ValueNotDone.
ValueDone(Cow<'a, BStr>)
The last line of a value which was continued onto another line.
With this it’s possible to obtain the complete value by concatenating
the prior ValueNotDone
events.
Whitespace(Cow<'a, BStr>)
A continuous section of insignificant whitespace.
Note that values with internal whitespace will not be separated by this event, hence interior whitespace there is always part of the value.
KeyValueSeparator
This event is emitted when the parser counters a valid =
character
separating the key and value.
This event is necessary as it eliminates the ambiguity for whitespace
events between a key and value event.
Implementations§
source§impl Event<'_>
impl Event<'_>
sourcepub fn to_bstring(&self) -> BString
pub fn to_bstring(&self) -> BString
Serialize this type into a BString
for convenience.
Note that to_string()
can also be used, but might not be lossless.
Examples found in repository?
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.to_bstring(), f)
}
}
impl From<Event<'_>> for BString {
fn from(event: Event<'_>) -> Self {
event.into()
}
}
impl From<&Event<'_>> for BString {
fn from(event: &Event<'_>) -> Self {
event.to_bstring()
}
sourcepub fn to_bstr_lossy(&self) -> &BStr
pub fn to_bstr_lossy(&self) -> &BStr
Turn ourselves into the text we represent, lossy.
Note that this will be partial in case of ValueNotDone
which doesn’t include the backslash, and SectionHeader
will only
provide their name, lacking the sub-section name.
Examples found in repository?
More examples
339 340 341 342 343 344 345 346 347 348 349 350
fn extend_and_assure_newline<'a>(
lhs: &mut FrontMatterEvents<'a>,
rhs: FrontMatterEvents<'a>,
nl: &impl AsRef<[u8]>,
) {
if !ends_with_newline(lhs.as_ref(), nl, true)
&& !rhs.first().map_or(true, |e| e.to_bstr_lossy().starts_with(nl.as_ref()))
{
lhs.push(Event::Newline(Cow::Owned(nl.as_ref().into())))
}
lhs.extend(rhs);
}
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
pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> {
self.header.write_to(&mut out)?;
if self.body.0.is_empty() {
return Ok(());
}
let nl = self
.body
.as_ref()
.iter()
.find_map(extract_newline)
.unwrap_or_else(|| platform_newline());
if !self
.body
.as_ref()
.iter()
.take_while(|e| !matches!(e, Event::SectionKey(_)))
.any(|e| e.to_bstr_lossy().contains_str(nl))
{
out.write_all(nl)?;
}
let mut saw_newline_after_value = true;
let mut in_key_value_pair = false;
for (idx, event) in self.body.as_ref().iter().enumerate() {
match event {
Event::SectionKey(_) => {
if !saw_newline_after_value {
out.write_all(nl)?;
}
saw_newline_after_value = false;
in_key_value_pair = true;
}
Event::Newline(_) if !in_key_value_pair => {
saw_newline_after_value = true;
}
Event::Value(_) | Event::ValueDone(_) => {
in_key_value_pair = false;
}
_ => {}
}
event.write_to(&mut out)?;
if let Event::ValueNotDone(_) = event {
if self
.body
.0
.get(idx + 1)
.filter(|e| matches!(e, Event::Newline(_)))
.is_none()
{
out.write_all(nl)?;
}
}
}
Ok(())
}
sourcepub fn write_to(&self, out: impl Write) -> Result<()>
pub fn write_to(&self, out: impl Write) -> Result<()>
Stream ourselves to the given out
, in order to reproduce this event mostly losslessly
as it was parsed.
Examples found in repository?
More examples
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
pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> {
let nl = self.detect_newline_style();
{
for event in self.frontmatter_events.as_ref() {
event.write_to(&mut out)?;
}
if !ends_with_newline(self.frontmatter_events.as_ref(), nl, true) && self.sections.iter().next().is_some() {
out.write_all(nl)?;
}
}
let mut prev_section_ended_with_newline = true;
for section_id in &self.section_order {
if !prev_section_ended_with_newline {
out.write_all(nl)?;
}
let section = self.sections.get(section_id).expect("known section-id");
section.write_to(&mut out)?;
prev_section_ended_with_newline = ends_with_newline(section.body.0.as_ref(), nl, false);
if let Some(post_matter) = self.frontmatter_post_section.get(section_id) {
if !prev_section_ended_with_newline {
out.write_all(nl)?;
}
for event in post_matter {
event.write_to(&mut out)?;
}
prev_section_ended_with_newline = ends_with_newline(post_matter, nl, prev_section_ended_with_newline);
}
}
if !prev_section_ended_with_newline {
out.write_all(nl)?;
}
Ok(())
}
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
pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> {
self.header.write_to(&mut out)?;
if self.body.0.is_empty() {
return Ok(());
}
let nl = self
.body
.as_ref()
.iter()
.find_map(extract_newline)
.unwrap_or_else(|| platform_newline());
if !self
.body
.as_ref()
.iter()
.take_while(|e| !matches!(e, Event::SectionKey(_)))
.any(|e| e.to_bstr_lossy().contains_str(nl))
{
out.write_all(nl)?;
}
let mut saw_newline_after_value = true;
let mut in_key_value_pair = false;
for (idx, event) in self.body.as_ref().iter().enumerate() {
match event {
Event::SectionKey(_) => {
if !saw_newline_after_value {
out.write_all(nl)?;
}
saw_newline_after_value = false;
in_key_value_pair = true;
}
Event::Newline(_) if !in_key_value_pair => {
saw_newline_after_value = true;
}
Event::Value(_) | Event::ValueDone(_) => {
in_key_value_pair = false;
}
_ => {}
}
event.write_to(&mut out)?;
if let Event::ValueNotDone(_) = event {
if self
.body
.0
.get(idx + 1)
.filter(|e| matches!(e, Event::Newline(_)))
.is_none()
{
out.write_all(nl)?;
}
}
}
Ok(())
}
Trait Implementations§
source§impl<'a> Ord for Event<'a>
impl<'a> Ord for Event<'a>
source§impl<'a> PartialEq<Event<'a>> for Event<'a>
impl<'a> PartialEq<Event<'a>> for Event<'a>
source§impl<'a> PartialOrd<Event<'a>> for Event<'a>
impl<'a> PartialOrd<Event<'a>> for Event<'a>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more