Hi all,
I am trying to write a simple Nom parser to read through a file from disk.
To this end I've set up a public function to be used by the rest of the application, and an inner function to do the parsing. However, as the header suggests, I'm running into a lifetime error.
This is the code I'm running:
pub fn parse_template<P: AsRef<std::path::Path> + std::fmt::Debug + ?Sized>(filepath: &P) -> Result<(Setup, File)> {
let mut buffer: String = String::new();
std::fs::File::open(filepath)
.with_context(|| format!("Failed whilst parsing, could not read file: {filepath:?}"))?
.read_to_string(&mut buffer)?;
parse_setup(&buffer)?;
return Ok((Setup::default(), File::default()));
}
fn parse_setup(i: &str) -> nom::IResult<&str, &str> {
let (i, _) = tag("/*")(i)?;
let (i, _) = multispace0(i)?;
let (i, _) = tag("setup:")(i)?;
todo!();
}
With File
and Setup
being simple strings with a few Strings in them
This is the compiler diagnostic I'm getting:
error[E0597]: `buffer` does not live long enough
--> src\parse.rs:14:17
|
8 | let mut buffer: String = String::new();
| ---------- binding `buffer` declared here
...
14 | parse_setup(&buffer)?;
| ------------^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `buffer` is borrowed for `'static`
...
17 | }
| - `buffer` dropped here while still borrowed
Considering I'm reading the file in right there, I'm not sure how to make the buffer live long enough to be parsed.
I have tried cloning the string, calling .to_owned()
, and calling .as_str()
, but none seem to have the desired effect.