aboutsummaryrefslogtreecommitdiffstats
path: root/components/plugins/utils.rs
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2014-12-01 10:10:33 -0500
committerManish Goregaokar <manishsmail@gmail.com>2014-12-27 02:43:13 +0530
commit21607f066ca07a68a6fe84acad373de5da45523f (patch)
tree57781640172e2f07deaf0023f1e12727bb9ab55f /components/plugins/utils.rs
parentdd8360fb10a16f2124addfb276400615fe78a7ee (diff)
downloadservo-21607f066ca07a68a6fe84acad373de5da45523f.tar.gz
servo-21607f066ca07a68a6fe84acad373de5da45523f.zip
Add internal plugin for creating Reflectable implementations
Diffstat (limited to 'components/plugins/utils.rs')
-rw-r--r--components/plugins/utils.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/components/plugins/utils.rs b/components/plugins/utils.rs
new file mode 100644
index 00000000000..434a5e08582
--- /dev/null
+++ b/components/plugins/utils.rs
@@ -0,0 +1,29 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+use syntax::ptr::P;
+use syntax::ast::{TyPath, Path, AngleBracketedParameters, PathSegment, Ty};
+
+/// Matches a type with a provided string, and returns its type parameters if successful
+pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
+ match ty.node {
+ TyPath(Path {segments: ref seg, ..}, _, _) => {
+ // So ast::Path isn't the full path, just the tokens that were provided.
+ // I could muck around with the maps and find the full path
+ // however the more efficient way is to simply reverse the iterators and zip them
+ // which will compare them in reverse until one of them runs out of segments
+ if seg.iter().rev().zip(segments.iter().rev()).all(|(a,b)| a.identifier.as_str() == *b) {
+ match seg.as_slice().last() {
+ Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
+ Some(a.types.as_slice())
+ }
+ _ => None
+ }
+ } else {
+ None
+ }
+ },
+ _ => None
+ }
+}