From bdd3f20522eefe01831ad9cd74002dfe95de20d1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 13 Dec 2018 16:30:40 -0800 Subject: [PATCH] Add PropertySheet::map method --- src/lib.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 428e8101..0a53e320 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ pub enum PropertySheetError { InvalidRegex(regex::Error) } -pub struct PropertySheet> { +pub struct PropertySheet

> { states: Vec, property_sets: Vec

, text_regexes: Vec, @@ -86,7 +86,7 @@ pub struct Tree(*mut ffi::TSTree); pub struct TreeCursor<'a>(ffi::TSTreeCursor, PhantomData<&'a ()>); -pub struct TreePropertyCursor<'a, P: 'a + DeserializeOwned> { +pub struct TreePropertyCursor<'a, P> { cursor: TreeCursor<'a>, state_stack: Vec, child_index_stack: Vec, @@ -370,7 +370,7 @@ impl Tree { self.root_node().walk() } - pub fn walk_with_properties<'a, P: DeserializeOwned>( + pub fn walk_with_properties<'a, P>( &'a self, property_sheet: &'a PropertySheet

, source: &'a str, @@ -574,7 +574,7 @@ impl<'a> Drop for TreeCursor<'a> { } } -impl<'a, P: DeserializeOwned> TreePropertyCursor<'a, P> { +impl<'a, P> TreePropertyCursor<'a, P> { fn new(tree: &'a Tree, property_sheet: &'a PropertySheet

, source: &'a str) -> Self { let mut result = Self { cursor: tree.root_node().walk(), @@ -714,8 +714,11 @@ impl Into for Range { } } -impl PropertySheet

{ - pub fn new(language: Language, json: &str) -> Result { +impl

PropertySheet

{ + pub fn new(language: Language, json: &str) -> Result + where + P: DeserializeOwned, + { #[derive(Deserialize, Debug)] struct PropertyTransitionJSON { #[serde(rename = "type")] @@ -787,6 +790,21 @@ impl PropertySheet

{ text_regexes, }) } + + pub fn map(self, mut f: F) -> Result, E> + where + F: FnMut(P) -> Result, + { + let mut property_sets = Vec::with_capacity(self.property_sets.len()); + for set in self.property_sets { + property_sets.push(f(set)?); + } + Ok(PropertySheet { + states: self.states, + text_regexes: self.text_regexes, + property_sets, + }) + } } #[cfg(test)]