pub struct ResourceBuilder { /* private fields */ }Implementations§
Source§impl ResourceBuilder
impl ResourceBuilder
pub fn new<T: ToString>(type_: T) -> Self
Sourcepub fn new_with_id<T: ToString, I: ToString>(type_: T, id: I) -> Self
pub fn new_with_id<T: ToString, I: ToString>(type_: T, id: I) -> Self
Examples found in repository?
examples/builders.rs (line 16)
4fn main() {
5 let document = jsonapis::DocumentBuilder::default()
6 .jsonapi(jsonapis::Version::new(0))
7 .meta1("current_page", 1)
8 .meta1("items_per_page", 2)
9 .meta1("total_pages", 3)
10 .meta1("total_items", 6)
11 .link("self", "http://example.com/posts.json?page=1")
12 .link("first", "http://example.com/posts.json?page=1")
13 .link("last", "http://example.com/posts.json?page=3")
14 .link("next", "http://example.com/posts.json?page=2")
15 .data(vec![
16 jsonapis::ResourceBuilder::new_with_id("posts", "1")
17 .link("self", "http://example.com/posts/1.json")
18 .attr("title", "Some blog post")
19 .attr("summary", "Here is the beginning of some blog post.")
20 .rel(
21 "author",
22 jsonapis::ResourceBuilder::new_with_id("users", "1")
23 .link("self", "http://example.com/users/1.json")
24 .attr("username", "alice"),
25 ),
26 jsonapis::ResourceBuilder::new_with_id("posts", "2")
27 .link("self", "http://example.com/posts/2.json")
28 .attr("title", "Other blog post")
29 .attr("summary", "Here is the beginning of other blog post.")
30 .rel(
31 "author",
32 jsonapis::ResourceBuilder::new_with_id("users", "2")
33 .link("self", "http://example.com/users/2.json")
34 .attr("username", "bob"),
35 ),
36 ])
37 .unwrap();
38
39 let expected_value = json!({
40 "jsonapi": json!({
41 "version": json!("1.0"),
42 "meta": json!(null),
43 }),
44 "meta": json!({
45 "current_page": json!(1),
46 "items_per_page": json!(2),
47 "total_pages": json!(3),
48 "total_items": json!(6),
49 }),
50 "links": json!({
51 "self": json!("http://example.com/posts.json?page=1"),
52 "related": json!(null),
53 "first": json!("http://example.com/posts.json?page=1"),
54 "last": json!("http://example.com/posts.json?page=3"),
55 "prev": json!(null),
56 "next": json!("http://example.com/posts.json?page=2"),
57 "about": json!(null),
58 }),
59 "data": json!([
60 json!({
61 "type": json!("posts"),
62 "id": json!("1"),
63 "meta": json!(null),
64 "links": json!({
65 "self": json!("http://example.com/posts/1.json"),
66 "related": json!(null),
67 "first": json!(null),
68 "last": json!(null),
69 "prev": json!(null),
70 "next": json!(null),
71 "about": json!(null),
72 }),
73 "attributes": json!({
74 "title": json!("Some blog post"),
75 "summary": json!("Here is the beginning of some blog post."),
76 }),
77 "relationships": json!({
78 "author": json!({
79 "meta": json!(null),
80 "links": json!(null),
81 "data": json!({
82 "type": json!("users"),
83 "id": json!("1"),
84 "meta": json!(null),
85 "links": json!({
86 "self": json!("http://example.com/users/1.json"),
87 "related": json!(null),
88 "first": json!(null),
89 "last": json!(null),
90 "prev": json!(null),
91 "next": json!(null),
92 "about": json!(null),
93 }),
94 "attributes": json!({
95 "username": json!("alice"),
96 }),
97 "relationships": json!(null),
98 }),
99 }),
100 }),
101 }),
102 json!({
103 "type": json!("posts"),
104 "id": json!("2"),
105 "meta": json!(null),
106 "links": json!({
107 "self": json!("http://example.com/posts/2.json"),
108 "related": json!(null),
109 "first": json!(null),
110 "last": json!(null),
111 "prev": json!(null),
112 "next": json!(null),
113 "about": json!(null),
114 }),
115 "attributes": json!({
116 "title": json!("Other blog post"),
117 "summary": json!("Here is the beginning of other blog post."),
118 }),
119 "relationships": json!({
120 "author": json!({
121 "meta": json!(null),
122 "links": json!(null),
123 "data": json!({
124 "type": json!("users"),
125 "id": json!("2"),
126 "meta": json!(null),
127 "links": json!({
128 "self": json!("http://example.com/users/2.json"),
129 "related": json!(null),
130 "first": json!(null),
131 "last": json!(null),
132 "prev": json!(null),
133 "next": json!(null),
134 "about": json!(null),
135 }),
136 "attributes": json!({
137 "username": json!("bob"),
138 }),
139 "relationships": json!(null),
140 }),
141 }),
142 }),
143 }),
144 ]),
145 "errors": json!(null),
146 });
147
148 let actual_json = serde_json::to_string(&document).unwrap();
149
150 let actual_value: Value = serde_json::from_str(&actual_json).unwrap();
151
152 println!("{:#?}", actual_value);
153
154 assert_eq!(actual_value, expected_value);
155}Source§impl ResourceBuilder
impl ResourceBuilder
pub fn id<I: ToString>(self, id: I) -> Self
pub fn meta<M: Into<MetaOrAttrsBuilder>>(self, meta: M) -> Self
pub fn links<L: Into<LinksBuilder>>(self, links: L) -> Self
pub fn attributes<M: Into<MetaOrAttrsBuilder>>(self, attributes: M) -> Self
pub fn relationships<R: Into<RelationshipsBuilder>>( self, relationships: R, ) -> Self
pub fn meta1<N: ToString, V: Into<Value>>(self, name: N, meta1: V) -> Self
Sourcepub fn link<N: ToString, L: Into<LinkBuilder>>(self, name: N, link: L) -> Self
pub fn link<N: ToString, L: Into<LinkBuilder>>(self, name: N, link: L) -> Self
Examples found in repository?
examples/builders.rs (line 17)
4fn main() {
5 let document = jsonapis::DocumentBuilder::default()
6 .jsonapi(jsonapis::Version::new(0))
7 .meta1("current_page", 1)
8 .meta1("items_per_page", 2)
9 .meta1("total_pages", 3)
10 .meta1("total_items", 6)
11 .link("self", "http://example.com/posts.json?page=1")
12 .link("first", "http://example.com/posts.json?page=1")
13 .link("last", "http://example.com/posts.json?page=3")
14 .link("next", "http://example.com/posts.json?page=2")
15 .data(vec![
16 jsonapis::ResourceBuilder::new_with_id("posts", "1")
17 .link("self", "http://example.com/posts/1.json")
18 .attr("title", "Some blog post")
19 .attr("summary", "Here is the beginning of some blog post.")
20 .rel(
21 "author",
22 jsonapis::ResourceBuilder::new_with_id("users", "1")
23 .link("self", "http://example.com/users/1.json")
24 .attr("username", "alice"),
25 ),
26 jsonapis::ResourceBuilder::new_with_id("posts", "2")
27 .link("self", "http://example.com/posts/2.json")
28 .attr("title", "Other blog post")
29 .attr("summary", "Here is the beginning of other blog post.")
30 .rel(
31 "author",
32 jsonapis::ResourceBuilder::new_with_id("users", "2")
33 .link("self", "http://example.com/users/2.json")
34 .attr("username", "bob"),
35 ),
36 ])
37 .unwrap();
38
39 let expected_value = json!({
40 "jsonapi": json!({
41 "version": json!("1.0"),
42 "meta": json!(null),
43 }),
44 "meta": json!({
45 "current_page": json!(1),
46 "items_per_page": json!(2),
47 "total_pages": json!(3),
48 "total_items": json!(6),
49 }),
50 "links": json!({
51 "self": json!("http://example.com/posts.json?page=1"),
52 "related": json!(null),
53 "first": json!("http://example.com/posts.json?page=1"),
54 "last": json!("http://example.com/posts.json?page=3"),
55 "prev": json!(null),
56 "next": json!("http://example.com/posts.json?page=2"),
57 "about": json!(null),
58 }),
59 "data": json!([
60 json!({
61 "type": json!("posts"),
62 "id": json!("1"),
63 "meta": json!(null),
64 "links": json!({
65 "self": json!("http://example.com/posts/1.json"),
66 "related": json!(null),
67 "first": json!(null),
68 "last": json!(null),
69 "prev": json!(null),
70 "next": json!(null),
71 "about": json!(null),
72 }),
73 "attributes": json!({
74 "title": json!("Some blog post"),
75 "summary": json!("Here is the beginning of some blog post."),
76 }),
77 "relationships": json!({
78 "author": json!({
79 "meta": json!(null),
80 "links": json!(null),
81 "data": json!({
82 "type": json!("users"),
83 "id": json!("1"),
84 "meta": json!(null),
85 "links": json!({
86 "self": json!("http://example.com/users/1.json"),
87 "related": json!(null),
88 "first": json!(null),
89 "last": json!(null),
90 "prev": json!(null),
91 "next": json!(null),
92 "about": json!(null),
93 }),
94 "attributes": json!({
95 "username": json!("alice"),
96 }),
97 "relationships": json!(null),
98 }),
99 }),
100 }),
101 }),
102 json!({
103 "type": json!("posts"),
104 "id": json!("2"),
105 "meta": json!(null),
106 "links": json!({
107 "self": json!("http://example.com/posts/2.json"),
108 "related": json!(null),
109 "first": json!(null),
110 "last": json!(null),
111 "prev": json!(null),
112 "next": json!(null),
113 "about": json!(null),
114 }),
115 "attributes": json!({
116 "title": json!("Other blog post"),
117 "summary": json!("Here is the beginning of other blog post."),
118 }),
119 "relationships": json!({
120 "author": json!({
121 "meta": json!(null),
122 "links": json!(null),
123 "data": json!({
124 "type": json!("users"),
125 "id": json!("2"),
126 "meta": json!(null),
127 "links": json!({
128 "self": json!("http://example.com/users/2.json"),
129 "related": json!(null),
130 "first": json!(null),
131 "last": json!(null),
132 "prev": json!(null),
133 "next": json!(null),
134 "about": json!(null),
135 }),
136 "attributes": json!({
137 "username": json!("bob"),
138 }),
139 "relationships": json!(null),
140 }),
141 }),
142 }),
143 }),
144 ]),
145 "errors": json!(null),
146 });
147
148 let actual_json = serde_json::to_string(&document).unwrap();
149
150 let actual_value: Value = serde_json::from_str(&actual_json).unwrap();
151
152 println!("{:#?}", actual_value);
153
154 assert_eq!(actual_value, expected_value);
155}Sourcepub fn attr<N: ToString, V: Into<Value>>(self, name: N, attribute: V) -> Self
pub fn attr<N: ToString, V: Into<Value>>(self, name: N, attribute: V) -> Self
Examples found in repository?
examples/builders.rs (line 18)
4fn main() {
5 let document = jsonapis::DocumentBuilder::default()
6 .jsonapi(jsonapis::Version::new(0))
7 .meta1("current_page", 1)
8 .meta1("items_per_page", 2)
9 .meta1("total_pages", 3)
10 .meta1("total_items", 6)
11 .link("self", "http://example.com/posts.json?page=1")
12 .link("first", "http://example.com/posts.json?page=1")
13 .link("last", "http://example.com/posts.json?page=3")
14 .link("next", "http://example.com/posts.json?page=2")
15 .data(vec![
16 jsonapis::ResourceBuilder::new_with_id("posts", "1")
17 .link("self", "http://example.com/posts/1.json")
18 .attr("title", "Some blog post")
19 .attr("summary", "Here is the beginning of some blog post.")
20 .rel(
21 "author",
22 jsonapis::ResourceBuilder::new_with_id("users", "1")
23 .link("self", "http://example.com/users/1.json")
24 .attr("username", "alice"),
25 ),
26 jsonapis::ResourceBuilder::new_with_id("posts", "2")
27 .link("self", "http://example.com/posts/2.json")
28 .attr("title", "Other blog post")
29 .attr("summary", "Here is the beginning of other blog post.")
30 .rel(
31 "author",
32 jsonapis::ResourceBuilder::new_with_id("users", "2")
33 .link("self", "http://example.com/users/2.json")
34 .attr("username", "bob"),
35 ),
36 ])
37 .unwrap();
38
39 let expected_value = json!({
40 "jsonapi": json!({
41 "version": json!("1.0"),
42 "meta": json!(null),
43 }),
44 "meta": json!({
45 "current_page": json!(1),
46 "items_per_page": json!(2),
47 "total_pages": json!(3),
48 "total_items": json!(6),
49 }),
50 "links": json!({
51 "self": json!("http://example.com/posts.json?page=1"),
52 "related": json!(null),
53 "first": json!("http://example.com/posts.json?page=1"),
54 "last": json!("http://example.com/posts.json?page=3"),
55 "prev": json!(null),
56 "next": json!("http://example.com/posts.json?page=2"),
57 "about": json!(null),
58 }),
59 "data": json!([
60 json!({
61 "type": json!("posts"),
62 "id": json!("1"),
63 "meta": json!(null),
64 "links": json!({
65 "self": json!("http://example.com/posts/1.json"),
66 "related": json!(null),
67 "first": json!(null),
68 "last": json!(null),
69 "prev": json!(null),
70 "next": json!(null),
71 "about": json!(null),
72 }),
73 "attributes": json!({
74 "title": json!("Some blog post"),
75 "summary": json!("Here is the beginning of some blog post."),
76 }),
77 "relationships": json!({
78 "author": json!({
79 "meta": json!(null),
80 "links": json!(null),
81 "data": json!({
82 "type": json!("users"),
83 "id": json!("1"),
84 "meta": json!(null),
85 "links": json!({
86 "self": json!("http://example.com/users/1.json"),
87 "related": json!(null),
88 "first": json!(null),
89 "last": json!(null),
90 "prev": json!(null),
91 "next": json!(null),
92 "about": json!(null),
93 }),
94 "attributes": json!({
95 "username": json!("alice"),
96 }),
97 "relationships": json!(null),
98 }),
99 }),
100 }),
101 }),
102 json!({
103 "type": json!("posts"),
104 "id": json!("2"),
105 "meta": json!(null),
106 "links": json!({
107 "self": json!("http://example.com/posts/2.json"),
108 "related": json!(null),
109 "first": json!(null),
110 "last": json!(null),
111 "prev": json!(null),
112 "next": json!(null),
113 "about": json!(null),
114 }),
115 "attributes": json!({
116 "title": json!("Other blog post"),
117 "summary": json!("Here is the beginning of other blog post."),
118 }),
119 "relationships": json!({
120 "author": json!({
121 "meta": json!(null),
122 "links": json!(null),
123 "data": json!({
124 "type": json!("users"),
125 "id": json!("2"),
126 "meta": json!(null),
127 "links": json!({
128 "self": json!("http://example.com/users/2.json"),
129 "related": json!(null),
130 "first": json!(null),
131 "last": json!(null),
132 "prev": json!(null),
133 "next": json!(null),
134 "about": json!(null),
135 }),
136 "attributes": json!({
137 "username": json!("bob"),
138 }),
139 "relationships": json!(null),
140 }),
141 }),
142 }),
143 }),
144 ]),
145 "errors": json!(null),
146 });
147
148 let actual_json = serde_json::to_string(&document).unwrap();
149
150 let actual_value: Value = serde_json::from_str(&actual_json).unwrap();
151
152 println!("{:#?}", actual_value);
153
154 assert_eq!(actual_value, expected_value);
155}Sourcepub fn rel<N: ToString, R: Into<RelationshipBuilder>>(
self,
name: N,
relationship: R,
) -> Self
pub fn rel<N: ToString, R: Into<RelationshipBuilder>>( self, name: N, relationship: R, ) -> Self
Examples found in repository?
examples/builders.rs (lines 20-25)
4fn main() {
5 let document = jsonapis::DocumentBuilder::default()
6 .jsonapi(jsonapis::Version::new(0))
7 .meta1("current_page", 1)
8 .meta1("items_per_page", 2)
9 .meta1("total_pages", 3)
10 .meta1("total_items", 6)
11 .link("self", "http://example.com/posts.json?page=1")
12 .link("first", "http://example.com/posts.json?page=1")
13 .link("last", "http://example.com/posts.json?page=3")
14 .link("next", "http://example.com/posts.json?page=2")
15 .data(vec![
16 jsonapis::ResourceBuilder::new_with_id("posts", "1")
17 .link("self", "http://example.com/posts/1.json")
18 .attr("title", "Some blog post")
19 .attr("summary", "Here is the beginning of some blog post.")
20 .rel(
21 "author",
22 jsonapis::ResourceBuilder::new_with_id("users", "1")
23 .link("self", "http://example.com/users/1.json")
24 .attr("username", "alice"),
25 ),
26 jsonapis::ResourceBuilder::new_with_id("posts", "2")
27 .link("self", "http://example.com/posts/2.json")
28 .attr("title", "Other blog post")
29 .attr("summary", "Here is the beginning of other blog post.")
30 .rel(
31 "author",
32 jsonapis::ResourceBuilder::new_with_id("users", "2")
33 .link("self", "http://example.com/users/2.json")
34 .attr("username", "bob"),
35 ),
36 ])
37 .unwrap();
38
39 let expected_value = json!({
40 "jsonapi": json!({
41 "version": json!("1.0"),
42 "meta": json!(null),
43 }),
44 "meta": json!({
45 "current_page": json!(1),
46 "items_per_page": json!(2),
47 "total_pages": json!(3),
48 "total_items": json!(6),
49 }),
50 "links": json!({
51 "self": json!("http://example.com/posts.json?page=1"),
52 "related": json!(null),
53 "first": json!("http://example.com/posts.json?page=1"),
54 "last": json!("http://example.com/posts.json?page=3"),
55 "prev": json!(null),
56 "next": json!("http://example.com/posts.json?page=2"),
57 "about": json!(null),
58 }),
59 "data": json!([
60 json!({
61 "type": json!("posts"),
62 "id": json!("1"),
63 "meta": json!(null),
64 "links": json!({
65 "self": json!("http://example.com/posts/1.json"),
66 "related": json!(null),
67 "first": json!(null),
68 "last": json!(null),
69 "prev": json!(null),
70 "next": json!(null),
71 "about": json!(null),
72 }),
73 "attributes": json!({
74 "title": json!("Some blog post"),
75 "summary": json!("Here is the beginning of some blog post."),
76 }),
77 "relationships": json!({
78 "author": json!({
79 "meta": json!(null),
80 "links": json!(null),
81 "data": json!({
82 "type": json!("users"),
83 "id": json!("1"),
84 "meta": json!(null),
85 "links": json!({
86 "self": json!("http://example.com/users/1.json"),
87 "related": json!(null),
88 "first": json!(null),
89 "last": json!(null),
90 "prev": json!(null),
91 "next": json!(null),
92 "about": json!(null),
93 }),
94 "attributes": json!({
95 "username": json!("alice"),
96 }),
97 "relationships": json!(null),
98 }),
99 }),
100 }),
101 }),
102 json!({
103 "type": json!("posts"),
104 "id": json!("2"),
105 "meta": json!(null),
106 "links": json!({
107 "self": json!("http://example.com/posts/2.json"),
108 "related": json!(null),
109 "first": json!(null),
110 "last": json!(null),
111 "prev": json!(null),
112 "next": json!(null),
113 "about": json!(null),
114 }),
115 "attributes": json!({
116 "title": json!("Other blog post"),
117 "summary": json!("Here is the beginning of other blog post."),
118 }),
119 "relationships": json!({
120 "author": json!({
121 "meta": json!(null),
122 "links": json!(null),
123 "data": json!({
124 "type": json!("users"),
125 "id": json!("2"),
126 "meta": json!(null),
127 "links": json!({
128 "self": json!("http://example.com/users/2.json"),
129 "related": json!(null),
130 "first": json!(null),
131 "last": json!(null),
132 "prev": json!(null),
133 "next": json!(null),
134 "about": json!(null),
135 }),
136 "attributes": json!({
137 "username": json!("bob"),
138 }),
139 "relationships": json!(null),
140 }),
141 }),
142 }),
143 }),
144 ]),
145 "errors": json!(null),
146 });
147
148 let actual_json = serde_json::to_string(&document).unwrap();
149
150 let actual_value: Value = serde_json::from_str(&actual_json).unwrap();
151
152 println!("{:#?}", actual_value);
153
154 assert_eq!(actual_value, expected_value);
155}Trait Implementations§
Source§impl Builder<'_> for ResourceBuilder
impl Builder<'_> for ResourceBuilder
Source§impl Clone for ResourceBuilder
impl Clone for ResourceBuilder
Source§fn clone(&self) -> ResourceBuilder
fn clone(&self) -> ResourceBuilder
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ResourceBuilder
impl Debug for ResourceBuilder
Source§impl From<Resource> for ResourceBuilder
impl From<Resource> for ResourceBuilder
Source§impl PartialEq for ResourceBuilder
impl PartialEq for ResourceBuilder
impl Eq for ResourceBuilder
impl StructuralPartialEq for ResourceBuilder
Auto Trait Implementations§
impl Freeze for ResourceBuilder
impl RefUnwindSafe for ResourceBuilder
impl Send for ResourceBuilder
impl Sync for ResourceBuilder
impl Unpin for ResourceBuilder
impl UnwindSafe for ResourceBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.