Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Typed creation of MavConnection implementations #290

Open
peterkrull opened this issue Nov 7, 2024 · 0 comments
Open

Request: Typed creation of MavConnection implementations #290

peterkrull opened this issue Nov 7, 2024 · 0 comments

Comments

@peterkrull
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant