Skip to main content

x_twitter_sdk/types/
response.rs

1use serde::Deserialize;
2
3/// Response from posting a tweet
4#[derive(Debug, Clone, Deserialize)]
5pub struct TweetResponse {
6    /// The tweet data
7    pub data: TweetData,
8}
9
10/// Tweet data from the API
11#[derive(Debug, Clone, Deserialize)]
12pub struct TweetData {
13    /// Unique identifier of the tweet
14    pub id: String,
15
16    /// The actual UTF-8 text of the tweet
17    pub text: String,
18
19    /// Edit history tweet IDs (if tweet was edited)
20    #[serde(default)]
21    pub edit_history_tweet_ids: Option<Vec<String>>,
22}
23
24impl TweetResponse {
25    /// Get the tweet URL
26    ///
27    /// # Arguments
28    /// * `username` - The username of the account that posted the tweet
29    ///
30    /// # Returns
31    /// The full URL to the tweet
32    pub fn tweet_url(&self, username: &str) -> String {
33        format!("https://twitter.com/{}/status/{}", username, self.data.id)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_deserialize_tweet_response() {
43        let json = r#"
44        {
45            "data": {
46                "id": "1234567890",
47                "text": "Hello, world!"
48            }
49        }
50        "#;
51
52        let response: TweetResponse = serde_json::from_str(json).unwrap();
53        assert_eq!(response.data.id, "1234567890");
54        assert_eq!(response.data.text, "Hello, world!");
55        assert!(response.data.edit_history_tweet_ids.is_none());
56    }
57
58    #[test]
59    fn test_tweet_url() {
60        let response = TweetResponse {
61            data: TweetData {
62                id: "1234567890".to_string(),
63                text: "Test".to_string(),
64                edit_history_tweet_ids: None,
65            },
66        };
67
68        let url = response.tweet_url("testuser");
69        assert_eq!(url, "https://twitter.com/testuser/status/1234567890");
70    }
71}