MCPcopy Index your code
hub / github.com/cloud-hypervisor/cloud-hypervisor / parse_format

Function parse_format

cloud-hypervisor/src/logger.rs:54–98  ·  view source on GitHub ↗
(fmt: &str)

Source from the content-addressed store, hash-verified

52}
53
54fn parse_format(fmt: &str) -> Result<Vec<Token>, Error> {
55 let mut tokens = Vec::new();
56 let mut literal = String::new();
57 let mut chars = fmt.chars().peekable();
58
59 while let Some(c) = chars.next() {
60 match c {
61 '{' => {
62 if chars.peek() == Some(&'{') {
63 chars.next();
64 literal.push('{');
65 continue;
66 }
67
68 if !literal.is_empty() {
69 tokens.push(Token::Literal(std::mem::take(&mut literal)));
70 }
71
72 let mut name = String::new();
73 loop {
74 match chars.next() {
75 Some('}') => break,
76 Some(ch) => name.push(ch),
77 None => return Err(Error::UnterminatedBrace),
78 }
79 }
80
81 tokens.push(name.parse()?);
82 }
83 '}' => {
84 if chars.peek() == Some(&'}') {
85 chars.next();
86 literal.push('}');
87 } else {
88 return Err(Error::UnmatchedBrace);
89 }
90 }
91 _ => literal.push(c),
92 }
93 }
94 if !literal.is_empty() {
95 tokens.push(Token::Literal(literal));
96 }
97 Ok(tokens)
98}
99
100pub const DEFAULT_FORMAT: &str =
101 "cloud-hypervisor: {boottime}s: <{thread}> {level}:{location} -- {msg}";

Callers 10

newMethod · 0.85
parse_plain_literalFunction · 0.85
parse_empty_stringFunction · 0.85
parse_all_known_tokensFunction · 0.85
parse_escaped_bracesFunction · 0.85

Calls 6

newFunction · 0.85
peekMethod · 0.80
nextMethod · 0.45
pushMethod · 0.45
is_emptyMethod · 0.45
parseMethod · 0.45

Tested by 9

parse_plain_literalFunction · 0.68
parse_empty_stringFunction · 0.68
parse_all_known_tokensFunction · 0.68
parse_escaped_bracesFunction · 0.68