Browse Source

Why allocate when we can reference?

master
Julio Biason 1 year ago
parent
commit
705154e5f8
  1. 38
      nomfoamtest/src/foam.rs

38
nomfoamtest/src/foam.rs

@ -8,33 +8,43 @@ use nom::bytes::complete::take_until;
use nom::IResult; use nom::IResult;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct Comment { struct Comment<'a> {
comment: String, content: &'a str,
} }
impl FromStr for Comment { // impl FromStr for Comment {
type Err = (); // type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> { // fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self { // Ok(Self {
comment: s.trim().to_string(), // comment: s.trim().to_string(),
}) // })
} // }
} // }
fn multiline_comment(input: &str) -> IResult<&str, Comment> { fn multiline_comment(input: &str) -> IResult<&str, Comment> {
let (input, _) = tag("/*")(input)?; let (input, _) = tag("/*")(input)?;
let (input, content) = take_until("*/")(input)?; let (input, content) = take_until("*/")(input)?;
let (input, _) = tag("*/")(input)?; let (input, _) = tag("*/")(input)?;
Ok((input, Comment::from_str(content).unwrap())) Ok((
input,
Comment {
content: content.trim(),
},
))
} }
fn singleline_comment(input: &str) -> IResult<&str, Comment> { fn singleline_comment(input: &str) -> IResult<&str, Comment> {
let (input, _) = tag("//")(input)?; let (input, _) = tag("//")(input)?;
let (input, content) = is_not("\n\r")(input)?; let (input, content) = is_not("\n\r")(input)?;
Ok((input, Comment::from_str(content).unwrap())) Ok((
input,
Comment {
content: content.trim(),
},
))
} }
#[cfg(test)] #[cfg(test)]
@ -50,7 +60,7 @@ mod tests {
Ok(( Ok((
"", "",
Comment { Comment {
comment: "this is comment".to_string() content: "this is comment"
} }
)) ))
) )
@ -65,7 +75,7 @@ mod tests {
Ok(( Ok((
"", "",
Comment { Comment {
comment: "this is comment".to_string() content: "this is comment"
} }
)) ))
) )

Loading…
Cancel
Save