hevy 0.1.0

An async Rust client library for the Hevy public API (https://api.hevyapp.com/docs/)
Documentation
use serde::{Deserialize, Serialize};

use super::common::{CustomExerciseType, EquipmentCategory, MuscleGroup};

/// An exercise template, either built-in or custom to the account.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ExerciseTemplate {
    /// The exercise template ID.
    pub id: String,
    /// The exercise title.
    #[serde(default)]
    pub title: String,
    /// The exercise type (e.g. `weight_reps`).
    #[serde(rename = "type", default)]
    pub r#type: String,
    /// The primary muscle group of the exercise.
    #[serde(default)]
    pub primary_muscle_group: String,
    /// The secondary muscle groups of the exercise.
    #[serde(default)]
    pub secondary_muscle_groups: Vec<String>,
    /// The equipment category of the exercise.
    #[serde(default)]
    pub equipment_category: Option<EquipmentCategory>,
    /// Whether the exercise is a custom exercise created by the account.
    #[serde(default)]
    pub is_custom: bool,
}

/// A page of [`ExerciseTemplate`]s, as returned by
/// [`crate::HevyClient::list_exercise_templates`].
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ExerciseTemplatesPage {
    /// The current page number.
    pub page: u32,
    /// The total number of pages available.
    pub page_count: u32,
    /// The exercise templates on this page.
    #[serde(default)]
    pub exercise_templates: Vec<ExerciseTemplate>,
}

/// The fields accepted when creating a new custom exercise template.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NewCustomExercise {
    /// The title of the exercise template.
    pub title: String,
    /// The exercise type.
    pub exercise_type: CustomExerciseType,
    /// The equipment category of the exercise template.
    pub equipment_category: EquipmentCategory,
    /// The primary muscle group of the exercise template.
    pub muscle_group: MuscleGroup,
    /// Other muscles worked by the exercise template.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub other_muscles: Vec<MuscleGroup>,
}

impl NewCustomExercise {
    /// Creates a new custom exercise payload.
    pub fn new(
        title: impl Into<String>,
        exercise_type: CustomExerciseType,
        equipment_category: EquipmentCategory,
        muscle_group: MuscleGroup,
    ) -> Self {
        Self {
            title: title.into(),
            exercise_type,
            equipment_category,
            muscle_group,
            other_muscles: Vec::new(),
        }
    }

    /// Sets the other muscles worked by the exercise template.
    pub fn with_other_muscles(mut self, muscles: impl IntoIterator<Item = MuscleGroup>) -> Self {
        self.other_muscles = muscles.into_iter().collect();
        self
    }
}

/// The request body used to create a new custom exercise template.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateExerciseTemplateRequest {
    /// The exercise template to create.
    pub exercise: NewCustomExercise,
}

impl CreateExerciseTemplateRequest {
    /// Wraps a [`NewCustomExercise`] in the request body envelope expected by the API.
    pub fn new(exercise: NewCustomExercise) -> Self {
        Self { exercise }
    }
}

/// The response returned after successfully creating a custom exercise template.
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
pub struct CreatedExerciseTemplate {
    /// The ID of the newly created exercise template.
    pub id: i64,
}