mavlink_core/connection/file/
sync.rs1use super::config::FileConfig;
4
5use crate::Dialect;
6use crate::MavlinkReader;
7use crate::connection::sync::{Connectable, DialectConnection, SyncTransport, Transport};
8use std::fs::File;
9use std::io::{self, Sink};
10use std::path::PathBuf;
11use std::sync::Mutex;
12
13pub fn open(file_path: &PathBuf) -> io::Result<FileConnection> {
14 Ok(FileConnection {
15 file: Mutex::new(MavlinkReader::new(File::open(file_path)?)),
16 })
17}
18
19pub struct FileConnection {
20 file: Mutex<MavlinkReader<File>>,
21}
22
23impl SyncTransport for FileConnection {
24 type Reader = File;
25 type Writer = Sink;
26
27 fn reader(&self) -> std::sync::MutexGuard<'_, MavlinkReader<Self::Reader>> {
28 self.file.lock().unwrap()
29 }
30
31 fn lock_writer(&self) -> Option<std::sync::MutexGuard<'_, Self::Writer>> {
32 None
33 }
34
35 fn retry_receive(&self, error: &crate::error::MessageReadError) -> bool {
36 !matches!(error, crate::error::MessageReadError::Io(error) if error.kind() == io::ErrorKind::UnexpectedEof)
37 }
38}
39
40impl Connectable for FileConfig {
41 fn connect_with_dialect<D: Dialect + Send + Sync>(
42 &self,
43 dialect: D,
44 ) -> io::Result<DialectConnection<D>> {
45 Ok(DialectConnection::new(
46 Transport::File(open(&self.address)?),
47 dialect,
48 ))
49 }
50}