buttplug_server_device_config 10.1.0

Buttplug Intimate Hardware Control Library - Server Device Config Library
Documentation
// 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 getset::{Getters, MutGetters};
use serde::{Deserialize, Serialize};

/// Identifying information for devices that are currently connected or have connected in the past.
///
/// Contains the 3 fields needed to uniquely identify a device in the system. Unlike
/// [ConfigurationDeviceIdentifier]s, [UserDeviceIdentifier] will always have a device address
/// available.
///
/// NOTE: UserDeviceIdentifiers are NOT portable across platforms. For instance, bluetooth addresses
/// are used for the address field on bluetooth devices. These will differ between all platforms due
/// to address formatting as well as available information (macOS/iOS and WebBluetooth obfuscate
/// bluetooth addresses)
#[derive(Debug, Eq, PartialEq, Hash, Clone, Getters, MutGetters, Serialize, Deserialize)]
#[getset(get = "pub", get_mut = "pub(crate)")]
pub struct UserDeviceIdentifier {
  /// Name of the protocol used
  protocol: String,
  /// Internal identifier for the protocol used
  identifier: Option<String>,
  /// Address, as possibly serialized by whatever the managing library for the Device Communication Manager is.
  address: String,
}

impl UserDeviceIdentifier {
  /// Creates a new instance
  pub fn new(address: &str, protocol: &str, identifier: &Option<String>) -> Self {
    Self {
      address: address.to_owned(),
      protocol: protocol.to_owned(),
      identifier: identifier.clone(),
    }
  }
}

/// Set of information used for matching devices to their features and related communication protocol.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Getters, MutGetters, Serialize, Deserialize)]
#[getset(get = "pub", get_mut = "pub(crate)")]
pub struct BaseDeviceIdentifier {
  /// Name of the protocol this device uses to communicate
  protocol: String,
  /// Some([identifier]) if there's an identifier, otherwise None if default
  identifier: Option<String>,
}

impl BaseDeviceIdentifier {
  pub fn new_default(protocol: &str) -> Self {
    Self::new(protocol, &None)
  }

  pub fn new_with_identifier(protocol: &str, attributes_identifier: &str) -> Self {
    Self::new(protocol, &Some(attributes_identifier.to_owned()))
  }

  pub fn new(protocol: &str, attributes_identifier: &Option<String>) -> Self {
    Self {
      protocol: protocol.to_owned(),
      identifier: attributes_identifier.clone(),
    }
  }
}

impl From<&UserDeviceIdentifier> for BaseDeviceIdentifier {
  fn from(other: &UserDeviceIdentifier) -> Self {
    Self {
      protocol: other.protocol().clone(),
      identifier: other.identifier().clone(),
    }
  }
}

impl PartialEq<UserDeviceIdentifier> for BaseDeviceIdentifier {
  fn eq(&self, other: &UserDeviceIdentifier) -> bool {
    self.protocol == *other.protocol() && self.identifier == *other.identifier()
  }
}