de-vri-es / serial2-rs

Cross platform serial ports for Rust
Other
48 stars 11 forks source link

Added feature 'with-serde' that allows (de-)serialization of settings… #19

Closed avocadochicken closed 10 months ago

avocadochicken commented 11 months ago

Hello!

I am using this library like:

use serial2::{SerialPort, CharSize, Parity, StopBits, FlowControl};

#[derive(Debug, Serialize, Deserialize)]
pub struct Settings {
    baudrate: u32,
    char_size: CharSize,
    parity: Parity,
    stop_bits: StopBits,
    flow_control: FlowControl,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            baudrate: 115200,
            char_size: CharSize::Bits8,
            parity: Parity::None,
            stop_bits: StopBits::One,
            flow_control: FlowControl::None,
        }
    }
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct SerialInterface {
    pub serial_config: Settings,
    pub serial_input: Vec<u8>,
    pub serial_output: Vec<u8>,
}

I need de-/serialization functionality on the Settings. This is why I added a feature "with-serde" to your library. In Cargo.toml: serial2 = { version = "0.2.8", features = ["with-serde"] }

Maybe you find it useful and want to merge it. Feel free to implement differently.

de-vri-es commented 11 months ago

Hey! Thanks for the PR.

I'm definitely interested adding some basic support for serde behind a feature flag.

I do think it would be nice to implement serialization as numbers instead of strings for StopBits and CharSize. That requires manual implementations, but I think it is worth it.

avocadochicken commented 11 months ago

I do think it would be nice to implement serialization as numbers instead of strings for StopBits and CharSize. That requires manual implementations, but I think it is worth it.

Meaning CharSize returns number (u8, usize, ... ?) between 5 and 8? StopBits returning 1 or 2, ...? What about Parity? How to encode odd and even ? Or FlowControl?

Tell me and I can make adjustments. But personally I am not against the idea of using strings.

de-vri-es commented 11 months ago

For flow control and parity I would indeed stick with strings. But for char size and stop bits I think it would be nice to serialize as 5, 6, 7 or 8 and 1 or 2.

The backing number type doesn't really matter, it's just for serialization.

avocadochicken commented 11 months ago

If it's ok to pull another crate see: ad345ff

de-vri-es commented 11 months ago

Looks good in general. We can keep the repr() even when the feature is disabled. Although I don't really like the extra dependency.

It's convenient, but if we do it manually it saves us some build time. If you want, please update the PR to make those changes. If you're busy, I can also get round to it soonTM.

de-vri-es commented 10 months ago

I guess soonTM was today :)

Merging!

Thanks again for the PR!

de-vri-es commented 10 months ago

Released with 0.2.9! :rocket: