From fdcacd7d18aa8cafac4abdab8f15e6a002296921 Mon Sep 17 00:00:00 2001
From: Connor Wood <connorwood71@gmail.com>
Date: Mon, 24 Jul 2017 17:27:01 +0100
Subject: [PATCH] Implemented numerous "simple" type conversions

---
 src/acpi/aml/namedobj.rs  |   6 +-
 src/acpi/aml/namespace.rs | 116 ++++++++++++++++++++++----------------
 2 files changed, 70 insertions(+), 52 deletions(-)

diff --git a/src/acpi/aml/namedobj.rs b/src/acpi/aml/namedobj.rs
index c92e3455..7ca72c2f 100644
--- a/src/acpi/aml/namedobj.rs
+++ b/src/acpi/aml/namedobj.rs
@@ -4,7 +4,7 @@ use collections::string::String;
 use super::AmlError;
 use super::parser::{ AmlParseType, ParseResult, AmlParseTypeGeneric, AmlExecutionContext, ExecutionState };
 use super::namespace::{AmlValue, ObjectReference, FieldSelector, Method, get_namespace_string,
-                       Accessor, BufferField};
+                       Accessor, BufferField, FieldUnit};
 use super::namestring::{parse_name_string, parse_name_seg};
 use super::termlist::{parse_term_arg, parse_object_list};
 use super::pkglength::parse_pkg_length;
@@ -632,13 +632,13 @@ fn parse_field_element(data: &[u8],
     let length = if let Ok(field) = parse_named_field(data, ctx) {
         let local_scope_string = get_namespace_string(ctx.scope.clone(), AmlValue::String(field.val.name.clone()))?;
         
-        ctx.add_to_namespace(local_scope_string, AmlValue::FieldUnit {
+        ctx.add_to_namespace(local_scope_string, AmlValue::FieldUnit(FieldUnit {
             selector: selector.clone(),
             connection: Box::new(connection.clone()),
             flags: flags.clone(),
             offset: offset.clone(),
             length: field.val.length
-        })?;
+        }))?;
 
         *offset += field.val.length;
         field.len
diff --git a/src/acpi/aml/namespace.rs b/src/acpi/aml/namespace.rs
index ba9ebf49..a8b3336f 100644
--- a/src/acpi/aml/namespace.rs
+++ b/src/acpi/aml/namespace.rs
@@ -47,6 +47,15 @@ pub struct BufferField {
     pub length: Box<AmlValue>
 }
 
+#[derive(Clone)]
+pub struct FieldUnit {
+    pub selector: FieldSelector,
+    pub connection: Box<AmlValue>,
+    pub flags: FieldFlags,
+    pub offset: usize,
+    pub length: usize
+}
+
 pub struct Accessor {
     pub read: fn(usize) -> u64,
     pub write: fn(usize, u64)
@@ -71,13 +80,7 @@ pub enum AmlValue {
     DebugObject,
     Device(Vec<String>),
     Event(u64),
-    FieldUnit {
-        selector: FieldSelector,
-        connection: Box<AmlValue>,
-        flags: FieldFlags,
-        offset: usize,
-        length: usize
-    },
+    FieldUnit(FieldUnit),
     Integer(u64),
     IntegerConstant(u64),
     Method(Method),
@@ -110,46 +113,7 @@ impl Debug for AmlValue {
     fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { Ok(()) }
 }
 
-impl AmlValue {
-    pub fn get_as_event(&self) -> Result<u64, AmlError> {
-        match *self {
-            AmlValue::Event(ref e) => Ok(e.clone()),
-            _ => Err(AmlError::AmlValueError)
-        }
-    }
-
-    pub fn get_as_ddb_handle(&self) -> Result<Vec<String>, AmlError> {
-        match *self {
-            AmlValue::DDBHandle(ref v) => Ok(v.clone()),
-            _ => Err(AmlError::AmlValueError)
-        }
-    }
-    
-    pub fn get_as_string(&self) -> Result<String, AmlError> {
-        match *self {
-            AmlValue::String(ref s) => Ok(s.clone()),
-            _ => Err(AmlError::AmlValueError)
-        }
-    }
-
-    pub fn get_as_buffer_field(&self) -> Result<BufferField, AmlError> {
-        match *self {
-            AmlValue::BufferField(ref b) => Ok(b.clone()),
-            _ => {
-                let raw_buf = self.get_as_buffer()?;
-                let buf = Box::new(AmlValue::Buffer(raw_buf.clone()));
-                let idx = Box::new(AmlValue::IntegerConstant(0));
-                let len = Box::new(AmlValue::Integer(raw_buf.len() as u64));
-
-                Ok(BufferField {
-                    source_buf: buf,
-                    index: idx,
-                    length: len
-                })
-            }
-        }
-    }
-    
+impl AmlValue {    
     pub fn get_as_buffer(&self) -> Result<Vec<u8>, AmlError> {
         match *self {
             AmlValue::Buffer(ref b) => Ok(b.clone()),
@@ -185,10 +149,50 @@ impl AmlValue {
             _ => Err(AmlError::AmlValueError)
         }
     }
+
+    pub fn get_as_buffer_field(&self) -> Result<BufferField, AmlError> {
+        match *self {
+            AmlValue::BufferField(ref b) => Ok(b.clone()),
+            _ => {
+                let raw_buf = self.get_as_buffer()?;
+                let buf = Box::new(AmlValue::Buffer(raw_buf.clone()));
+                let idx = Box::new(AmlValue::IntegerConstant(0));
+                let len = Box::new(AmlValue::Integer(raw_buf.len() as u64));
+
+                Ok(BufferField {
+                    source_buf: buf,
+                    index: idx,
+                    length: len
+                })
+            }
+        }
+    }
+
+    pub fn get_as_ddb_handle(&self) -> Result<Vec<String>, AmlError> {
+        // TODO: Integer conversion
+        match *self {
+            AmlValue::DDBHandle(ref v) => Ok(v.clone()),
+            _ => Err(AmlError::AmlValueError)
+        }
+    }
+
+    pub fn get_as_device(&self) -> Result<Vec<String>, AmlError> {
+        match *self {
+            AmlValue::Device(ref s) => Ok(s.clone()),
+            _ => Err(AmlError::AmlValueError)
+        }
+    }
     
-    pub fn get_as_package(&self) -> Result<Vec<AmlValue>, AmlError> {
+    pub fn get_as_event(&self) -> Result<u64, AmlError> {
         match *self {
-            AmlValue::Package(ref p) => Ok(p.clone()),
+            AmlValue::Event(ref e) => Ok(e.clone()),
+            _ => Err(AmlError::AmlValueError)
+        }
+    }
+    
+    pub fn get_as_field_unit(&self) -> Result<FieldUnit, AmlError> {
+        match *self {
+            AmlValue::FieldUnit(ref e) => Ok(e.clone()),
             _ => Err(AmlError::AmlValueError)
         }
     }
@@ -206,6 +210,20 @@ impl AmlValue {
             _ => Err(AmlError::AmlValueError)
         }
     }
+    
+    pub fn get_as_package(&self) -> Result<Vec<AmlValue>, AmlError> {
+        match *self {
+            AmlValue::Package(ref p) => Ok(p.clone()),
+            _ => Err(AmlError::AmlValueError)
+        }
+    }
+    
+    pub fn get_as_string(&self) -> Result<String, AmlError> {
+        match *self {
+            AmlValue::String(ref s) => Ok(s.clone()),
+            _ => Err(AmlError::AmlValueError)
+        }
+    }
 }
 
 impl Method {
-- 
GitLab