Struct Device

Source
pub struct Device {
    pub serialized_py_object: Vec<u8>,
    pub callable_methods: HashMap<String, String>,
}
Expand description

Represents a Miio device with its associated properties and Python object.

The Device struct includes data necessary for device communication and method invocation, along with functionalities to serialize/deserialize the device configuration.

Fields§

§serialized_py_object: Vec<u8>

A serialized representation of the underlying Python object as bytes.

§callable_methods: HashMap<String, String>

A map of callable method names to their corresponding Python signatures.

Implementations§

Source§

impl Device

Source

pub fn deserialize_json(json_str: &str) -> Result<Device, Error>

Deserializes a JSON string into a Device instance.

This function leverages Serde’s JSON deserializer to parse the provided JSON string and convert it into a corresponding Device instance. The JSON must correctly represent the Device struct’s fields.

§Arguments
  • json_str - A string slice that holds the JSON representation of a Device.
§Returns
  • Ok(Device) containing the Device instance if deserialization succeeds.
  • Err(serde_json::Error) if parsing fails due to invalid JSON or type mismatch.
Source

pub fn serialize_json(&self) -> Result<String, Error>

Serializes the Device instance into a JSON string using pretty formatting.

This function leverages Serde’s JSON serializer with pretty print enabled to convert the Device struct into a human-readable JSON string. The output includes appropriate whitespace and line breaks, making it easier to read and debug.

§Returns
  • Ok(String) containing the formatted JSON representation of the Device if serialization succeeds.
  • Err(serde_json::Error) if any serialization error occurs.
Source

pub fn serialize_to_file(&self, folder: &str, file_name: &str) -> Result<()>

Serializes the Device instance to a JSON file.

§Arguments
  • folder - The directory where the file will be saved.
  • file_name - The name of the file to create.
§Returns
  • Ok(()) on success, or an std::io::Error on failure.
Source

pub fn deserialize_from_file(folder: &str, file_name: &str) -> Result<Device>

Deserializes a Device instance from a JSON file.

§Arguments
  • folder - The directory containing the file.
  • file_name - The name of the file to read.
§Returns
  • Ok(Device) if deserialization is successful.
  • Err(std::io::Error) if an error occurs during file read or parsing.
Source

pub fn create_device( ip: &str, token: &str, device_type: &str, ) -> Result<Device, PyErr>

Creates a new Device instance by invoking the Python function.

This function calls the Python module to create a device and retrieve its properties, including serialized state and callable methods.

§Arguments
  • ip - The IP address of the device.
  • token - The token used for authentication.
  • device_type - The type of the device.
§Returns
  • Ok(Device) on success.
  • Err(PyErr) if any Python call fails.
Source

pub fn call_method( &self, method_name: &str, args: Vec<&str>, ) -> Result<String, PyErr>

Calls a method on the device by invoking the corresponding Python function.

This function sends a command to the device through Python and returns the result.

§Arguments
  • method_name - The name of the method to be called.
  • args - A vector of string arguments for the method.
§Returns
  • Ok(String) containing the result if successful.
  • Err(PyErr) if the Python call fails.

Trait Implementations§

Source§

impl Clone for Device

Source§

fn clone(&self) -> Device

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Device

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Device

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl Send for Device

§

impl Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Ungil for T
where T: Send,