about summary refs log tree commit diff
path: root/src/facility/i3.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/facility/i3.rs')
-rw-r--r--src/facility/i3.rs53
1 files changed, 12 insertions, 41 deletions
diff --git a/src/facility/i3.rs b/src/facility/i3.rs
index 4dd4a87..9435b45 100644
--- a/src/facility/i3.rs
+++ b/src/facility/i3.rs
@@ -1,5 +1,6 @@
 use crate::error::{ Error, Result };
 use crate::facility::{ CreatableFacility, Facility, FacilityType };
+use crate::json::{ parse_json, Value };
 use crate::unix::get_username;
 use crate::xdg::xdg_runtime_directory;
 
@@ -20,7 +21,6 @@ use async_task::Task;
 use bitflags::bitflags;
 use futures_lite::{ AsyncReadExt, AsyncWriteExt, StreamExt };
 use futures_lite::io::{ split, ReadHalf, WriteHalf };
-use json_event_parser::{ JsonEvent, SliceJsonParser };
 
 
 #[allow(unused)]
@@ -504,54 +504,25 @@ async fn read(socket: Arc<Mutex<ReadHalf<UnixStream>>>)
 }
 
 
-/*   We make no attempt to parse fully general JSON; we exploit our knowledge
- * of the format i3 uses. Maybe later.
- */
 fn parse_success_object(json: &[u8]) -> Result<bool> {
-  let mut parser = SliceJsonParser::new(json);
-
-  if let JsonEvent::StartObject = parser.parse_next()? { } else {
-    return Err(Error::custom("Reading i3 JSON, expected object start."));
-  }
-
-  if let JsonEvent::ObjectKey(key) = parser.parse_next()? {
-    if key == "success" {
-      if let JsonEvent::Boolean(result) = parser.parse_next()? {
-        /* That's our answer; exit early. */
-        return Ok(result);
-      } else {
-        return Err(Error::custom("Reading i3 JSON, expected boolean."));
-      }
-    } else {
-      return Err(Error::custom("Reading i3 JSON, expected \"success\" key."));
-    }
+  if let Value::Object(object) = parse_json(json)?
+      && let Some(Value::Boolean(result)) = object.get("success")
+  {
+    Ok(*result)
   } else {
-    return Err(Error::custom("Reading i3 JSON, expected object key."));
+    Err(Error::custom("Reading i3 JSON, expected success boolean."))
   }
 }
 
 
 fn parse_top_level_key_string(json: &[u8], expected_key: &str) -> Result<String> {
-  let mut parser = SliceJsonParser::new(json);
-
-  if let JsonEvent::StartObject = parser.parse_next()? { } else {
-    return Err(Error::custom("Reading i3 JSON, expected object start."));
-  }
-
-  if let JsonEvent::ObjectKey(key) = parser.parse_next()? {
-    if key == expected_key {
-      if let JsonEvent::String(result) = parser.parse_next()? {
-        /* That's our answer; exit early. */
-        return Ok(result.into_owned());
-      } else {
-        return Err(Error::custom("Reading i3 JSON, expected string."));
-      }
-    } else {
-      return Err(Error::custom(&format!("Reading i3 JSON, expected {:?} key.",
-                                        expected_key)));
-    }
+  if let Value::Object(object) = parse_json(json)?
+      && let Some(Value::String(result)) = object.get(expected_key)
+  {
+    Ok(String::clone(result))
   } else {
-    return Err(Error::custom("Reading i3 JSON, expected object key."));
+    Err(Error::custom(&format!("Reading i3 JSON, expected {} string.",
+                               expected_key)))
   }
 }