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
use {Document, Error, IntoDatabasePath, IntoDocumentPath, IntoViewPath, Revision, action, serde, url};
use transport::HyperTransport;
pub trait IntoUrl {
fn into_url(self) -> Result<url::Url, Error>;
}
impl IntoUrl for url::Url {
fn into_url(self) -> Result<url::Url, Error> {
Ok(self)
}
}
impl<'a> IntoUrl for &'a str {
fn into_url(self) -> Result<url::Url, Error> {
url::Url::parse(self).map_err(|e| Error::UrlParse { cause: e })
}
}
impl<'a> IntoUrl for &'a String {
fn into_url(self) -> Result<url::Url, Error> {
url::Url::parse(self).map_err(|e| Error::UrlParse { cause: e })
}
}
#[derive(Debug)]
pub struct Client {
transport: HyperTransport,
}
impl Client {
pub fn new<U: IntoUrl>(server_url: U) -> Result<Self, Error> {
let server_url = try!(server_url.into_url());
let transport = HyperTransport::new(server_url);
Ok((Client { transport: transport }))
}
pub fn create_database<'a, P>(&'a self, db_path: P) -> action::CreateDatabase<'a, HyperTransport, P>
where P: IntoDatabasePath
{
action::CreateDatabase::new(&self.transport, db_path)
}
pub fn create_document<'a, C, P>(&'a self,
db_path: P,
content: &'a C)
-> action::CreateDocument<'a, HyperTransport, P, C>
where C: serde::Serialize,
P: IntoDatabasePath
{
action::CreateDocument::new(&self.transport, db_path, content)
}
pub fn read_document<'a, P>(&'a self, doc_path: P) -> action::ReadDocument<'a, HyperTransport, P>
where P: IntoDocumentPath
{
action::ReadDocument::new(&self.transport, doc_path)
}
pub fn update_document<'a>(&'a self, doc: &'a Document) -> action::UpdateDocument<'a, HyperTransport> {
action::UpdateDocument::new(&self.transport, doc)
}
pub fn delete_document<'a, P>(&'a self,
doc_path: P,
revision: &'a Revision)
-> action::DeleteDocument<'a, HyperTransport, P>
where P: IntoDocumentPath
{
action::DeleteDocument::new(&self.transport, doc_path, revision)
}
pub fn execute_view<'a, P>(&'a self, view_path: P) -> action::ExecuteView<'a, HyperTransport, P, (), ()>
where P: IntoViewPath
{
action::ExecuteView::new(&self.transport, view_path)
}
}