Struct rustfmt_nightly::config::license::TemplateParser
source · [−]pub(crate) struct TemplateParser {
parsed: String,
buffer: String,
state: ParsingState,
linum: u32,
open_brace_line: u32,
}Fields
parsed: Stringbuffer: Stringstate: ParsingStatelinum: u32open_brace_line: u32Implementations
sourceimpl TemplateParser
impl TemplateParser
fn new() -> Self
sourcepub(crate) fn parse(template: &str) -> Result<String, LicenseError>
pub(crate) fn parse(template: &str) -> Result<String, LicenseError>
Converts a license template into a string which can be turned into a regex.
The license template could use regex syntax directly, but that would require a lot of manual
escaping, which is inconvenient. It is therefore literal by default, with optional regex
subparts delimited by { and }. Additionally:
- to insert literal
{,}or\, escape it with\ - an empty regex placeholder (
{}) is shorthand for{.*?}
This function parses this input format and builds a properly escaped string representation of the equivalent regular expression. It does not however guarantee that the returned string is a syntactically valid regular expression.
Examples
assert_eq!(
TemplateParser::parse(
r"
// Copyright {\d+} The \} Rust \\ Project \{ Developers. See the {([A-Z]+)}
// file at the top-level directory of this distribution and at
// {}.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
"
).unwrap(),
r"^
// Copyright \d+ The \} Rust \\ Project \{ Developers\. See the ([A-Z]+)
// file at the top\-level directory of this distribution and at
// .*?\.
//
// Licensed under the Apache License, Version 2\.0 <LICENSE\-APACHE or
// http://www\.apache\.org/licenses/LICENSE\-2\.0> or the MIT license
// <LICENSE\-MIT or http://opensource\.org/licenses/MIT>, at your
// option\. This file may not be copied, modified, or distributed
// except according to those terms\.
"
);fn trans_from_lit(&mut self, chr: char) -> ParsingState
fn trans_from_litesc(&mut self, chr: char) -> ParsingState
fn trans_from_re(&mut self, chr: char, brace_nesting: u32) -> ParsingState
fn trans_from_reesc(&mut self, chr: char, brace_nesting: u32) -> ParsingState
Auto Trait Implementations
impl RefUnwindSafe for TemplateParser
impl Send for TemplateParser
impl Sync for TemplateParser
impl Unpin for TemplateParser
impl UnwindSafe for TemplateParser
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Layout
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.
Size: 88 bytes