I-CAN-hack / automotive

Rust crate providing a variety of automotive related libraries, such as communicating with CAN interfaces and diagnostic APIs
MIT License
44 stars 8 forks source link

change IsoTPConfig into builder pattern #72

Open pd0wm opened 2 months ago

pd0wm commented 2 months ago

Before:

    let mut isotp_config = IsoTPConfig::new(0, Identifier::Standard(0x7a1));
    isotp_config.padding = config.padding;
    isotp_config.fd = config.fd;
    isotp_config.ext_address = config.ext_address;
    isotp_config.timeout = std::time::Duration::from_millis(1000);
    isotp_config.max_dlen = config.max_dlen;

After:

  let isotp_config = IsoTPConfig::default()
        .tx(Identifier::Standard(0x7a1))
        .padding(config.padding)
        .fd(config.fd)
        .ext_address(config.ext_address)
        .max_dlen(config.max_dlen);

Maybe use separate builder type containing the offset? This would mean you always have to call .build() to finish the type. Now you could try setting the offset before the tx id, or the rx id before the tx id, and it would't work. E.g. the following fails silently:

  let isotp_config = IsoTPConfig::default()
        .offset(8)
        .tx(Identifier::Standard(0x7a1))

or

  let isotp_config = IsoTPConfig::default()
        .rx(Identifier::Standard(0x123))
        .tx(Identifier::Standard(0x7a1))