Skip to content

Commit b0b90e6

Browse files
committed
feat: Writer utilities for open state/path + add schema/channel object
1 parent a95657d commit b0b90e6

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/writer.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ impl MCAPWriter {
200200
}
201201
}
202202

203+
/// Returns whether the MCAPWriter is currently open.
204+
/// Returns true if open, false otherwise.
205+
#[func]
206+
pub fn is_open(&self) -> bool {
207+
self.writer.is_some()
208+
}
209+
210+
/// Returns the path of the currently opened MCAP file.
211+
/// If no file is open, returns an empty string.
212+
#[func]
213+
pub fn get_path(&self) -> GString {
214+
self.path.clone()
215+
}
216+
203217
/// Adds a schema, returning its ID. If a schema with the same content has been added already,
204218
/// its ID is returned. Returns -1 on error.
205219
///
@@ -225,6 +239,30 @@ impl MCAPWriter {
225239
)
226240
}
227241

242+
/// Adds a schema using an MCAPSchema resource
243+
/// The ID of the schema resource will be updated with the assigned ID.
244+
///
245+
/// * `schema`: The MCAPSchema resource to add.
246+
#[func]
247+
pub fn add_schema_object(
248+
&mut self,
249+
mut schema: Gd<crate::types::MCAPSchema>,
250+
) {
251+
let mut sc = schema.bind_mut();
252+
let new_id = self.with_writer(
253+
"add_schema_object",
254+
|w| {
255+
w.add_schema(
256+
sc.name.to_string().as_str(),
257+
sc.encoding.to_string().as_str(),
258+
sc.data.as_slice(),
259+
)
260+
},
261+
0,
262+
);
263+
sc.id = new_id;
264+
}
265+
228266
/// Adds a channel, returning its ID. If a channel with equivalent content was added previously,
229267
/// its ID is returned. Returns -1 on error.
230268
///
@@ -262,6 +300,39 @@ impl MCAPWriter {
262300
)
263301
}
264302

303+
/// Adds a channel using an MCAPChannel resource
304+
/// The ID of the channel resource will be updated with the assigned ID.
305+
/// It is required that the schema (if any) has already been added via `add_schema()`.
306+
///
307+
/// * `channel`: The MCAPChannel resource to add.
308+
#[func]
309+
pub fn add_channel_object(
310+
&mut self,
311+
mut channel: Gd<crate::types::MCAPChannel>,
312+
) {
313+
let mut ch = channel.bind_mut();
314+
// Convert Godot Dictionary to BTreeMap<String, String>
315+
let meta_map = dict_to_btreemap(&ch.metadata);
316+
let new_id = self.with_writer(
317+
"add_channel_object",
318+
|w| {
319+
w.add_channel(
320+
if let Some(schema) = &ch.schema {
321+
schema.bind().id as u16
322+
} else {
323+
0
324+
},
325+
ch.topic.to_string().as_str(),
326+
ch.message_encoding.to_string().as_str(),
327+
&meta_map,
328+
)
329+
330+
},
331+
0,
332+
);
333+
ch.id = new_id;
334+
}
335+
265336
/// Write the given message (and its provided channel, if not already added).
266337
/// The provided channel ID and schema ID will be used as IDs in the resulting MCAP.
267338
/// Write a full message resource (channel provided in the resource) to the file.

0 commit comments

Comments
 (0)