diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/facility/i3.rs | 17 | ||||
| -rw-r--r-- | src/module/i3_mode.rs | 7 |
2 files changed, 19 insertions, 5 deletions
diff --git a/src/facility/i3.rs b/src/facility/i3.rs index 53258f8..4dd4a87 100644 --- a/src/facility/i3.rs +++ b/src/facility/i3.rs @@ -531,7 +531,7 @@ fn parse_success_object(json: &[u8]) -> Result<bool> { } -pub fn parse_change_string(json: &[u8]) -> Result<String> { +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 { @@ -539,7 +539,7 @@ pub fn parse_change_string(json: &[u8]) -> Result<String> { } if let JsonEvent::ObjectKey(key) = parser.parse_next()? { - if key == "change" { + if key == expected_key { if let JsonEvent::String(result) = parser.parse_next()? { /* That's our answer; exit early. */ return Ok(result.into_owned()); @@ -547,10 +547,21 @@ pub fn parse_change_string(json: &[u8]) -> Result<String> { return Err(Error::custom("Reading i3 JSON, expected string.")); } } else { - return Err(Error::custom("Reading i3 JSON, expected \"change\" key.")); + return Err(Error::custom(&format!("Reading i3 JSON, expected {:?} key.", + expected_key))); } } else { return Err(Error::custom("Reading i3 JSON, expected object key.")); } } + +pub fn parse_change_string(json: &[u8]) -> Result<String> { + parse_top_level_key_string(json, "change") +} + + +pub fn parse_name_string(json: &[u8]) -> Result<String> { + parse_top_level_key_string(json, "name") +} + diff --git a/src/module/i3_mode.rs b/src/module/i3_mode.rs index c1b6484..96862eb 100644 --- a/src/module/i3_mode.rs +++ b/src/module/i3_mode.rs @@ -1,6 +1,8 @@ use crate::error::{ Error, Result }; use crate::facility::{ FacilityMap, FacilityType }; -use crate::facility::i3::{ parse_change_string, EventType, I3 }; +use crate::facility::i3::{ + parse_change_string, parse_name_string, EventType, I3 +}; use crate::module::{ CreatableModule, Module }; use std::sync::Arc; @@ -36,7 +38,8 @@ impl CreatableModule for I3ModeModule { return Err(Error::custom("Unable to subscribe to i3 mode changes.")); } - let current_mode = Arc::new(Mutex::new("unknown".to_string())); + let current_mode = Arc::new(Mutex::new(parse_name_string( + i3.command(12, "").await?.as_bytes())?)); let update_task = executor.spawn(update_loop(receiver, Arc::clone(¤t_mode), |