robohouse-delft / aravis-rs

Rust bindings for Aravis, a vision library for genicam based cameras
Other
14 stars 5 forks source link

Serde Feature #14

Open osiler opened 1 year ago

osiler commented 1 year ago

Hi,

I was wondering if it would be a good idea to serdify some of the constants and settings that the aravis bindings are generating.

While the genicam standard does provide the information from most gige cameras (my use case), application settings for developers are likely to be co-located in one place (yaml, json, toml).

An example of some of the pixel formats below.


use aravis::PixelFormat;
use serde::{Deserialize, de::Visitor};

#[derive(Copy, Clone)]
pub struct CameraPixelFormat(pub PixelFormat);

impl<'de> Deserialize<'de> for CameraPixelFormat {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(PixelFormatVisitor {})
    }
}

pub struct PixelFormatVisitor {}

impl<'de> Visitor<'de> for PixelFormatVisitor {
    type Value = CameraPixelFormat;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("Could not deserialise CameraPixelFormat")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error, {

       match v {
            "RGB_8_PACKER" => Ok(CameraPixelFormat(PixelFormat::RGB_8_PACKED)),
            "BAYER_RG_8" => Ok(CameraPixelFormat(PixelFormat::BAYER_RG_8)),
            "RGB_8_PLANAR" => Ok(CameraPixelFormat(PixelFormat::RGB_8_PLANAR)),
            _ => Err(serde::de::Error::custom("Unknown pixel format {v:?}")),
        } 
    }
}