Skip to main content

profile_twitter/
profile_twitter.rs

1//! Profile twitter.json parsing (for callgrind, etc.)
2//!
3//! cargo build --profile profiling -p facet-json --example profile_twitter
4//! valgrind --tool=callgrind ./target/profiling/examples/profile_twitter
5
6use facet::Facet;
7use facet_json::from_str;
8use std::hint::black_box;
9
10#[derive(Debug, Facet)]
11struct Twitter {
12    statuses: Vec<Status>,
13    search_metadata: SearchMetadata,
14}
15
16#[derive(Debug, Facet)]
17struct SearchMetadata {
18    completed_in: f64,
19    max_id: u64,
20    max_id_str: String,
21    next_results: String,
22    query: String,
23    refresh_url: String,
24    count: u64,
25    since_id: u64,
26    since_id_str: String,
27}
28
29#[derive(Debug, Facet)]
30struct Status {
31    metadata: Metadata,
32    created_at: String,
33    id: u64,
34    id_str: String,
35    text: String,
36    source: String,
37    truncated: bool,
38    in_reply_to_status_id: Option<u64>,
39    in_reply_to_status_id_str: Option<String>,
40    in_reply_to_user_id: Option<u64>,
41    in_reply_to_user_id_str: Option<String>,
42    in_reply_to_screen_name: Option<String>,
43    user: User,
44    geo: Option<()>,
45    coordinates: Option<()>,
46    place: Option<()>,
47    contributors: Option<()>,
48    retweet_count: u64,
49    favorite_count: u64,
50    entities: Entities,
51    favorited: bool,
52    retweeted: bool,
53    lang: String,
54    #[facet(default, recursive_type)]
55    retweeted_status: Option<Box<Status>>,
56    #[facet(default)]
57    possibly_sensitive: Option<bool>,
58}
59
60#[derive(Debug, Facet)]
61struct Metadata {
62    result_type: String,
63    iso_language_code: String,
64}
65
66#[derive(Debug, Facet)]
67struct User {
68    id: u64,
69    id_str: String,
70    name: String,
71    screen_name: String,
72    location: String,
73    description: String,
74    url: Option<String>,
75    entities: UserEntities,
76    protected: bool,
77    followers_count: u64,
78    friends_count: u64,
79    listed_count: u64,
80    created_at: String,
81    favourites_count: u64,
82    utc_offset: Option<i64>,
83    time_zone: Option<String>,
84    geo_enabled: bool,
85    verified: bool,
86    statuses_count: u64,
87    lang: String,
88    contributors_enabled: bool,
89    is_translator: bool,
90    is_translation_enabled: bool,
91    profile_background_color: String,
92    profile_background_image_url: String,
93    profile_background_image_url_https: String,
94    profile_background_tile: bool,
95    profile_image_url: String,
96    profile_image_url_https: String,
97    #[facet(default)]
98    profile_banner_url: Option<String>,
99    profile_link_color: String,
100    profile_sidebar_border_color: String,
101    profile_sidebar_fill_color: String,
102    profile_text_color: String,
103    profile_use_background_image: bool,
104    default_profile: bool,
105    default_profile_image: bool,
106    following: bool,
107    follow_request_sent: bool,
108    notifications: bool,
109}
110
111#[derive(Debug, Facet)]
112struct UserEntities {
113    #[facet(default)]
114    url: Option<EntityUrl>,
115    description: EntityDescription,
116}
117
118#[derive(Debug, Facet)]
119struct EntityUrl {
120    urls: Vec<Url>,
121}
122
123#[derive(Debug, Facet)]
124struct EntityDescription {
125    urls: Vec<Url>,
126}
127
128#[derive(Debug, Facet)]
129struct Entities {
130    hashtags: Vec<Hashtag>,
131    symbols: Vec<Symbol>,
132    urls: Vec<Url>,
133    user_mentions: Vec<UserMention>,
134    #[facet(default)]
135    media: Option<Vec<Media>>,
136}
137
138#[derive(Debug, Facet)]
139struct Hashtag {
140    text: String,
141    indices: Vec<u64>,
142}
143
144#[derive(Debug, Facet)]
145struct Symbol {
146    text: String,
147    indices: Vec<u64>,
148}
149
150#[derive(Debug, Facet)]
151struct Url {
152    url: String,
153    expanded_url: String,
154    display_url: String,
155    indices: Vec<u64>,
156}
157
158#[derive(Debug, Facet)]
159struct UserMention {
160    screen_name: String,
161    name: String,
162    id: u64,
163    id_str: String,
164    indices: Vec<u64>,
165}
166
167#[derive(Debug, Facet)]
168struct Media {
169    id: u64,
170    id_str: String,
171    indices: Vec<u64>,
172    media_url: String,
173    media_url_https: String,
174    url: String,
175    display_url: String,
176    expanded_url: String,
177    #[facet(rename = "type")]
178    media_type: String,
179    sizes: Sizes,
180    #[facet(default)]
181    source_status_id: Option<u64>,
182    #[facet(default)]
183    source_status_id_str: Option<String>,
184}
185
186#[derive(Debug, Facet)]
187struct Sizes {
188    medium: Size,
189    small: Size,
190    thumb: Size,
191    large: Size,
192}
193
194#[derive(Debug, Facet)]
195struct Size {
196    w: u64,
197    h: u64,
198    resize: String,
199}
200
201fn main() {
202    let json = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/../../twitter.json"))
203        .expect("twitter.json not found");
204
205    for _ in 0..100 {
206        let result: Twitter = from_str(&json).unwrap();
207        black_box(result);
208    }
209}