uobors_core 0.0.8

Unleash OpenBOR modding in Rust
Documentation
pub mod model;

use anyhow::Result;
use garde::Validate;
use model::Model;

/// The OpenBOR project.
#[derive(Debug, Clone, PartialEq, Validate)]
pub struct Project {
    #[garde(skip)]
    models: Vec<Model>,
}

impl Project {
    /// Creates a new Project.
    ///
    /// # Arguments
    ///
    /// * `models` - The models in the project.
    ///
    /// # Returns
    ///
    /// * `anyhow::Result<Project>` - The result of creating a new Project.
    pub fn new(models: Vec<Model>) -> Result<Self> {
        let project = Self { models };

        for model in &project.models {
            model.validate(&())?;
        }

        Ok(project)
    }

    /// Returns the models in the project.
    pub fn models(&self) -> &[Model] {
        &self.models
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use image::RgbaImage;
    use model::{
        animation::{
            animation_name::AnimationName,
            animation_number::AnimationNumber,
            frame::{bbox::FrameBBox, delay::FrameDelay, offset::FrameOffset, Frame},
            Animation,
        },
        model_name::ModelName,
        model_type::ModelType,
        Model,
    };
    use rstest::{fixture, rstest};

    #[fixture]
    fn stub_frame() -> Frame {
        Frame::new(
            Some(FrameDelay::new(1).unwrap()),
            Some(FrameOffset::new(0, 0).unwrap()),
            Some(FrameBBox::new(0, 0, 10, 10, 0, 0).unwrap()),
            RgbaImage::new(1, 1),
        )
        .unwrap()
    }

    #[fixture]
    fn stub_animation() -> Animation {
        Animation::new(
            AnimationName::Idle,
            AnimationNumber::new(1).unwrap(),
            vec![stub_frame()],
        )
        .unwrap()
    }

    #[fixture]
    fn stub_model() -> Model {
        Model::new(
            ModelName::new("model").unwrap(),
            ModelType::Player,
            vec![stub_animation()],
        )
        .unwrap()
    }

    #[rstest]
    fn test_new_valid_with_one_model(stub_model: Model) {
        // Act
        let project = Project::new(vec![stub_model]);

        // Assert
        assert!(project.is_ok());
    }

    #[rstest]
    fn test_getters_with_one_model(stub_model: Model) {
        // Arrange
        let model_vec = vec![stub_model];
        let project = Project::new(model_vec.clone()).unwrap();

        // Act
        let models = project.models();

        // Assert
        assert_eq!(models.len(), 1);
        assert_eq!(models, &model_vec);
    }
}