//! The type that represents a response of `files.completeUploadExternal`.
//!
//! <https://api.slack.com/methods/files.completeUploadExternal>

use crate::generated::files_complete_upload_external::FilesCompleteUploadExternalResponse as Generated;
use crate::Error;
use serde::Deserialize;

/// The type that represents a response of `files.completeUploadExternal`.
///
/// <https://api.slack.com/methods/files.completeUploadExternal>
#[derive(Debug, Deserialize)]
pub struct FilesCompleteUploadExternalResponse(Generated);

impl FilesCompleteUploadExternalResponse {
    /// Converts to generated type.
    pub fn into_inner(self) -> Generated {
        self.0
    }
}

impl TryFrom<serde_json::Value> for FilesCompleteUploadExternalResponse {
    type Error = Error;

    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
        serde_json::from_value(value).map_err(Error::DeserializeJson)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_de_files_complete_upload_external_response() -> anyhow::Result<()> {
        let testdata = r#"{
            "ok": true,
            "files": [
                {
                    "id": "F123ABC456",
                    "title": "slack-test"
                }
            ]
        }"#;
        let deserialized: FilesCompleteUploadExternalResponse = serde_json::from_str(testdata)?;
        let deserialized = deserialized.into_inner();

        let files = deserialized.files.unwrap();
        let file = files.get(0).unwrap();
        assert_eq!(file.id.as_ref().unwrap(), "F123ABC456");
        assert_eq!(file.title.as_ref().unwrap(), "slack-test");

        Ok(())
    }
}
