Skip to content

Commit b5511fd

Browse files
authored
Automatic C documentation for Duration type: structs, function parameters, and return values (#50)
* document the time unit in structs. emit any default values for durations in the correct unit * document the unit of durations in function arguments and return values
1 parent 28ca1ae commit b5511fd

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

generators/c-oo-bindgen/src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,19 @@ fn write_struct_definition(
343343
StructElementType::OneTimeCallback(_) => None,
344344
StructElementType::Iterator(_) => None,
345345
StructElementType::Collection(_) => None,
346-
StructElementType::Duration(_, default) => {
347-
default.map(|x| format!("{}s", x.as_secs_f32()))
346+
StructElementType::Duration(mapping, default) => {
347+
default.map(|x| mapping.get_value_string(x))
348348
}
349349
};
350350

351351
if let Some(default_value) = default_value {
352352
f.writeln(&format!("@note Default value is {}", default_value))?;
353353
}
354354

355+
if let StructElementType::Duration(mapping, _) = &element.element_type {
356+
f.writeln(&format!("@note The unit is {}", mapping.unit()))?;
357+
}
358+
355359
Ok(())
356360
})?;
357361
f.writeln(&format!(
@@ -591,6 +595,9 @@ fn write_function_docs(
591595
for param in &handle.parameters {
592596
f.writeln(&format!("@param {} ", param.name))?;
593597
docstring_print(f, &param.doc, lib)?;
598+
if let Type::Duration(mapping) = param.param_type {
599+
f.write(&format!(" ({})", mapping.unit()))?;
600+
}
594601
}
595602

596603
fn write_error_return_doc(f: &mut dyn Printer) -> FormattingResult<()> {
@@ -599,16 +606,22 @@ fn write_function_docs(
599606

600607
match handle.get_type() {
601608
NativeFunctionType::NoErrorNoReturn => {}
602-
NativeFunctionType::NoErrorWithReturn(_, doc) => {
609+
NativeFunctionType::NoErrorWithReturn(ret, doc) => {
603610
f.writeln("@return ")?;
604611
docstring_print(f, &doc, lib)?;
612+
if let Type::Duration(mapping) = ret {
613+
f.write(&format!(" ({})", mapping.unit()))?;
614+
}
605615
}
606616
NativeFunctionType::ErrorNoReturn(_) => {
607617
write_error_return_doc(f)?;
608618
}
609-
NativeFunctionType::ErrorWithReturn(_, _, doc) => {
619+
NativeFunctionType::ErrorWithReturn(_, ret, doc) => {
610620
f.writeln("@param out ")?;
611621
docstring_print(f, &doc, lib)?;
622+
if let Type::Duration(mapping) = ret {
623+
f.write(&format!(" ({})", mapping.unit()))?;
624+
}
612625
write_error_return_doc(f)?;
613626
}
614627
}

oo-bindgen/src/native_function.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::collection::CollectionHandle;
22
use crate::doc::{Doc, DocString};
33
use crate::iterator::IteratorHandle;
44
use crate::*;
5+
use std::time::Duration;
56

67
#[derive(Debug, Clone, PartialEq)]
78
pub enum Type {
@@ -42,6 +43,30 @@ pub enum DurationMapping {
4243
SecondsFloat,
4344
}
4445

46+
impl DurationMapping {
47+
pub fn unit(&self) -> &'static str {
48+
match self {
49+
DurationMapping::Milliseconds => "milliseconds",
50+
DurationMapping::Seconds => "seconds",
51+
DurationMapping::SecondsFloat => "fractional seconds",
52+
}
53+
}
54+
55+
pub fn get_value_string(&self, duration: Duration) -> String {
56+
match self {
57+
DurationMapping::Milliseconds => {
58+
format!("{}", duration.as_millis())
59+
}
60+
DurationMapping::Seconds => {
61+
format!("{}", duration.as_secs())
62+
}
63+
DurationMapping::SecondsFloat => {
64+
format!("{}", duration.as_secs_f32())
65+
}
66+
}
67+
}
68+
}
69+
4570
#[derive(Debug)]
4671
pub enum ReturnType {
4772
Void,

tests/foo-schema/src/duration.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@ pub fn define(lib: &mut LibraryBuilder) -> Result<(), BindingError> {
88
.param(
99
"value",
1010
Type::Duration(DurationMapping::Milliseconds),
11-
"Duration (in milliseconds)",
11+
"Duration",
1212
)?
1313
.return_type(ReturnType::new(
1414
Type::Duration(DurationMapping::Milliseconds),
15-
"Duration (in milliseconds)",
15+
"Duration",
1616
))?
17-
.doc("Echo duration through count of milliseconds")?
17+
.doc("Echo duration as count of milliseconds")?
1818
.build()?;
1919

2020
let duration_s_echo_func = lib
2121
.declare_native_function("duration_s_echo")?
2222
.param(
2323
"value",
2424
Type::Duration(DurationMapping::Seconds),
25-
"Duration (in seconds)",
25+
"Duration",
2626
)?
2727
.return_type(ReturnType::new(
2828
Type::Duration(DurationMapping::Seconds),
29-
"Duration (in seconds)",
29+
"Duration",
3030
))?
31-
.doc("Echo duration through count of seconds")?
31+
.doc("Echo duration as count of seconds")?
3232
.build()?;
3333

3434
let duration_s_float_echo_func = lib
3535
.declare_native_function("duration_s_float_echo")?
3636
.param(
3737
"value",
3838
Type::Duration(DurationMapping::SecondsFloat),
39-
"Duration (in seconds)",
39+
"Duration",
4040
)?
4141
.return_type(ReturnType::new(
4242
Type::Duration(DurationMapping::SecondsFloat),
43-
"Duration (in seconds)",
43+
"Duration",
4444
))?
45-
.doc("Echo duration through floating point (in seconds)")?
45+
.doc("Echo duration as fractional seconds")?
4646
.build()?;
4747

4848
// Declare static class

0 commit comments

Comments
 (0)