Skip to content

Request: Typed creation of MavConnection implementations #290

@peterkrull

Description

@peterkrull

I find the current way of using strings to construct a MavConnection implementer through mavlink::connect(..) a bit awkward. It feels more like an application-level interface and not a library level interface, making it harder to build on top of the existing connection code or just use e.g. the serial implementation directly.

What I propose would include:

  • Make types which encode the description/configuration of a connection
  • Expose the individual MavConnection implementations for direct creation.

I imagine it could look something like this:

// in: mavlink-core/connection
pub trait Connectable<M: Message>
where Self: Sized
{
    fn connect(&self) -> io::Result<Box<dyn MavConnection<M> + Sync + Send>>;
}

// in: mavlink-core/connection/serial
pub struct SerialConfig {
    pub port: String,
    pub baud: usize,
}

impl<M: Message> Connectable<M> for SerialConfig {
    // impl
}

// in: mavlink-core/connection/tcp
pub struct TcpConfig {
    pub addr: SocketAddr,
    pub out: bool,
}

impl<M: Message> Connectable<M> for TcpConfig {
    // impl
}

// And so on..

which would make it fairly easy to implement a string-parser like:

match argument.split_whitespace().collect::<Vec<_>>().as_slice() {
    ["serial", port, baud] => SerialConfig {
        port: port.to_string(),
        baud: baud.parse().ok()?,
    }.connect()?,
    
    [conn @ "tcpin" | "tcpout", addr] => TcpConfig {
        addr: SocketAddr::from_str(port).ok()?,
        out: if conn == &"tcpout" { true } else { false },
    }.connect()?,

    // and so on..
    _ => None, // some error
}

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions