buttplug_core 10.0.2

Buttplug Intimate Hardware Control Library - Core Library
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2026 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

use crate::message::{ButtplugMessage, ButtplugMessageError, ButtplugMessageValidator};
use serde::{Deserialize, Serialize};

/// Ok message, signifying successful response to a command. [Spec link](https://buttplug-spec.docs.buttplug.io/status.html#ok).
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct OkV0 {
  /// Message Id, used for matching message pairs in remote connection instances.
  #[serde(rename = "Id")]
  id: u32,
}

impl OkV0 {
  /// Creates a new Ok message with the given Id.
  pub fn new(id: u32) -> Self {
    Self { id }
  }
}

impl Default for OkV0 {
  fn default() -> Self {
    Self { id: 1 }
  }
}

impl ButtplugMessage for OkV0 {
  fn id(&self) -> u32 {
    self.id
  }
  fn set_id(&mut self, id: u32) {
    self.id = id;
  }
}

impl ButtplugMessageValidator for OkV0 {
  fn is_valid(&self) -> Result<(), ButtplugMessageError> {
    self.is_not_system_id(self.id)
  }
}

#[cfg(test)]
mod test {
  use crate::message::{ButtplugServerMessageCurrent, OkV0};

  const OK_STR: &str = "{\"Ok\":{\"Id\":0}}";

  #[test]
  fn test_ok_serialize() {
    let ok = ButtplugServerMessageCurrent::Ok(OkV0::new(0));
    let js = serde_json::to_string(&ok).expect("Infallible serialization");
    assert_eq!(OK_STR, js);
  }
}