1
use std::fmt;
2

            
3
use serde::Serialize;
4
use zbus::zvariant::{ObjectPath, Type};
5

            
6
use super::DESTINATION;
7
use crate::dbus::{Error, ServiceError};
8

            
9
#[derive(Type)]
10
#[zvariant(signature = "o")]
11
#[doc(alias = "org.freedesktop.Secret.Session")]
12
pub struct Session<'a>(zbus::Proxy<'a>);
13

            
14
impl zbus::proxy::Defaults for Session<'_> {
15
    const INTERFACE: &'static Option<zbus::names::InterfaceName<'static>> = &Some(
16
        zbus::names::InterfaceName::from_static_str_unchecked("org.freedesktop.Secret.Session"),
17
    );
18
    const DESTINATION: &'static Option<zbus::names::BusName<'static>> = &Some(DESTINATION);
19
    const PATH: &'static Option<ObjectPath<'static>> = &None;
20
}
21

            
22
impl<'a> From<zbus::Proxy<'a>> for Session<'a> {
23
5
    fn from(value: zbus::Proxy<'a>) -> Self {
24
        Self(value)
25
    }
26
}
27

            
28
impl<'a> Session<'a> {
29
13
    pub async fn new<P>(connection: &zbus::Connection, object_path: P) -> Result<Session<'a>, Error>
30
    where
31
        P: TryInto<ObjectPath<'a>>,
32
        P::Error: Into<zbus::Error>,
33
    {
34
28
        zbus::proxy::Builder::new(connection)
35
7
            .path(object_path)?
36
            .build()
37
21
            .await
38
7
            .map_err(From::from)
39
    }
40

            
41
4
    pub fn inner(&self) -> &zbus::Proxy<'_> {
42
        &self.0
43
    }
44

            
45
8
    pub async fn close(&self) -> Result<(), Error> {
46
8
        self.inner()
47
2
            .call_method("Close", &())
48
8
            .await
49
4
            .map_err::<ServiceError, _>(From::from)?;
50
2
        Ok(())
51
    }
52
}
53

            
54
impl Serialize for Session<'_> {
55
8
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
56
    where
57
        S: serde::Serializer,
58
    {
59
16
        ObjectPath::serialize(self.inner().path(), serializer)
60
    }
61
}
62

            
63
impl fmt::Debug for Session<'_> {
64
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65
        f.debug_tuple("Session")
66
            .field(&self.inner().path().as_str())
67
            .finish()
68
    }
69
}