etcd_rs/lease/
grant.rs

1use std::time::Duration;
2
3use crate::lease::LeaseId;
4use crate::proto::etcdserverpb;
5use crate::ResponseHeader;
6
7#[derive(Debug)]
8pub struct LeaseGrantRequest {
9    proto: crate::proto::etcdserverpb::LeaseGrantRequest,
10}
11
12impl LeaseGrantRequest {
13    /// Creates a new LeaseGrantRequest with the specified TTL.
14    pub fn new(ttl: Duration) -> Self {
15        Self {
16            proto: etcdserverpb::LeaseGrantRequest {
17                ttl: ttl.as_secs() as i64,
18                id: 0,
19            },
20        }
21    }
22
23    /// Set custom lease ID.
24    pub fn with_id(mut self, id: LeaseId) -> Self {
25        self.proto.id = id as LeaseId;
26        self
27    }
28}
29
30impl From<LeaseGrantRequest> for crate::proto::etcdserverpb::LeaseGrantRequest {
31    fn from(x: LeaseGrantRequest) -> Self {
32        x.proto
33    }
34}
35
36impl From<Duration> for LeaseGrantRequest {
37    fn from(ttl: Duration) -> Self {
38        Self::new(ttl)
39    }
40}
41
42#[derive(Debug, Clone)]
43pub struct LeaseGrantResponse {
44    pub header: ResponseHeader,
45    pub id: LeaseId,
46    pub ttl: i64,
47}
48
49impl From<crate::proto::etcdserverpb::LeaseGrantResponse> for LeaseGrantResponse {
50    fn from(proto: crate::proto::etcdserverpb::LeaseGrantResponse) -> Self {
51        Self {
52            header: From::from(proto.header.expect("must fetch header")),
53            id: proto.id,
54            ttl: proto.ttl,
55        }
56    }
57}