Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/src/schema/idl/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,17 @@ pub mod grammar {
Named(ScopedIdentifier),
Array(Box<ArrayType>),
Union(UnionType),
Unit(UnitType),
}

/// The unit type `()` - an explicit "reply, but no value" (an empty ack),
/// distinct from omitting `->` entirely, which means "no reply at all".
#[derive(Debug, Clone)]
pub struct UnitType {
#[rust_sitter::leaf(text = "(")]
_open: (),
#[rust_sitter::leaf(text = ")")]
_close: (),
}

/// Array type: Type[] or Type[SIZE]
Expand Down
6 changes: 5 additions & 1 deletion core/src/schema/ir/compiler/interpreted/kind_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ pub enum KindValue {
Primitive(Primitive),
EnumVariant(String, Option<Box<KindValue>>),
Union(Vec<KindValue>),
Namespaced(String, Option<Box<KindValue>>)
Namespaced(String, Option<Box<KindValue>>),
/// The unit type `()` - a reply carrying no value. Distinct from a
/// function with no `_return` at all (no reply).
Unit,
}

impl KindValue {
Expand Down Expand Up @@ -127,6 +130,7 @@ impl KindValue {
// TODO: Properly implement, it was testing at this stage
(namespace.clone(), None)
}
KindValue::Unit => ("()".to_owned(), None),
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion core/src/schema/ir/compiler/interpreter/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ impl IncrementalInterpreter {

FrozenUnit::Function {
name: func_name,
parameters: annotation_units(&func.annotations()),
arguments,
_return: return_type,
synchronous: true,
docstring: func.docstring().unwrap_or_default(),
throws: func.throws().into_iter().collect(),
span: func.span,
Expand Down Expand Up @@ -371,6 +371,10 @@ fn build_kind_value(
);
}

if matches!(type_def, crate::schema::idl::grammar::Type::Unit(_)) {
return KindValue::Unit;
}

let type_name = type_to_string(type_def);

match (type_name.as_str(), value) {
Expand Down Expand Up @@ -415,6 +419,7 @@ fn type_to_string(type_def: &crate::schema::idl::grammar::Type) -> String {
let members: Vec<String> = union_type.members().iter().map(type_to_string).collect();
format!("union({})", members.join(" "))
}
crate::schema::idl::grammar::Type::Unit(_) => "()".to_string(),
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/schema/ir/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ fn kind_to_string(kind: &KindValue) -> String {
let parts: Vec<String> = members.iter().map(kind_to_string).collect();
format!("union({})", parts.join(" | "))
}
KindValue::Unit => "()".to_string(),
}
}

Expand Down
6 changes: 5 additions & 1 deletion core/src/schema/ir/frozen/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ pub enum FrozenUnit {
},
Function {
docstring: String,
// `@key = value` function annotations (per-call settings: `@timeout_ms`,
// `@idempotent`, ...) as `Property { name, expression }`, same shape as
// `Protocol` / `Struct` carry. Open namespace - a consumer acts on the
// keys it knows and ignores the rest.
parameters: Vec<FrozenUnit>,
name: String,
synchronous: bool,
// direction: Box<FrozenUnit>,
arguments: Vec<FrozenArgument>,
_return: Option<KindValue>,
Expand Down
3 changes: 3 additions & 0 deletions core/src/schema/ir/validation/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ fn validate_type(
// type today (it's only ever used to represent one variant inside
// an Enum's own `variants` list, not as a referenceable type).
}
KindValue::Unit => {
// `()` - the empty-reply type. Nothing to resolve.
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions core/tests/schema/ir/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,48 @@ protocol EventService {
}
}

#[test]
fn test_protocol_unit_return_ir() {
// `-> ()` freezes as `_return: Some(KindValue::Unit)` - an empty
// reply - where a function with no `->` freezes as `_return: None`.
use comline_core::schema::ir::compiler::interpreted::kind_search::KindValue;

let code = r#"
protocol TxService {
function commit() -> ();
function log(str);
}
"#;
let ir_units = IncrementalInterpreter::from_source(code);
match &ir_units[0] {
comline_core::schema::ir::frozen::unit::FrozenUnit::Protocol { functions, .. } => {
match &functions[0] {
comline_core::schema::ir::frozen::unit::FrozenUnit::Function {
name,
_return,
..
} => {
assert_eq!(name, "commit");
assert_eq!(_return, &Some(KindValue::Unit));
}
_ => panic!("Expected Function"),
}
match &functions[1] {
comline_core::schema::ir::frozen::unit::FrozenUnit::Function {
name,
_return,
..
} => {
assert_eq!(name, "log");
assert_eq!(_return, &None);
}
_ => panic!("Expected Function"),
}
}
_ => panic!("Expected Protocol"),
}
}

#[test]
fn test_struct_and_field_spans_are_populated() {
let code = "struct User {\n id: u64\n name: str\n}\n";
Expand Down
7 changes: 7 additions & 0 deletions core/tests/schema/parser/comprehensive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ enum DayOfWeek {
assert!(grammar::parse(code).is_ok());
}

#[test]
fn test_protocol_unit_return() {
// `-> ()` is an explicit empty reply, distinct from no `->` at all.
let code = "protocol API { function commit() -> (); }";
assert!(grammar::parse(code).is_ok());
}

#[test]
fn test_protocol_multiple_args() {
let code = "protocol API { function process(str, u32, bool) -> s64; }";
Expand Down
Loading