1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
use std::fmt;
use std::num::ParseIntError;
use crate::{Reflect, ReflectMut, ReflectRef, VariantType};
use thiserror::Error;
/// An error returned from a failed path string query.
#[derive(Debug, PartialEq, Eq, Error)]
pub enum ReflectPathError<'a> {
#[error("expected an identifier at index {index}")]
ExpectedIdent { index: usize },
#[error("the current struct doesn't have a field with the name `{field}`")]
InvalidField { index: usize, field: &'a str },
#[error("the current struct doesn't have a field at the given index")]
InvalidFieldIndex { index: usize, field_index: usize },
#[error("the current tuple struct doesn't have a field with the index {tuple_struct_index}")]
InvalidTupleStructIndex {
index: usize,
tuple_struct_index: usize,
},
#[error("the current tuple doesn't have a field with the index {tuple_index}")]
InvalidTupleIndex { index: usize, tuple_index: usize },
#[error("the current struct variant doesn't have a field with the name `{field}`")]
InvalidStructVariantField { index: usize, field: &'a str },
#[error("the current tuple variant doesn't have a field with the index {tuple_variant_index}")]
InvalidTupleVariantIndex {
index: usize,
tuple_variant_index: usize,
},
#[error("the current list doesn't have a value at the index {list_index}")]
InvalidListIndex { index: usize, list_index: usize },
#[error("encountered an unexpected token `{token}`")]
UnexpectedToken { index: usize, token: &'a str },
#[error("expected token `{token}`, but it wasn't there.")]
ExpectedToken { index: usize, token: &'a str },
#[error("expected a struct, but found a different reflect value")]
ExpectedStruct { index: usize },
#[error("expected a list, but found a different reflect value")]
ExpectedList { index: usize },
#[error("expected a struct variant, but found a different reflect value")]
ExpectedStructVariant { index: usize },
#[error("expected a tuple variant, but found a different reflect value")]
ExpectedTupleVariant { index: usize },
#[error("failed to parse a usize")]
IndexParseError(#[from] ParseIntError),
#[error("failed to downcast to the path result to the given type")]
InvalidDowncast,
}
/// A trait which allows nested [`Reflect`] values to be retrieved with path strings.
///
/// Using these functions repeatedly with the same string requires parsing the string every time.
/// To avoid this cost, it's recommended to construct a [`ParsedPath`] instead.
///
/// # Syntax
///
/// ## Structs
///
/// Field paths for [`Struct`] elements use the standard Rust field access syntax of
/// dot and field name: `.field_name`.
///
/// Additionally, struct fields may be accessed by their index within the struct's definition.
/// This is accomplished by using the hash symbol (`#`) in place of the standard dot: `#0`.
///
/// Accessing a struct's field by index can speed up fetches at runtime due to the removed
/// need for string matching.
/// And while this can be more performant, it's best to keep in mind the tradeoffs when
/// utilizing such optimizations.
/// For example, this can result in fairly fragile code as the string paths will need to be
/// kept in sync with the struct definitions since the order of fields could be easily changed.
/// Because of this, storing these kinds of paths in persistent storage (i.e. game assets)
/// is strongly discouraged.
///
/// Note that a leading dot (`.`) or hash (`#`) token is implied for the first item in a path,
/// and may therefore be omitted.
///
/// ### Example
/// ```
/// # use bevy_reflect::{GetPath, Reflect};
/// #[derive(Reflect)]
/// struct MyStruct {
/// value: u32
/// }
///
/// let my_struct = MyStruct { value: 123 };
/// // Access via field name
/// assert_eq!(my_struct.path::<u32>(".value").unwrap(), &123);
/// // Access via field index
/// assert_eq!(my_struct.path::<u32>("#0").unwrap(), &123);
/// ```
///
/// ## Tuples and Tuple Structs
///
/// [`Tuple`] and [`TupleStruct`] elements also follow a conventional Rust syntax.
/// Fields are accessed with a dot and the field index: `.0`.
///
/// Note that a leading dot (`.`) token is implied for the first item in a path,
/// and may therefore be omitted.
///
/// ### Example
/// ```
/// # use bevy_reflect::{GetPath, Reflect};
/// #[derive(Reflect)]
/// struct MyTupleStruct(u32);
///
/// let my_tuple_struct = MyTupleStruct(123);
/// assert_eq!(my_tuple_struct.path::<u32>(".0").unwrap(), &123);
/// ```
///
/// ## Lists and Arrays
///
/// [`List`] and [`Array`] elements are accessed with brackets: `[0]`.
///
/// ### Example
/// ```
/// # use bevy_reflect::{GetPath};
/// let my_list: Vec<u32> = vec![1, 2, 3];
/// assert_eq!(my_list.path::<u32>("[2]").unwrap(), &3);
/// ```
///
/// ## Enums
///
/// Pathing for [`Enum`] elements works a bit differently than in normal Rust.
/// Usually, you would need to pattern match an enum, branching off on the desired variants.
/// Paths used by this trait do not have any pattern matching capabilities;
/// instead, they assume the variant is already known ahead of time.
///
/// The syntax used, therefore, depends on the variant being accessed:
/// - Struct variants use the struct syntax (outlined above)
/// - Tuple variants use the tuple syntax (outlined above)
/// - Unit variants have no fields to access
///
/// If the variant cannot be known ahead of time, the path will need to be split up
/// and proper enum pattern matching will need to be handled manually.
///
/// ### Example
/// ```
/// # use bevy_reflect::{GetPath, Reflect};
/// #[derive(Reflect)]
/// enum MyEnum {
/// Unit,
/// Tuple(bool),
/// Struct {
/// value: u32
/// }
/// }
///
/// let tuple_variant = MyEnum::Tuple(true);
/// assert_eq!(tuple_variant.path::<bool>(".0").unwrap(), &true);
///
/// let struct_variant = MyEnum::Struct { value: 123 };
/// // Access via field name
/// assert_eq!(struct_variant.path::<u32>(".value").unwrap(), &123);
/// // Access via field index
/// assert_eq!(struct_variant.path::<u32>("#0").unwrap(), &123);
///
/// // Error: Expected struct variant
/// assert!(matches!(tuple_variant.path::<u32>(".value"), Err(_)));
/// ```
///
/// # Chaining
///
/// Using the aforementioned syntax, path items may be chained one after another
/// to create a full path to a nested element.
///
/// ## Example
/// ```
/// # use bevy_reflect::{GetPath, Reflect};
/// #[derive(Reflect)]
/// struct MyStruct {
/// value: Vec<Option<u32>>
/// }
///
/// let my_struct = MyStruct {
/// value: vec![None, None, Some(123)],
/// };
/// assert_eq!(
/// my_struct.path::<u32>(".value[2].0").unwrap(),
/// &123,
/// );
/// ```
///
/// [`Struct`]: crate::Struct
/// [`Tuple`]: crate::Tuple
/// [`TupleStruct`]: crate::TupleStruct
/// [`List`]: crate::List
/// [`Array`]: crate::Array
/// [`Enum`]: crate::Enum
pub trait GetPath {
/// Returns a reference to the value specified by `path`.
///
/// To retrieve a statically typed reference, use
/// [`path`][GetPath::path].
fn reflect_path<'r, 'p>(
&'r self,
path: &'p str,
) -> Result<&'r dyn Reflect, ReflectPathError<'p>>;
/// Returns a mutable reference to the value specified by `path`.
///
/// To retrieve a statically typed mutable reference, use
/// [`path_mut`][GetPath::path_mut].
fn reflect_path_mut<'r, 'p>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut dyn Reflect, ReflectPathError<'p>>;
/// Returns a statically typed reference to the value specified by `path`.
///
/// This will automatically handle downcasting to type `T`.
/// The downcast will fail if this value is not of type `T`
/// (which may be the case when using dynamic types like [`DynamicStruct`]).
///
/// [`DynamicStruct`]: crate::DynamicStruct
fn path<'r, 'p, T: Reflect>(&'r self, path: &'p str) -> Result<&'r T, ReflectPathError<'p>> {
self.reflect_path(path).and_then(|p| {
p.downcast_ref::<T>()
.ok_or(ReflectPathError::InvalidDowncast)
})
}
/// Returns a statically typed mutable reference to the value specified by `path`.
///
/// This will automatically handle downcasting to type `T`.
/// The downcast will fail if this value is not of type `T`
/// (which may be the case when using dynamic types like [`DynamicStruct`]).
///
/// [`DynamicStruct`]: crate::DynamicStruct
fn path_mut<'r, 'p, T: Reflect>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut T, ReflectPathError<'p>> {
self.reflect_path_mut(path).and_then(|p| {
p.downcast_mut::<T>()
.ok_or(ReflectPathError::InvalidDowncast)
})
}
}
impl<T: Reflect> GetPath for T {
fn reflect_path<'r, 'p>(
&'r self,
path: &'p str,
) -> Result<&'r dyn Reflect, ReflectPathError<'p>> {
(self as &dyn Reflect).reflect_path(path)
}
fn reflect_path_mut<'r, 'p>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut dyn Reflect, ReflectPathError<'p>> {
(self as &mut dyn Reflect).reflect_path_mut(path)
}
}
impl GetPath for dyn Reflect {
fn reflect_path<'r, 'p>(
&'r self,
path: &'p str,
) -> Result<&'r dyn Reflect, ReflectPathError<'p>> {
let mut current: &dyn Reflect = self;
for (access, current_index) in PathParser::new(path) {
current = access?.read_element(current, current_index)?;
}
Ok(current)
}
fn reflect_path_mut<'r, 'p>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut dyn Reflect, ReflectPathError<'p>> {
let mut current: &mut dyn Reflect = self;
for (access, current_index) in PathParser::new(path) {
current = access?.read_element_mut(current, current_index)?;
}
Ok(current)
}
}
/// A pre-parsed path to an element within a type.
///
/// This struct may be used like [`GetPath`] but removes the cost of parsing the path
/// string at each element access.
///
/// It's recommended to use this in place of `GetPath` when the path string is
/// unlikely to be changed and will be accessed repeatedly.
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub struct ParsedPath(
/// This is the boxed slice of pre-parsed accesses.
///
/// Each item in the slice contains the access along with the character
/// index of the start of the access within the parsed path string.
///
/// The index is mainly used for more helpful error reporting.
Box<[(Access, usize)]>,
);
impl ParsedPath {
/// Parses a [`ParsedPath`] from a string.
///
/// Returns an error if the string does not represent a valid path to an element.
///
/// The exact format for path strings can be found in the documentation for [`GetPath`].
/// In short, though, a path consists of one or more chained accessor strings.
/// These are:
/// - Named field access (`.field`)
/// - Unnamed field access (`.1`)
/// - Field index access (`#0`)
/// - Sequence access (`[2]`)
///
/// # Example
/// ```
/// # use bevy_reflect::{ParsedPath, Reflect};
/// #[derive(Reflect)]
/// struct Foo {
/// bar: Bar,
/// }
///
/// #[derive(Reflect)]
/// struct Bar {
/// baz: Baz,
/// }
///
/// #[derive(Reflect)]
/// struct Baz(f32, Vec<Option<u32>>);
///
/// let foo = Foo {
/// bar: Bar {
/// baz: Baz(3.14, vec![None, None, Some(123)])
/// },
/// };
///
/// let parsed_path = ParsedPath::parse("bar#0.1[2].0").unwrap();
/// // Breakdown:
/// // "bar" - Access struct field named "bar"
/// // "#0" - Access struct field at index 0
/// // ".1" - Access tuple struct field at index 1
/// // "[2]" - Access list element at index 2
/// // ".0" - Access tuple variant field at index 0
///
/// assert_eq!(parsed_path.element::<u32>(&foo).unwrap(), &123);
/// ```
///
pub fn parse(string: &str) -> Result<Self, ReflectPathError<'_>> {
let mut parts = Vec::new();
for (access, idx) in PathParser::new(string) {
parts.push((access?.to_owned(), idx));
}
Ok(Self(parts.into_boxed_slice()))
}
/// Gets a read-only reference to the specified element on the given [`Reflect`] object.
///
/// Returns an error if the path is invalid for the provided type.
///
/// See [`element_mut`](Self::reflect_element_mut) for a typed version of this method.
pub fn reflect_element<'r, 'p>(
&'p self,
root: &'r dyn Reflect,
) -> Result<&'r dyn Reflect, ReflectPathError<'p>> {
let mut current = root;
for (access, current_index) in self.0.iter() {
current = access.to_ref().read_element(current, *current_index)?;
}
Ok(current)
}
/// Gets a mutable reference to the specified element on the given [`Reflect`] object.
///
/// Returns an error if the path is invalid for the provided type.
///
/// See [`element_mut`](Self::element_mut) for a typed version of this method.
pub fn reflect_element_mut<'r, 'p>(
&'p self,
root: &'r mut dyn Reflect,
) -> Result<&'r mut dyn Reflect, ReflectPathError<'p>> {
let mut current = root;
for (access, current_index) in self.0.iter() {
current = access.to_ref().read_element_mut(current, *current_index)?;
}
Ok(current)
}
/// Gets a typed, read-only reference to the specified element on the given [`Reflect`] object.
///
/// Returns an error if the path is invalid for the provided type.
///
/// See [`reflect_element`](Self::reflect_element) for an untyped version of this method.
pub fn element<'r, 'p, T: Reflect>(
&'p self,
root: &'r dyn Reflect,
) -> Result<&'r T, ReflectPathError<'p>> {
self.reflect_element(root).and_then(|p| {
p.downcast_ref::<T>()
.ok_or(ReflectPathError::InvalidDowncast)
})
}
/// Gets a typed, read-only reference to the specified element on the given [`Reflect`] object.
///
/// Returns an error if the path is invalid for the provided type.
///
/// See [`reflect_element_mut`](Self::reflect_element_mut) for an untyped version of this method.
pub fn element_mut<'r, 'p, T: Reflect>(
&'p self,
root: &'r mut dyn Reflect,
) -> Result<&'r mut T, ReflectPathError<'p>> {
self.reflect_element_mut(root).and_then(|p| {
p.downcast_mut::<T>()
.ok_or(ReflectPathError::InvalidDowncast)
})
}
}
impl fmt::Display for ParsedPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (idx, (access, _)) in self.0.iter().enumerate() {
match access {
Access::Field(field) => {
if idx != 0 {
Token::DOT.fmt(f)?;
}
f.write_str(field.as_str())?;
}
Access::FieldIndex(index) => {
Token::CROSSHATCH.fmt(f)?;
index.fmt(f)?;
}
Access::TupleIndex(index) => {
if idx != 0 {
Token::DOT.fmt(f)?;
}
index.fmt(f)?;
}
Access::ListIndex(index) => {
Token::OPEN_BRACKET.fmt(f)?;
index.fmt(f)?;
Token::CLOSE_BRACKET.fmt(f)?;
}
}
}
Ok(())
}
}
/// A singular owned element access within a path.
///
/// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
///
/// A path is composed of multiple accesses in sequence.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Access {
Field(String),
FieldIndex(usize),
TupleIndex(usize),
ListIndex(usize),
}
impl Access {
fn to_ref(&self) -> AccessRef<'_> {
match self {
Self::Field(value) => AccessRef::Field(value),
Self::FieldIndex(value) => AccessRef::FieldIndex(*value),
Self::TupleIndex(value) => AccessRef::TupleIndex(*value),
Self::ListIndex(value) => AccessRef::ListIndex(*value),
}
}
}
/// A singular borrowed element access within a path.
///
/// Can be applied to a `dyn Reflect` to get a reference to the targeted element.
///
/// Does not own the backing store it's sourced from.
/// For an owned version, you can convert one to an [`Access`].
#[derive(Debug)]
enum AccessRef<'a> {
Field(&'a str),
FieldIndex(usize),
TupleIndex(usize),
ListIndex(usize),
}
impl<'a> AccessRef<'a> {
fn to_owned(&self) -> Access {
match self {
Self::Field(value) => Access::Field(value.to_string()),
Self::FieldIndex(value) => Access::FieldIndex(*value),
Self::TupleIndex(value) => Access::TupleIndex(*value),
Self::ListIndex(value) => Access::ListIndex(*value),
}
}
fn read_element<'r>(
&self,
current: &'r dyn Reflect,
current_index: usize,
) -> Result<&'r dyn Reflect, ReflectPathError<'a>> {
match (self, current.reflect_ref()) {
(Self::Field(field), ReflectRef::Struct(reflect_struct)) => reflect_struct
.field(field)
.ok_or(ReflectPathError::InvalidField {
index: current_index,
field,
}),
(Self::FieldIndex(field_index), ReflectRef::Struct(reflect_struct)) => reflect_struct
.field_at(*field_index)
.ok_or(ReflectPathError::InvalidFieldIndex {
index: current_index,
field_index: *field_index,
}),
(Self::TupleIndex(tuple_index), ReflectRef::TupleStruct(reflect_struct)) => {
reflect_struct.field(*tuple_index).ok_or(
ReflectPathError::InvalidTupleStructIndex {
index: current_index,
tuple_struct_index: *tuple_index,
},
)
}
(Self::TupleIndex(tuple_index), ReflectRef::Tuple(reflect_tuple)) => reflect_tuple
.field(*tuple_index)
.ok_or(ReflectPathError::InvalidTupleIndex {
index: current_index,
tuple_index: *tuple_index,
}),
(Self::ListIndex(list_index), ReflectRef::List(reflect_list)) => reflect_list
.get(*list_index)
.ok_or(ReflectPathError::InvalidListIndex {
index: current_index,
list_index: *list_index,
}),
(Self::ListIndex(list_index), ReflectRef::Array(reflect_list)) => reflect_list
.get(*list_index)
.ok_or(ReflectPathError::InvalidListIndex {
index: current_index,
list_index: *list_index,
}),
(Self::ListIndex(_), _) => Err(ReflectPathError::ExpectedList {
index: current_index,
}),
(Self::Field(field), ReflectRef::Enum(reflect_enum)) => {
match reflect_enum.variant_type() {
VariantType::Struct => {
reflect_enum
.field(field)
.ok_or(ReflectPathError::InvalidField {
index: current_index,
field,
})
}
_ => Err(ReflectPathError::ExpectedStructVariant {
index: current_index,
}),
}
}
(Self::FieldIndex(field_index), ReflectRef::Enum(reflect_enum)) => {
match reflect_enum.variant_type() {
VariantType::Struct => reflect_enum.field_at(*field_index).ok_or(
ReflectPathError::InvalidFieldIndex {
index: current_index,
field_index: *field_index,
},
),
_ => Err(ReflectPathError::ExpectedStructVariant {
index: current_index,
}),
}
}
(Self::TupleIndex(tuple_variant_index), ReflectRef::Enum(reflect_enum)) => {
match reflect_enum.variant_type() {
VariantType::Tuple => reflect_enum.field_at(*tuple_variant_index).ok_or(
ReflectPathError::InvalidTupleVariantIndex {
index: current_index,
tuple_variant_index: *tuple_variant_index,
},
),
_ => Err(ReflectPathError::ExpectedTupleVariant {
index: current_index,
}),
}
}
_ => Err(ReflectPathError::ExpectedStruct {
index: current_index,
}),
}
}
fn read_element_mut<'r>(
&self,
current: &'r mut dyn Reflect,
current_index: usize,
) -> Result<&'r mut dyn Reflect, ReflectPathError<'a>> {
match (self, current.reflect_mut()) {
(Self::Field(field), ReflectMut::Struct(reflect_struct)) => reflect_struct
.field_mut(field)
.ok_or(ReflectPathError::InvalidField {
index: current_index,
field,
}),
(Self::FieldIndex(field_index), ReflectMut::Struct(reflect_struct)) => reflect_struct
.field_at_mut(*field_index)
.ok_or(ReflectPathError::InvalidFieldIndex {
index: current_index,
field_index: *field_index,
}),
(Self::TupleIndex(tuple_index), ReflectMut::TupleStruct(reflect_struct)) => {
reflect_struct.field_mut(*tuple_index).ok_or(
ReflectPathError::InvalidTupleStructIndex {
index: current_index,
tuple_struct_index: *tuple_index,
},
)
}
(Self::TupleIndex(tuple_index), ReflectMut::Tuple(reflect_tuple)) => reflect_tuple
.field_mut(*tuple_index)
.ok_or(ReflectPathError::InvalidTupleIndex {
index: current_index,
tuple_index: *tuple_index,
}),
(Self::ListIndex(list_index), ReflectMut::List(reflect_list)) => reflect_list
.get_mut(*list_index)
.ok_or(ReflectPathError::InvalidListIndex {
index: current_index,
list_index: *list_index,
}),
(Self::ListIndex(list_index), ReflectMut::Array(reflect_list)) => reflect_list
.get_mut(*list_index)
.ok_or(ReflectPathError::InvalidListIndex {
index: current_index,
list_index: *list_index,
}),
(Self::ListIndex(_), _) => Err(ReflectPathError::ExpectedList {
index: current_index,
}),
(Self::Field(field), ReflectMut::Enum(reflect_enum)) => {
match reflect_enum.variant_type() {
VariantType::Struct => {
reflect_enum
.field_mut(field)
.ok_or(ReflectPathError::InvalidField {
index: current_index,
field,
})
}
_ => Err(ReflectPathError::ExpectedStructVariant {
index: current_index,
}),
}
}
(Self::FieldIndex(field_index), ReflectMut::Enum(reflect_enum)) => {
match reflect_enum.variant_type() {
VariantType::Struct => reflect_enum.field_at_mut(*field_index).ok_or(
ReflectPathError::InvalidFieldIndex {
index: current_index,
field_index: *field_index,
},
),
_ => Err(ReflectPathError::ExpectedStructVariant {
index: current_index,
}),
}
}
(Self::TupleIndex(tuple_variant_index), ReflectMut::Enum(reflect_enum)) => {
match reflect_enum.variant_type() {
VariantType::Tuple => reflect_enum.field_at_mut(*tuple_variant_index).ok_or(
ReflectPathError::InvalidTupleVariantIndex {
index: current_index,
tuple_variant_index: *tuple_variant_index,
},
),
_ => Err(ReflectPathError::ExpectedTupleVariant {
index: current_index,
}),
}
}
_ => Err(ReflectPathError::ExpectedStruct {
index: current_index,
}),
}
}
}
struct PathParser<'a> {
path: &'a str,
index: usize,
}
impl<'a> PathParser<'a> {
fn new(path: &'a str) -> Self {
Self { path, index: 0 }
}
fn next_token(&mut self) -> Option<Token<'a>> {
if self.index >= self.path.len() {
return None;
}
match self.path[self.index..].chars().next().unwrap() {
Token::DOT => {
self.index += 1;
return Some(Token::Dot);
}
Token::CROSSHATCH => {
self.index += 1;
return Some(Token::CrossHatch);
}
Token::OPEN_BRACKET => {
self.index += 1;
return Some(Token::OpenBracket);
}
Token::CLOSE_BRACKET => {
self.index += 1;
return Some(Token::CloseBracket);
}
_ => {}
}
// we can assume we are parsing an ident now
for (char_index, character) in self.path[self.index..].chars().enumerate() {
match character {
Token::DOT | Token::CROSSHATCH | Token::OPEN_BRACKET | Token::CLOSE_BRACKET => {
let ident = Token::Ident(&self.path[self.index..self.index + char_index]);
self.index += char_index;
return Some(ident);
}
_ => {}
}
}
let ident = Token::Ident(&self.path[self.index..]);
self.index = self.path.len();
Some(ident)
}
fn token_to_access(&mut self, token: Token<'a>) -> Result<AccessRef<'a>, ReflectPathError<'a>> {
let current_index = self.index;
match token {
Token::Dot => {
if let Some(Token::Ident(value)) = self.next_token() {
value
.parse::<usize>()
.map(AccessRef::TupleIndex)
.or(Ok(AccessRef::Field(value)))
} else {
Err(ReflectPathError::ExpectedIdent {
index: current_index,
})
}
}
Token::CrossHatch => {
if let Some(Token::Ident(value)) = self.next_token() {
Ok(AccessRef::FieldIndex(value.parse::<usize>()?))
} else {
Err(ReflectPathError::ExpectedIdent {
index: current_index,
})
}
}
Token::OpenBracket => {
let access = if let Some(Token::Ident(value)) = self.next_token() {
AccessRef::ListIndex(value.parse::<usize>()?)
} else {
return Err(ReflectPathError::ExpectedIdent {
index: current_index,
});
};
if !matches!(self.next_token(), Some(Token::CloseBracket)) {
return Err(ReflectPathError::ExpectedToken {
index: current_index,
token: Token::OPEN_BRACKET_STR,
});
}
Ok(access)
}
Token::CloseBracket => Err(ReflectPathError::UnexpectedToken {
index: current_index,
token: Token::CLOSE_BRACKET_STR,
}),
Token::Ident(value) => value
.parse::<usize>()
.map(AccessRef::TupleIndex)
.or(Ok(AccessRef::Field(value))),
}
}
}
impl<'a> Iterator for PathParser<'a> {
type Item = (Result<AccessRef<'a>, ReflectPathError<'a>>, usize);
fn next(&mut self) -> Option<Self::Item> {
let token = self.next_token()?;
let index = self.index;
Some((self.token_to_access(token), index))
}
}
enum Token<'a> {
Dot,
CrossHatch,
OpenBracket,
CloseBracket,
Ident(&'a str),
}
impl<'a> Token<'a> {
const DOT: char = '.';
const CROSSHATCH: char = '#';
const OPEN_BRACKET: char = '[';
const CLOSE_BRACKET: char = ']';
const OPEN_BRACKET_STR: &'static str = "[";
const CLOSE_BRACKET_STR: &'static str = "]";
}
#[cfg(test)]
#[allow(clippy::float_cmp, clippy::approx_constant)]
mod tests {
use super::*;
use crate as bevy_reflect;
use crate::*;
#[derive(Reflect)]
struct A {
w: usize,
x: B,
y: Vec<C>,
z: D,
unit_variant: F,
tuple_variant: F,
struct_variant: F,
array: [i32; 3],
tuple: (bool, f32),
}
#[derive(Reflect)]
struct B {
foo: usize,
bar: C,
}
#[derive(Reflect)]
struct C {
baz: f32,
}
#[derive(Reflect)]
struct D(E);
#[derive(Reflect)]
struct E(f32, usize);
#[derive(Reflect, PartialEq, Debug)]
enum F {
Unit,
Tuple(u32, u32),
Struct { value: char },
}
#[test]
fn parsed_path_parse() {
assert_eq!(
&*ParsedPath::parse("w").unwrap().0,
&[(Access::Field("w".to_string()), 1)]
);
assert_eq!(
&*ParsedPath::parse("x.foo").unwrap().0,
&[
(Access::Field("x".to_string()), 1),
(Access::Field("foo".to_string()), 2)
]
);
assert_eq!(
&*ParsedPath::parse("x.bar.baz").unwrap().0,
&[
(Access::Field("x".to_string()), 1),
(Access::Field("bar".to_string()), 2),
(Access::Field("baz".to_string()), 6)
]
);
assert_eq!(
&*ParsedPath::parse("y[1].baz").unwrap().0,
&[
(Access::Field("y".to_string()), 1),
(Access::ListIndex(1), 2),
(Access::Field("baz".to_string()), 5)
]
);
assert_eq!(
&*ParsedPath::parse("z.0.1").unwrap().0,
&[
(Access::Field("z".to_string()), 1),
(Access::TupleIndex(0), 2),
(Access::TupleIndex(1), 4),
]
);
assert_eq!(
&*ParsedPath::parse("x#0").unwrap().0,
&[
(Access::Field("x".to_string()), 1),
(Access::FieldIndex(0), 2),
]
);
assert_eq!(
&*ParsedPath::parse("x#0#1").unwrap().0,
&[
(Access::Field("x".to_string()), 1),
(Access::FieldIndex(0), 2),
(Access::FieldIndex(1), 4)
]
);
}
#[test]
fn parsed_path_get_field() {
let a = A {
w: 1,
x: B {
foo: 10,
bar: C { baz: 3.14 },
},
y: vec![C { baz: 1.0 }, C { baz: 2.0 }],
z: D(E(10.0, 42)),
unit_variant: F::Unit,
tuple_variant: F::Tuple(123, 321),
struct_variant: F::Struct { value: 'm' },
array: [86, 75, 309],
tuple: (true, 1.23),
};
let b = ParsedPath::parse("w").unwrap();
let c = ParsedPath::parse("x.foo").unwrap();
let d = ParsedPath::parse("x.bar.baz").unwrap();
let e = ParsedPath::parse("y[1].baz").unwrap();
let f = ParsedPath::parse("z.0.1").unwrap();
let g = ParsedPath::parse("x#0").unwrap();
let h = ParsedPath::parse("x#1#0").unwrap();
let i = ParsedPath::parse("unit_variant").unwrap();
let j = ParsedPath::parse("tuple_variant.1").unwrap();
let k = ParsedPath::parse("struct_variant.value").unwrap();
let l = ParsedPath::parse("struct_variant#0").unwrap();
let m = ParsedPath::parse("array[2]").unwrap();
let n = ParsedPath::parse("tuple.1").unwrap();
for _ in 0..30 {
assert_eq!(*b.element::<usize>(&a).unwrap(), 1);
assert_eq!(*c.element::<usize>(&a).unwrap(), 10);
assert_eq!(*d.element::<f32>(&a).unwrap(), 3.14);
assert_eq!(*e.element::<f32>(&a).unwrap(), 2.0);
assert_eq!(*f.element::<usize>(&a).unwrap(), 42);
assert_eq!(*g.element::<usize>(&a).unwrap(), 10);
assert_eq!(*h.element::<f32>(&a).unwrap(), 3.14);
assert_eq!(*i.element::<F>(&a).unwrap(), F::Unit);
assert_eq!(*j.element::<u32>(&a).unwrap(), 321);
assert_eq!(*k.element::<char>(&a).unwrap(), 'm');
assert_eq!(*l.element::<char>(&a).unwrap(), 'm');
assert_eq!(*m.element::<i32>(&a).unwrap(), 309);
assert_eq!(*n.element::<f32>(&a).unwrap(), 1.23);
}
}
#[test]
fn reflect_array_behaves_like_list() {
#[derive(Reflect)]
struct A {
list: Vec<u8>,
array: [u8; 10],
}
let a = A {
list: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};
assert_eq!(*a.path::<u8>("list[5]").unwrap(), 5);
assert_eq!(*a.path::<u8>("array[5]").unwrap(), 5);
assert_eq!(*a.path::<u8>("list[0]").unwrap(), 0);
assert_eq!(*a.path::<u8>("array[0]").unwrap(), 0);
}
#[test]
fn reflect_array_behaves_like_list_mut() {
#[derive(Reflect)]
struct A {
list: Vec<u8>,
array: [u8; 10],
}
let mut a = A {
list: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};
assert_eq!(*a.path_mut::<u8>("list[5]").unwrap(), 5);
assert_eq!(*a.path_mut::<u8>("array[5]").unwrap(), 5);
*a.path_mut::<u8>("list[5]").unwrap() = 10;
*a.path_mut::<u8>("array[5]").unwrap() = 10;
assert_eq!(*a.path_mut::<u8>("list[5]").unwrap(), 10);
assert_eq!(*a.path_mut::<u8>("array[5]").unwrap(), 10);
}
#[test]
fn reflect_path() {
let mut a = A {
w: 1,
x: B {
foo: 10,
bar: C { baz: 3.14 },
},
y: vec![C { baz: 1.0 }, C { baz: 2.0 }],
z: D(E(10.0, 42)),
unit_variant: F::Unit,
tuple_variant: F::Tuple(123, 321),
struct_variant: F::Struct { value: 'm' },
array: [86, 75, 309],
tuple: (true, 1.23),
};
assert_eq!(*a.path::<usize>("w").unwrap(), 1);
assert_eq!(*a.path::<usize>("x.foo").unwrap(), 10);
assert_eq!(*a.path::<f32>("x.bar.baz").unwrap(), 3.14);
assert_eq!(*a.path::<f32>("y[1].baz").unwrap(), 2.0);
assert_eq!(*a.path::<usize>("z.0.1").unwrap(), 42);
assert_eq!(*a.path::<usize>("x#0").unwrap(), 10);
assert_eq!(*a.path::<f32>("x#1#0").unwrap(), 3.14);
assert_eq!(*a.path::<F>("unit_variant").unwrap(), F::Unit);
assert_eq!(*a.path::<u32>("tuple_variant.1").unwrap(), 321);
assert_eq!(*a.path::<char>("struct_variant.value").unwrap(), 'm');
assert_eq!(*a.path::<char>("struct_variant#0").unwrap(), 'm');
assert_eq!(*a.path::<i32>("array[2]").unwrap(), 309);
assert_eq!(*a.path::<f32>("tuple.1").unwrap(), 1.23);
*a.path_mut::<f32>("tuple.1").unwrap() = 3.21;
assert_eq!(*a.path::<f32>("tuple.1").unwrap(), 3.21);
*a.path_mut::<f32>("y[1].baz").unwrap() = 3.0;
assert_eq!(a.y[1].baz, 3.0);
*a.path_mut::<u32>("tuple_variant.0").unwrap() = 1337;
assert_eq!(a.tuple_variant, F::Tuple(1337, 321));
assert_eq!(
a.reflect_path("x.notreal").err().unwrap(),
ReflectPathError::InvalidField {
index: 2,
field: "notreal"
}
);
assert_eq!(
a.reflect_path("unit_variant.0").err().unwrap(),
ReflectPathError::ExpectedTupleVariant { index: 13 }
);
assert_eq!(
a.reflect_path("x..").err().unwrap(),
ReflectPathError::ExpectedIdent { index: 2 }
);
assert_eq!(
a.reflect_path("x[0]").err().unwrap(),
ReflectPathError::ExpectedList { index: 2 }
);
assert_eq!(
a.reflect_path("y.x").err().unwrap(),
ReflectPathError::ExpectedStruct { index: 2 }
);
assert!(matches!(
a.reflect_path("y[badindex]"),
Err(ReflectPathError::IndexParseError(_))
));
}
}