wme-models 0.1.3

Type definitions for the Wikimedia Enterprise API
Documentation
//! Content types for the Wikimedia Enterprise API.
//!
//! This module provides types for article content including the body
//! (HTML and wikitext), images, and license information.
//!
//! # Article Body
//!
//! The [`ArticleBody`] struct contains both HTML and wikitext representations
//! of article content. Not all API endpoints return both - some may return
//! only HTML or only wikitext depending on the fields requested.
//!
//! # Images
//!
//! [`Image`] provides metadata about images including dimensions and
//! accessibility information (caption, alternative text).
//!
//! # Licenses
//!
//! Articles are typically licensed under Creative Commons licenses.
//! The [`License`] type captures this information.

use serde::{Deserialize, Serialize};

/// Article body content.
///
/// Contains both HTML and wikitext representations of the article.
/// Depending on the API endpoint and fields requested, one or both
/// may be present.
///
/// # Example
///
/// ```
/// use wme_models::ArticleBody;
///
/// let body = ArticleBody {
///     html: Some("<p>Article content</p>".to_string()),
///     wikitext: Some("Article content".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct ArticleBody {
    /// HTML content (parsed from wikitext)
    pub html: Option<String>,
    /// Wikitext content (raw markup)
    pub wikitext: Option<String>,
}

/// Image metadata.
///
/// Describes an image associated with an article. Includes
/// dimensions and accessibility information.
///
/// # Example
///
/// ```
/// use wme_models::Image;
///
/// let image = Image {
///     content_url: "https://upload.wikimedia.org/...".to_string(),
///     width: Some(800),
///     height: Some(600),
///     caption: Some("A squirrel in a tree".to_string()),
///     alternative_text: Some("Gray squirrel sitting on branch".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Image {
    /// Content URL (direct link to image file)
    pub content_url: String,
    /// Image width in pixels
    pub width: Option<u64>,
    /// Image height in pixels
    pub height: Option<u64>,
    /// Image caption (displayed below image)
    pub caption: Option<String>,
    /// Alternative text for accessibility (screen readers)
    pub alternative_text: Option<String>,
}

/// License information.
///
/// Describes the license under which article content is published.
/// Most Wikimedia content is CC-BY-SA licensed.
///
/// # Example
///
/// ```
/// use wme_models::License;
///
/// let license = License {
///     identifier: Some("CC-BY-SA-4.0".to_string()),
///     name: "Creative Commons Attribution Share Alike 4.0".to_string(),
///     url: "https://creativecommons.org/licenses/by-sa/4.0/".to_string(),
/// };
/// ```
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct License {
    /// License identifier (e.g., "CC-BY-SA-4.0")
    pub identifier: Option<String>,
    /// License name
    pub name: String,
    /// License URL
    pub url: String,
}

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

    #[test]
    fn test_article_body() {
        let body = ArticleBody {
            html: Some("<p>Test content</p>".to_string()),
            wikitext: Some("Test content".to_string()),
        };

        assert!(body.html.is_some());
        assert!(body.wikitext.is_some());
    }

    #[test]
    fn test_image() {
        let image = Image {
            content_url: "https://example.com/image.jpg".to_string(),
            width: Some(800),
            height: Some(600),
            caption: Some("Test image".to_string()),
            alternative_text: Some("Alt text".to_string()),
        };

        assert_eq!(image.width, Some(800));
        assert_eq!(image.height, Some(600));
    }

    #[test]
    fn test_license() {
        let license = License {
            identifier: Some("CC-BY-SA-4.0".to_string()),
            name: "Creative Commons Attribution Share Alike 4.0".to_string(),
            url: "https://creativecommons.org/licenses/by-sa/4.0/".to_string(),
        };

        assert_eq!(license.identifier, Some("CC-BY-SA-4.0".to_string()));
    }
}