1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::{io::Write, ops::DerefMut};
use bstr::BString;
use crate::{
client::{Capabilities, Error, ExtendedBufRead, MessageKind, TransportWithoutIO, WriteMode},
Protocol, Service,
};
pub struct SetServiceResponse<'a> {
pub actual_protocol: Protocol,
pub capabilities: Capabilities,
pub refs: Option<Box<dyn crate::client::ReadlineBufRead + 'a>>,
}
pub trait Transport: TransportWithoutIO {
fn handshake<'a>(
&mut self,
service: Service,
extra_parameters: &'a [(&'a str, Option<&'a str>)],
) -> Result<SetServiceResponse<'_>, Error>;
}
impl<T: Transport + ?Sized> Transport for Box<T> {
fn handshake<'a>(
&mut self,
service: Service,
extra_parameters: &'a [(&'a str, Option<&'a str>)],
) -> Result<SetServiceResponse<'_>, Error> {
self.deref_mut().handshake(service, extra_parameters)
}
}
impl<T: Transport + ?Sized> Transport for &mut T {
fn handshake<'a>(
&mut self,
service: Service,
extra_parameters: &'a [(&'a str, Option<&'a str>)],
) -> Result<SetServiceResponse<'_>, Error> {
self.deref_mut().handshake(service, extra_parameters)
}
}
pub trait TransportV2Ext {
fn invoke<'a>(
&mut self,
command: &str,
capabilities: impl Iterator<Item = (&'a str, Option<impl AsRef<str>>)> + 'a,
arguments: Option<impl Iterator<Item = bstr::BString>>,
) -> Result<Box<dyn ExtendedBufRead + Unpin + '_>, Error>;
}
impl<T: Transport> TransportV2Ext for T {
fn invoke<'a>(
&mut self,
command: &str,
capabilities: impl Iterator<Item = (&'a str, Option<impl AsRef<str>>)> + 'a,
arguments: Option<impl Iterator<Item = BString>>,
) -> Result<Box<dyn ExtendedBufRead + Unpin + '_>, Error> {
let mut writer = self.request(WriteMode::OneLfTerminatedLinePerWriteCall, MessageKind::Flush)?;
writer.write_all(format!("command={}", command).as_bytes())?;
for (name, value) in capabilities {
match value {
Some(value) => writer.write_all(format!("{}={}", name, value.as_ref()).as_bytes()),
None => writer.write_all(name.as_bytes()),
}?;
}
if let Some(arguments) = arguments {
writer.write_message(MessageKind::Delimiter)?;
for argument in arguments {
writer.write_all(argument.as_ref())?;
}
}
Ok(writer.into_read()?)
}
}