Struct git_config::parse::section::Header
source · pub struct Header<'a> { /* private fields */ }
Expand description
A parsed section header, containing a name and optionally a subsection name.
Implementations§
source§impl<'a> Header<'a>
impl<'a> Header<'a>
sourcepub fn new(
name: impl Into<Cow<'a, str>>,
subsection: impl Into<Option<Cow<'a, BStr>>>
) -> Result<Header<'a>, Error>
pub fn new(
name: impl Into<Cow<'a, str>>,
subsection: impl Into<Option<Cow<'a, BStr>>>
) -> Result<Header<'a>, Error>
Instantiate a new header either with a section name
, e.g. “core” serializing to ["core"]
or [remote "origin"]
for subsection
being “origin” and name
being “remote”.
Examples found in repository?
33 34 35 36 37 38 39 40 41 42 43 44
pub fn new(
name: impl Into<Cow<'a, str>>,
subsection: impl Into<Option<Cow<'a, BStr>>>,
meta: impl Into<OwnShared<file::Metadata>>,
) -> Result<Self, parse::section::header::Error> {
Ok(Section {
header: parse::section::Header::new(name, subsection)?,
body: Default::default(),
meta: meta.into(),
id: SectionId::default(),
})
}
More examples
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
pub fn rename_section<'a>(
&mut self,
name: impl AsRef<str>,
subsection_name: impl Into<Option<&'a BStr>>,
new_name: impl Into<Cow<'event, str>>,
new_subsection_name: impl Into<Option<Cow<'event, BStr>>>,
) -> Result<(), rename_section::Error> {
let id = self
.section_ids_by_name_and_subname(name.as_ref(), subsection_name.into())?
.rev()
.next()
.expect("list of sections were empty, which violates invariant");
let section = self.sections.get_mut(&id).expect("known section-id");
section.header = section::Header::new(new_name, new_subsection_name)?;
Ok(())
}
/// Renames the section with `name` and `subsection_name`, modifying the last matching section
/// that also passes `filter` to use `new_name` and `new_subsection_name`.
///
/// Note that the otherwise unused [`lookup::existing::Error::KeyMissing`] variant is used to indicate
/// that the `filter` rejected all candidates, leading to no section being renamed after all.
pub fn rename_section_filter<'a>(
&mut self,
name: impl AsRef<str>,
subsection_name: impl Into<Option<&'a BStr>>,
new_name: impl Into<Cow<'event, str>>,
new_subsection_name: impl Into<Option<Cow<'event, BStr>>>,
filter: &mut MetadataFilter,
) -> Result<(), rename_section::Error> {
let id = self
.section_ids_by_name_and_subname(name.as_ref(), subsection_name.into())?
.rev()
.find(|id| filter(self.sections.get(id).expect("each id has a section").meta()))
.ok_or(rename_section::Error::Lookup(lookup::existing::Error::KeyMissing))?;
let section = self.sections.get_mut(&id).expect("known section-id");
section.header = section::Header::new(new_name, new_subsection_name)?;
Ok(())
}
source§impl Header<'_>
impl Header<'_>
sourcepub fn is_legacy(&self) -> bool
pub fn is_legacy(&self) -> bool
Return true if this is a header like [legacy.subsection]
, or false otherwise.
sourcepub fn subsection_name(&self) -> Option<&BStr>
pub fn subsection_name(&self) -> Option<&BStr>
Return the subsection name, if present, i.e. “origin” in [remote "origin"]
.
It is parsed without quotes, and with escapes folded
into their resulting characters.
Thus during serialization, escapes and quotes must be re-added.
This makes it possible to use Event
data for lookups directly.
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?
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.to_bstring(), f)
}
}
impl From<Header<'_>> for BString {
fn from(header: Header<'_>) -> Self {
header.into()
}
}
impl From<&Header<'_>> for BString {
fn from(header: &Header<'_>) -> Self {
header.to_bstring()
}
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 header mostly losslessly
as it was parsed.
Examples found in repository?
More examples
36 37 38 39 40 41 42 43 44 45 46 47 48
pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> {
match self {
Self::ValueNotDone(e) => {
out.write_all(e.as_ref())?;
out.write_all(b"\\")
}
Self::Whitespace(e) | Self::Newline(e) | Self::Value(e) | Self::ValueDone(e) => out.write_all(e.as_ref()),
Self::KeyValueSeparator => out.write_all(b"="),
Self::SectionKey(k) => out.write_all(k.0.as_ref()),
Self::SectionHeader(h) => h.write_to(&mut out),
Self::Comment(c) => c.write_to(&mut out),
}
}
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 to_owned(&self) -> Header<'static>
pub fn to_owned(&self) -> Header<'static>
Turn this instance into a fully owned one with 'static
lifetime.
Examples found in repository?
More examples
52 53 54 55 56 57 58 59 60 61 62 63 64
pub fn to_owned(&self) -> Event<'static> {
match self {
Event::Comment(e) => Event::Comment(e.to_owned()),
Event::SectionHeader(e) => Event::SectionHeader(e.to_owned()),
Event::SectionKey(e) => Event::SectionKey(e.to_owned()),
Event::Value(e) => Event::Value(Cow::Owned(e.clone().into_owned())),
Event::ValueNotDone(e) => Event::ValueNotDone(Cow::Owned(e.clone().into_owned())),
Event::ValueDone(e) => Event::ValueDone(Cow::Owned(e.clone().into_owned())),
Event::Newline(e) => Event::Newline(Cow::Owned(e.clone().into_owned())),
Event::Whitespace(e) => Event::Whitespace(Cow::Owned(e.clone().into_owned())),
Event::KeyValueSeparator => Event::KeyValueSeparator,
}
}
Trait Implementations§
source§impl<'a> Ord for Header<'a>
impl<'a> Ord for Header<'a>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a> PartialEq<Header<'a>> for Header<'a>
impl<'a> PartialEq<Header<'a>> for Header<'a>
source§impl<'a> PartialOrd<Header<'a>> for Header<'a>
impl<'a> PartialOrd<Header<'a>> for Header<'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