Browse Source

Also return a Result<A> in map_with_context

We sometimes want to do fallible operations while adding stuff to the
namespace, too. This is less ergonomic, however, because you can't easily
use the `?` operator because you're returning the tuple.
Isaac Woods 5 years ago
parent
commit
99c4020f7d
2 changed files with 19 additions and 17 deletions
  1. 10 8
      aml_parser/src/parser.rs
  2. 9 9
      aml_parser/src/term_object.rs

+ 10 - 8
aml_parser/src/parser.rs

@@ -1,7 +1,6 @@
 use crate::{pkg_length::PkgLength, AmlContext, AmlError};
 use alloc::vec::Vec;
 use core::marker::PhantomData;
-use log::trace;
 
 pub type ParseResult<'a, 'c, R> =
     Result<(&'a [u8], &'c mut AmlContext, R), (&'a [u8], &'c mut AmlContext, AmlError)>;
@@ -21,7 +20,7 @@ where
 
     fn map_with_context<F, A>(self, map_fn: F) -> MapWithContext<'a, 'c, Self, F, R, A>
     where
-        F: Fn(R, &'c mut AmlContext) -> (A, &'c mut AmlContext),
+        F: Fn(R, &'c mut AmlContext) -> (Result<A, AmlError>, &'c mut AmlContext),
     {
         MapWithContext { parser: self, map_fn, _phantom: PhantomData }
     }
@@ -297,7 +296,7 @@ pub struct MapWithContext<'a, 'c, P, F, R, A>
 where
     'c: 'a,
     P: Parser<'a, 'c, R>,
-    F: Fn(R, &'c mut AmlContext) -> (A, &'c mut AmlContext),
+    F: Fn(R, &'c mut AmlContext) -> (Result<A, AmlError>, &'c mut AmlContext),
 {
     parser: P,
     map_fn: F,
@@ -308,13 +307,16 @@ impl<'a, 'c, P, F, R, A> Parser<'a, 'c, A> for MapWithContext<'a, 'c, P, F, R, A
 where
     'c: 'a,
     P: Parser<'a, 'c, R>,
-    F: Fn(R, &'c mut AmlContext) -> (A, &'c mut AmlContext),
+    F: Fn(R, &'c mut AmlContext) -> (Result<A, AmlError>, &'c mut AmlContext),
 {
     fn parse(&self, input: &'a [u8], context: &'c mut AmlContext) -> ParseResult<'a, 'c, A> {
-        self.parser.parse(input, context).map(|(new_input, context, result)| {
-            let (mapped_result, context) = (self.map_fn)(result, context);
-            (new_input, context, mapped_result)
-        })
+        match self.parser.parse(input, context) {
+            Ok((new_input, context, result)) => match (self.map_fn)(result, context) {
+                (Ok(result_value), context) => Ok((new_input, context, result_value)),
+                (Err(err), context) => Err((input, context, err)),
+            },
+            Err(result) => Err(result),
+        }
     }
 }
 

+ 9 - 9
aml_parser/src/term_object.rs

@@ -103,7 +103,7 @@ where
                 |(name, data_ref_object), context| {
                     // TODO: add to namespace
                     trace!("Defined name: {}", name);
-                    ((), context)
+                    (Ok(()), context)
                 },
             ),
         ))
@@ -125,16 +125,16 @@ where
                 .map_with_context(|(length, name), context| {
                     // TODO: change scope in `context`
                     trace!("Moving to scope: {:?}", name);
-                    ((length, name), context)
+                    (Ok((length, name, previous_scope)), context)
                 })
                 .feed(move |(pkg_length, name)| {
                     trace!("Scope with name: {}, length: {:?}", name, pkg_length);
                     term_list(pkg_length).map(move |_| Ok(name.clone()))
                 })
-                .map_with_context(|name, context| {
+                .map_with_context(|(name, previous_scope), context| {
                     // TODO: remove scope
                     trace!("Exiting scope: {}", name);
-                    ((), context)
+                    (Ok(()), context)
                 }),
         ))
         .discard_result()
@@ -168,7 +168,7 @@ where
                 |(((name, space), offset), region_len), context| {
                     trace!("Op region: {}, {}, {:?}, {:?}", name, space, offset, region_len);
                     // TODO: add to namespace
-                    ((), context)
+                    (Ok(()), context)
                 },
             ),
         ))
@@ -247,14 +247,14 @@ where
                 access_attrib
             );
             // TODO: put it in the namespace
-            ((), context)
+            (Ok(()), context)
         },
     );
 
     let named_field = name_seg().then(pkg_length()).map_with_context(|(name, length), context| {
         trace!("Named field with name {} and length {}", name.as_str(), length.raw_length);
         // TODO: put it in the namespace
-        ((), context)
+        (Ok(()), context)
     });
 
     choice!(reserved_field, access_field, named_field)
@@ -288,7 +288,7 @@ where
                         flags,
                         code.len()
                     );
-                    ((), context)
+                    (Ok(()), context)
                 }),
         ))
         .discard_result()
@@ -312,7 +312,7 @@ where
                 .map_with_context(|(name, result), context| {
                     // TODO: add to namespace
                     trace!("Device with name: {}", name);
-                    ((), context)
+                    (Ok(()), context)
                 }),
         ))
         .discard_result()