|
| 1 | +use crate::{Address, Result, process::run}; |
| 2 | + |
| 3 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 4 | +#[non_exhaustive] |
| 5 | +/// The transport properties of an IBus D-Bus address. |
| 6 | +/// |
| 7 | +/// This transport type queries the IBus daemon for its D-Bus address using the `ibus address` |
| 8 | +/// command. IBus (Intelligent Input Bus) is an input method framework used primarily on Linux |
| 9 | +/// systems for entering text in various languages. |
| 10 | +/// |
| 11 | +/// # Platform Support |
| 12 | +/// |
| 13 | +/// This transport is available on Unix-like systems where IBus is installed. |
| 14 | +/// |
| 15 | +/// # Example |
| 16 | +/// |
| 17 | +/// ```no_run |
| 18 | +/// # use zbus::address::transport::{Transport, Ibus}; |
| 19 | +/// # use zbus::Address; |
| 20 | +/// # |
| 21 | +/// // Create an IBus transport |
| 22 | +/// let ibus = Ibus::new(); |
| 23 | +/// let _addr = Transport::Ibus(ibus); |
| 24 | +/// |
| 25 | +/// // Or use it directly as an address |
| 26 | +/// let _addr = Address::from(Transport::Ibus(Ibus::new())); |
| 27 | +/// ``` |
| 28 | +pub struct Ibus; |
| 29 | + |
| 30 | +impl Ibus { |
| 31 | + /// Create a new IBus transport. |
| 32 | + /// |
| 33 | + /// This will query the IBus daemon for its D-Bus address when the connection is established. |
| 34 | + #[must_use] |
| 35 | + pub fn new() -> Self { |
| 36 | + Self |
| 37 | + } |
| 38 | + |
| 39 | + /// Determine the actual transport details behind an IBus address. |
| 40 | + /// |
| 41 | + /// This method executes the `ibus address` command to retrieve the D-Bus address from the |
| 42 | + /// running IBus daemon, then parses and returns the underlying transport. |
| 43 | + /// |
| 44 | + /// # Errors |
| 45 | + /// |
| 46 | + /// Returns an error if: |
| 47 | + /// - The `ibus` command is not found or fails to execute |
| 48 | + /// - The IBus daemon is not running |
| 49 | + /// - The command output cannot be parsed as a valid D-Bus address |
| 50 | + /// - The command output is not valid UTF-8 |
| 51 | + /// |
| 52 | + /// # Example |
| 53 | + /// |
| 54 | + /// ```no_run |
| 55 | + /// # use zbus::connection::Builder; |
| 56 | + /// # use zbus::block_on; |
| 57 | + /// # |
| 58 | + /// # block_on(async { |
| 59 | + /// // This method is used internally by the connection builder |
| 60 | + /// let _conn = Builder::ibus()?.build().await?; |
| 61 | + /// # Ok::<(), zbus::Error>(()) |
| 62 | + /// # }).unwrap(); |
| 63 | + /// ``` |
| 64 | + pub(super) async fn bus_address(&self) -> Result<Address> { |
| 65 | + let output = run("ibus", ["address"]) |
| 66 | + .await |
| 67 | + .expect("failed to wait on ibus output"); |
| 68 | + |
| 69 | + if !output.status.success() { |
| 70 | + return Err(crate::Error::Address(format!( |
| 71 | + "ibus terminated with code: {}", |
| 72 | + output.status |
| 73 | + ))); |
| 74 | + } |
| 75 | + |
| 76 | + let addr = String::from_utf8(output.stdout).map_err(|e| { |
| 77 | + crate::Error::Address(format!("Unable to parse ibus output as UTF-8: {e}")) |
| 78 | + })?; |
| 79 | + |
| 80 | + addr.trim().parse() |
| 81 | + } |
| 82 | + |
| 83 | + /// Parse IBus transport from D-Bus address options. |
| 84 | + /// |
| 85 | + /// The IBus transport type does not require any options, so this method will succeed |
| 86 | + /// as long as the transport type is specified as "ibus". |
| 87 | + /// |
| 88 | + /// # Errors |
| 89 | + /// |
| 90 | + /// This method does not return errors for the IBus transport, but the signature is kept |
| 91 | + /// consistent with other transport types. |
| 92 | + pub(super) fn from_options(_opts: std::collections::HashMap<&str, &str>) -> Result<Self> { |
| 93 | + Ok(Self) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl Default for Ibus { |
| 98 | + fn default() -> Self { |
| 99 | + Self::new() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl std::fmt::Display for Ibus { |
| 104 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 105 | + write!(f, "ibus:") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[cfg(test)] |
| 110 | +mod tests { |
| 111 | + use super::*; |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn test_ibus_new() { |
| 115 | + let ibus = Ibus::new(); |
| 116 | + assert_eq!(ibus.to_string(), "ibus:"); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_ibus_default() { |
| 121 | + let ibus = Ibus::default(); |
| 122 | + assert_eq!(ibus.to_string(), "ibus:"); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_ibus_from_options() { |
| 127 | + let options = std::collections::HashMap::new(); |
| 128 | + let ibus = Ibus::from_options(options).unwrap(); |
| 129 | + assert_eq!(ibus, Ibus::new()); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_ibus_display() { |
| 134 | + let ibus = Ibus::new(); |
| 135 | + assert_eq!(format!("{}", ibus), "ibus:"); |
| 136 | + } |
| 137 | +} |
0 commit comments