mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
ParsedPath::try_from<&str> (#15180)
# Objective - implements ParsedPath::try_from<&str> - resolves #14438 ## Testing - Added unit test for ParsedPath::try_from<&str> Note: I don't claim to be an expert on lifetimes! That said I think it makes sense that the error shares a lifetime with input string as deeper down it is used to construct it.
This commit is contained in:
parent
37443e0f3f
commit
cf55e6cb22
1 changed files with 22 additions and 0 deletions
|
@ -478,6 +478,13 @@ impl<const N: usize> From<[Access<'static>; N]> for ParsedPath {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a str> for ParsedPath {
|
||||
type Error = ReflectPathError<'a>;
|
||||
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
|
||||
ParsedPath::parse(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ParsedPath {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for OffsetAccess { access, .. } in &self.0 {
|
||||
|
@ -585,6 +592,21 @@ mod tests {
|
|||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_from() {
|
||||
assert_eq!(
|
||||
ParsedPath::try_from("w").unwrap().0,
|
||||
&[offset(access_field("w"), 1)]
|
||||
);
|
||||
|
||||
let r = ParsedPath::try_from("w[");
|
||||
let matches = matches!(r, Err(ReflectPathError::ParseError { .. }));
|
||||
assert!(
|
||||
matches,
|
||||
"ParsedPath::try_from did not return a ParseError for \"w[\""
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parsed_path_parse() {
|
||||
assert_eq!(
|
||||
|
|
Loading…
Reference in a new issue