twitter_stream_message/entities.rs
1//! Entities
2
3use std::borrow::Cow;
4
5use tweet::StatusId;
6use user::UserId;
7
8pub type MediaId = u64;
9
10/// Represents Entities.
11///
12/// # Reference
13///
14/// 1. [Entities — Twitter Developers][1]
15/// 1. [Entities in Objects — Twitter Developers][2]
16///
17/// [1]: https://dev.twitter.com/overview/api/entities
18/// [2]: https://dev.twitter.com/overview/api/entities-in-twitter-objects
19#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
20pub struct Entities<'a> {
21 /// Represents hashtags which have been parsed out of the Tweet text.
22 #[serde(borrow)]
23 pub hashtags: Vec<Hashtag<'a>>,
24
25 /// Represents media elements uploaded with the Tweet.
26 #[serde(borrow)]
27 pub media: Option<Vec<Media<'a>>>,
28
29 /// Represents URLs included in the `text` of a `Tweet`
30 /// or within textual fields of a `User` object.
31 #[serde(borrow)]
32 pub urls: Vec<Url<'a>>,
33
34 /// Represents other Twitter users mentioned in the `text` of the `Tweet`.
35 #[serde(borrow)]
36 pub user_mentions: Vec<UserMention<'a>>,
37
38 /// Represents financial symbols which have been parsed out of
39 /// the Tweet text.
40 #[serde(borrow)]
41 pub symbols: Vec<Symbol<'a>>,
42}
43
44/// Represents a hashtag in `hashtags` field of `Entities`.
45#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
46pub struct Hashtag<'a> {
47 /// A pair of integers indicating the offsets within the Tweet text
48 /// where the hashtag begins and ends. The first integer represents
49 /// the location of the `#` character in the Tweet text string.
50 /// The second integer represents the location of the first character
51 /// after the hashtag. Therefore the difference between the two numbers
52 /// will be the length of the hashtag name plus one (for the `#` character).
53 pub indices: (u64, u64),
54
55 /// Name of the hashtag, minus the leading `#` character.
56 #[serde(borrow)]
57 pub text: Cow<'a, str>,
58}
59
60/// Represents `media` field in `Entities`.
61#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
62pub struct Media<'a> {
63 /// URL of the media to display to clients.
64 #[serde(borrow)]
65 pub display_url: Cow<'a, str>,
66
67 /// An expanded version of `display_url`. Links to the media display page.
68 #[serde(borrow)]
69 pub expanded_url: Cow<'a, str>,
70
71 /// ID of the media expressed as a 64-bit integer.
72 pub id: MediaId,
73
74 // pub id_str: String,
75
76 /// A pair of integers indicating the offsets within the Tweet text
77 /// where the URL begins and ends. The first integer represents
78 /// the location of the first character of the URL in the Tweet text.
79 /// The second integer represents the location of the first non-URL
80 /// character occurring after the URL (or the end of the string
81 /// if the URL is the last part of the Tweet text).
82 pub indices: (u64, u64),
83
84 /// An http:// URL pointing directly to the uploaded media file.
85 #[serde(borrow)]
86 pub media_url: Cow<'a, str>,
87
88 /// An https:// URL pointing directly to the uploaded media file,
89 /// for embedding on https pages.
90 ///
91 /// For media in direct messages, `media_url_https` must be accessed
92 /// via an authenticated twitter.com session or by signing a request
93 /// with the user’s access token using OAuth 1.0A.
94 /// It is not possible to directly embed these images in a web page.
95 #[serde(borrow)]
96 pub media_url_https: Cow<'a, str>,
97
98 /// An object showing available sizes for the media file.
99 #[serde(borrow)]
100 pub sizes: Sizes<'a>,
101
102 /// For Tweets containing media that was originally associated with
103 /// a different tweet, this ID points to the original Tweet.
104 pub source_status_id: Option<StatusId>,
105
106 // source_status_id_str: String,
107
108 /// Type of uploaded media.
109 #[serde(borrow)]
110 #[serde(rename="type")]
111 pub kind: Cow<'a, str>,
112
113 /// Wrapped URL for the media link. This corresponds with the URL
114 /// embedded directly into the raw Tweet text,
115 /// and the values for the `indices` parameter.
116 #[serde(borrow)]
117 pub url: Cow<'a, str>,
118}
119
120/// Represents the `sizes` field in `Media`.
121#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
122pub struct Sizes<'a> {
123 #[serde(borrow)]
124 pub thumb: Size<'a>,
125
126 #[serde(borrow)]
127 pub large: Size<'a>,
128
129 #[serde(borrow)]
130 pub medium: Size<'a>,
131
132 #[serde(borrow)]
133 pub small: Size<'a>,
134}
135
136#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
137pub struct Size<'a> {
138 /// Height in pixels of this size.
139 pub h: u64,
140
141 /// Resizing method used to obtain this size.
142 #[serde(borrow)]
143 pub resize: Resize<'a>,
144
145 /// Width in pixels of this size.
146 pub w: u64,
147}
148
149string_enums! {
150 /// Represents the `resize` field in `Size`.
151 #[derive(Clone, Debug)]
152 pub enum Resize<'a> {
153 /// The media was resized to fit one dimension,
154 /// keeping its native aspect ratio.
155 Fit("fit"),
156 /// The media was cropped in order to fit a specific resolution.
157 Crop("crop");
158 Custom(_),
159 }
160}
161
162/// Represents a URL in `urls` field of `Entities`.
163#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
164pub struct Url<'a> {
165 /// Version of the URL to display to clients.
166 #[serde(borrow)]
167 #[serde(default)]
168 // nullable in Retweets.
169 #[serde(deserialize_with = "::util::deserialize_default")]
170 pub display_url: Cow<'a, str>,
171
172 /// Expanded version of `display_url`.
173 #[serde(borrow)]
174 #[serde(default)]
175 // nullable in Retweets.
176 #[serde(deserialize_with = "::util::deserialize_default")]
177 pub expanded_url: Cow<'a, str>,
178
179 /// A pair of integers representing offsets within the Tweet text
180 /// where the URL begins and ends. The first integer represents
181 /// the location of the first character of the URL in the Tweet text.
182 /// The second integer represents the location of the first non-URL
183 /// character after the end of the URL.
184 pub indices: (u64, u64),
185
186 /// Wrapped URL, corresponding to the value embedded directly into
187 /// the raw Tweet text, and the values for the `indices` parameter.
188 #[serde(borrow)]
189 pub url: Cow<'a, str>,
190}
191
192/// Represents a user in `user_mentions` field of `Entities`.
193#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
194pub struct UserMention<'a> {
195 /// ID of the mentioned user, as an integer.
196 pub id: UserId,
197
198 // pub id_str: String,
199
200 /// A pair of integers representing the offsets within the Tweet text
201 /// where the user reference begins and ends. The first integer represents
202 /// the location of the ‘@’ character of the user mention.
203 /// The second integer represents the location of the first non-screenname
204 /// character following the user mention.
205 pub indices: (u64, u64),
206
207 /// Display name of the referenced user.
208 #[serde(borrow)]
209 pub name: Cow<'a, str>,
210
211 /// Screen name of the referenced user.
212 #[serde(borrow)]
213 pub screen_name: Cow<'a, str>,
214}
215
216/// Represents a financial symbol in `symbols` field of `Entities`.
217#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)]
218pub struct Symbol<'a> {
219 #[serde(borrow)]
220 pub text: Cow<'a, str>,
221 pub indices: (u64, u64),
222}