enum to string in function signature?
I don't know if this is an issue, more likely It's that I'm unsure of how best to do this.
The following is everything reduced down to an example:
#[dbus_proxy(
interface = "org.freedesktop.login1.Manager",
default_service = "org.freedesktop.login1",
default_path = "/org/freedesktop/login1"
)]
trait Manager {
//...
/// Inhibit method
#[inline]
fn inhibit(
&self,
what: InhibitThis, // Problem point, this is `s`
who: &str,
why: &str,
mode: &str,
) -> zbus::Result<std::os::unix::io::RawFd>;
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum InhibitThis {
Shutdown,
Sleep,
Idle,
HandlePowerKey,
HandleSuspendKey,
HandleHibernateKey,
HandleLidSwitch,
Invalid,
}
impl From<&str> for InhibitThis {
fn from(s: &str) -> Self {
match s.trim() {
"shutdown" => Self::Shutdown,
"sleep" => Self::Sleep,
"idle" => Self::Idle,
"handle-power-key" => Self::HandlePowerKey,
"handle-suspend-key" => Self::HandleSuspendKey,
"handle-hibernate-key" => Self::HandleHibernateKey,
"handle-lid-switch" => Self::HandleLidSwitch,
_ => Self::Invalid,
}
}
}
impl From<InhibitThis> for &str {
fn from(s: InhibitThis) -> Self {
match s {
InhibitThis::Shutdown => "shutdown",
InhibitThis::Sleep => "sleep",
InhibitThis::Idle => "idle",
InhibitThis::HandlePowerKey => "handle-power-key",
InhibitThis::HandleSuspendKey => "handle-suspend-key",
InhibitThis::HandleHibernateKey => "handle-hibernate-key",
InhibitThis::HandleLidSwitch => "handle-lid-switch",
InhibitThis::Invalid => "invalid",
}
}
}
impl TryFrom<OwnedValue> for InhibitThis {
type Error = zbus::Error;
fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
let value = <String>::try_from(value)?;
return Ok(Self::from(value.as_str()));
}
}
impl TryFrom<InhibitThis> for Value<'_> {
type Error = zbus::Error;
fn try_from(value: InhibitThis) -> Result<Self, Self::Error> {
Ok(Value::Str(zvariant::Str::from(<&str>::from(value))))
}
}
impl TryFrom<InhibitThis> for OwnedValue {
type Error = zbus::Error;
fn try_from(value: InhibitThis) -> Result<Self, Self::Error> {
Ok(OwnedValue::from(zvariant::Str::from(<&str>::from(value))))
}
}
impl Type for InhibitThis {
fn signature() -> zvariant::Signature<'static> {
Signature::from_str_unchecked("s")
}
}
I'm sort of jumping around trying to find the solution here. InhibitThis
needs to be s
dbus type, and the derives usually turn in to u
. I imagine I need to manually ser/de?
I also tried:
/// Inhibit method
#[inline]
fn inhibit(
&self,
data: SomeStruct,
) -> zbus::Result<std::os::unix::io::RawFd>;
}
but the signature ends up being (ssss)
when ssss
is required. There's a lot of cases like this in logind, so any tips would be greatly appreciated.