浏览代码

Return Result from AmlName::from_str and co.

This allows it to be used more easily from existing methods, and also makes
diagnosing why a particular string is invalid easier.
Isaac Woods 5 年之前
父节点
当前提交
270a115202
共有 3 个文件被更改,包括 17 次插入24 次删除
  1. 2 1
      aml/src/lib.rs
  2. 6 10
      aml/src/name_object.rs
  3. 9 13
      aml/src/namespace.rs

+ 2 - 1
aml/src/lib.rs

@@ -205,7 +205,7 @@ pub enum AmlError {
      */
     UnexpectedEndOfStream,
     UnexpectedByte(u8),
-    InvalidNameSeg([u8; 4]),
+    InvalidNameSeg,
     InvalidFieldFlags,
     IncompatibleValueConversion,
     UnterminatedStringConstant,
@@ -219,6 +219,7 @@ pub enum AmlError {
     /*
      * Errors produced manipulating AML names.
      */
+    EmptyNamesAreInvalid,
     /// Produced when trying to normalize a path that does not point to a valid level of the
     /// namespace. E.g. `\_SB.^^PCI0` goes above the root of the namespace. The contained value is the name that
     /// normalization was attempted upon.

+ 6 - 10
aml/src/name_object.rs

@@ -139,10 +139,10 @@ where
 pub struct NameSeg(pub(crate) [u8; 4]);
 
 impl NameSeg {
-    pub(crate) fn from_str(string: &str) -> Option<NameSeg> {
+    pub(crate) fn from_str(string: &str) -> Result<NameSeg, AmlError> {
         // Each NameSeg can only have four chars, and must have at least one
         if string.len() < 1 || string.len() > 4 {
-            return None;
+            return Err(AmlError::InvalidNameSeg);
         }
 
         // We pre-fill the array with '_', so it will already be correct if the length is < 4
@@ -151,19 +151,19 @@ impl NameSeg {
 
         // Manually do the first one, because we have to check it's a LeadNameChar
         if !is_lead_name_char(bytes[0]) {
-            return None;
+            return Err(AmlError::InvalidNameSeg);
         }
         seg[0] = bytes[0];
 
         // Copy the rest of the chars, checking that they're NameChars
         for i in 1..bytes.len() {
             if !is_name_char(bytes[i]) {
-                return None;
+                return Err(AmlError::InvalidNameSeg);
             }
             seg[i] = bytes[i];
         }
 
-        Some(NameSeg(seg))
+        Ok(NameSeg(seg))
     }
 
     /// Turn a `NameSeg` into a `&str`. Returns it in a `ParseResult` so it's easy to use from
@@ -247,11 +247,7 @@ mod tests {
         check_err!(name_path().parse(&[], &mut context), AmlError::UnexpectedEndOfStream, &[]);
         check_ok!(name_path().parse(&[0x00], &mut context), alloc::vec![], &[]);
         check_ok!(name_path().parse(&[0x00, 0x00], &mut context), alloc::vec![], &[0x00]);
-        check_err!(
-            name_path().parse(&[0x2e, b'A'], &mut context),
-            AmlError::UnexpectedEndOfStream,
-            &[0x2e, b'A']
-        );
+        check_err!(name_path().parse(&[0x2e, b'A'], &mut context), AmlError::UnexpectedEndOfStream, &[0x2e, b'A']);
         check_ok!(
             name_path().parse(&[0x2e, b'A', b'B', b'C', b'D', b'E', b'_', b'F', b'G'], &mut context),
             alloc::vec![

+ 9 - 13
aml/src/namespace.rs

@@ -157,11 +157,10 @@ impl AmlName {
         AmlName(alloc::vec![NameComponent::Segment(seg)])
     }
 
-    /// Convert a string representation of an AML name into an `AmlName`. Returns `None` if the
-    /// passed string is not a valid AML path.
-    pub fn from_str(mut string: &str) -> Option<AmlName> {
+    /// Convert a string representation of an AML name into an `AmlName`.
+    pub fn from_str(mut string: &str) -> Result<AmlName, AmlError> {
         if string.len() == 0 {
-            return None;
+            return Err(AmlError::EmptyNamesAreInvalid);
         }
 
         let mut components = Vec::new();
@@ -185,7 +184,7 @@ impl AmlName {
             }
         }
 
-        Some(AmlName(components))
+        Ok(AmlName(components))
     }
 
     pub fn as_string(&self) -> String {
@@ -298,11 +297,11 @@ mod tests {
 
     #[test]
     fn test_aml_name_from_str() {
-        assert_eq!(AmlName::from_str(""), None);
-        assert_eq!(AmlName::from_str("\\"), Some(AmlName::root()));
+        assert_eq!(AmlName::from_str(""), Err(AmlError::EmptyNamesAreInvalid));
+        assert_eq!(AmlName::from_str("\\"), Ok(AmlName::root()));
         assert_eq!(
             AmlName::from_str("\\_SB.PCI0"),
-            Some(AmlName(alloc::vec![
+            Ok(AmlName(alloc::vec![
                 NameComponent::Root,
                 NameComponent::Segment(NameSeg([b'_', b'S', b'B', b'_'])),
                 NameComponent::Segment(NameSeg([b'P', b'C', b'I', b'0']))
@@ -310,7 +309,7 @@ mod tests {
         );
         assert_eq!(
             AmlName::from_str("\\_SB.^^^PCI0"),
-            Some(AmlName(alloc::vec![
+            Ok(AmlName(alloc::vec![
                 NameComponent::Root,
                 NameComponent::Segment(NameSeg([b'_', b'S', b'B', b'_'])),
                 NameComponent::Prefix,
@@ -383,10 +382,7 @@ mod tests {
     fn test_aml_name_parent() {
         assert_eq!(AmlName::from_str("\\").unwrap().parent(), Err(AmlError::RootHasNoParent));
         assert_eq!(AmlName::from_str("\\_SB").unwrap().parent(), Ok(AmlName::root()));
-        assert_eq!(
-            AmlName::from_str("\\_SB.PCI0").unwrap().parent(),
-            Ok(AmlName::from_str("\\_SB").unwrap())
-        );
+        assert_eq!(AmlName::from_str("\\_SB.PCI0").unwrap().parent(), Ok(AmlName::from_str("\\_SB").unwrap()));
         assert_eq!(AmlName::from_str("\\_SB.PCI0").unwrap().parent().unwrap().parent(), Ok(AmlName::root()));
     }
 }