diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-25 21:25:06 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-25 21:25:06 -0700 |
| commit | b3cb3553ab5c4649b0fe6b5f94ac42c5b69e8f89 (patch) | |
| tree | ed33922a84eeaa541d8a5c772445b81be1eb10fa /src/facility/i3.rs | |
| parent | c312a47101bf1787e7bbc44c409e15315684fd19 (diff) | |
refactor JSON code into its own file, now a full parser
also, update to the latest version of the JSON library. it's under active development and had a release today. huh! Force-Push: yes Change-Id: I0e162f64c963744cf786a85d00c83dbabade05be
Diffstat (limited to 'src/facility/i3.rs')
| -rw-r--r-- | src/facility/i3.rs | 53 |
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))) } } |