use zlink::idl::Interface;
use zlink_codegen::generate_interface;
#[test]
fn test_simple_interface() {
let idl = r#"
# Simple Ping interface
interface org.example.ping
method Ping(message: string) -> (reply: string)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("#[proxy(\"org.example.ping\")]"));
assert!(code.contains("pub trait Ping"));
assert!(code.contains("async fn ping"));
assert!(code.contains("message: &str"));
}
#[test]
fn test_interface_with_types() {
let idl = r#"
interface org.example.types
type Person (
name: string,
age: int,
email: ?string
)
type Status (idle, busy, away)
method GetPerson(id: int) -> (person: Person)
method SetStatus(status: Status) -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("pub struct Person"));
assert!(code.contains("pub name: String"));
assert!(code.contains("pub age: i64"));
assert!(code.contains("pub email: Option<String>"));
assert!(code.contains("pub enum Status"));
assert!(code.contains("Idle,"));
assert!(code.contains("Busy,"));
assert!(code.contains("Away,"));
}
#[test]
fn test_interface_with_errors() {
let idl = r#"
interface org.example.errors
error NotFound(id: int)
error InvalidInput(message: string)
method Get(id: int) -> (value: string)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("#[derive(Debug, Clone, PartialEq, ReplyError)]"));
assert!(code.contains("#[zlink(interface = \"org.example.errors\")]"));
assert!(code.contains("pub enum ErrorsError"));
assert!(code.contains("NotFound"));
assert!(code.contains("InvalidInput"));
}
#[test]
fn test_interface_with_arrays_and_dicts() {
let idl = r#"
interface org.example.collections
method ListItems() -> (items: []string)
method GetConfig() -> (config: [string]string)
method ProcessData(numbers: []int) -> (results: [string]int)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("Vec<&'a str>"));
assert!(code.contains("std::collections::HashMap<&'a str, &'a str>"));
assert!(code.contains("std::collections::HashMap<&'a str, i64>"));
assert!(code.contains("numbers: &[i64]"));
}
#[test]
fn test_interface_with_optional_fields() {
let idl = r#"
interface org.example.optional
type Config (
name: string,
value: ?string,
enabled: ?bool
)
method GetConfig() -> (config: ?Config)
method SetConfig(config: ?Config) -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("pub value: Option<String>"));
assert!(code.contains("pub enabled: Option<bool>"));
assert!(code.contains("Option<Config>"));
assert!(code.contains("config: Option<&Config>"));
}
#[test]
fn test_interface_with_multiple_outputs() {
let idl = r#"
interface org.example.multi
method GetStats() -> (
total: int,
average: float,
min: int,
max: int
)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("pub struct GetStatsOutput"));
assert!(code.contains("pub total: i64"));
assert!(code.contains("pub average: f64"));
assert!(code.contains("pub min: i64"));
assert!(code.contains("pub max: i64"));
}
#[test]
fn test_org_varlink_service() {
let idl = r#"
# The Varlink Service Interface is provided by every varlink service. It
# describes the service and the interfaces it implements.
interface org.varlink.service
# Get a list of all the interfaces a service provides and information
# about the service implementation.
method GetInfo() -> (
vendor: string,
product: string,
version: string,
url: string,
interfaces: []string
)
# Get the description of an interface that is implemented by this service.
method GetInterfaceDescription(interface: string) -> (description: string)
# The requested interface was not found.
error InterfaceNotFound (interface: string)
# The requested method was not found
error MethodNotFound (method: string)
# The interface defines the requested method, but the service does not
# implement it.
error MethodNotImplemented (method: string)
# One of the passed parameters is invalid.
error InvalidParameter (parameter: string)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("#[proxy(\"org.varlink.service\")]"));
assert!(code.contains("pub trait Service"));
assert!(code.contains("async fn get_info"));
assert!(code.contains("async fn get_interface_description"));
assert!(code.contains("#[derive(Debug, Clone, PartialEq, ReplyError)]"));
assert!(code.contains("#[zlink(interface = \"org.varlink.service\")]"));
assert!(code.contains("pub enum ServiceError"));
assert!(code.contains("InterfaceNotFound"));
assert!(code.contains("MethodNotFound"));
assert!(code.contains("MethodNotImplemented"));
assert!(code.contains("InvalidParameter"));
assert!(code.contains("interface: &str"));
assert!(code.contains("pub struct GetInfoOutput<'a>"));
assert!(code.contains("pub vendor: &'a str"));
assert!(code.contains("pub product: &'a str"));
assert!(code.contains("pub version: &'a str"));
assert!(code.contains("pub url: &'a str"));
assert!(code.contains("pub interfaces: Vec<&'a str>"));
}
#[test]
fn test_reference_types_in_proxy() {
let idl = r#"
interface org.example.refs
type CustomData (
field1: string,
field2: int
)
method SendString(text: string) -> ()
method SendArray(items: []string) -> ()
method SendMap(data: [string]int) -> ()
method SendCustom(data: CustomData) -> ()
method SendOptional(text: ?string, data: ?CustomData) -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("text: &str"));
assert!(code.contains("items: &[&str]"));
assert!(code.contains("data: &std::collections::HashMap<&str, i64>"));
assert!(code.contains("data: &CustomData"));
assert!(code.contains("text: Option<&str>"));
assert!(code.contains("data: Option<&CustomData>"));
}
#[test]
fn test_camelcase_rename_attributes() {
let idl_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("test-integration")
.join("camelcase.idl");
let idl = std::fs::read_to_string(idl_path).expect("Failed to read camelcase.idl");
let interface = Interface::try_from(idl.as_str()).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains(r#"#[zlink(rename = "userId")] user_id: i64"#));
assert!(code.contains(r#"#[zlink(rename = "includeEmail")] include_email: Option<bool>"#));
assert!(code.contains(r#"#[serde(rename = "userName")]"#));
assert!(code.contains("pub user_name: String"));
assert!(code.contains(r#"#[serde(rename = "emailAddress")]"#));
assert!(code.contains("pub email_address: Option<String>"));
assert!(code.contains(r#"#[serde(rename = "userData")]"#));
assert!(code.contains("pub user_data: UserData"));
assert!(code.contains(r#"#[serde(rename = "lastLogin")]"#));
assert!(code.contains("pub last_login: &'a str"));
assert!(code.contains("UserNotFound {"));
assert!(code.contains(r#"#[zlink(rename = "userId")]"#));
assert!(code.contains("user_id: i64"));
}
#[test]
fn test_interface_with_any_type() {
let idl = r#"
interface org.example.anytype
type Config (
name: string,
metadata: any,
extra: ?any,
items: []any,
tags: [string]any
)
method SetConfig(data: any) -> (result: any)
method GetData() -> (items: []any, map: [string]any)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert!(code.contains("pub metadata: serde_json::Value"));
assert!(code.contains("pub extra: Option<serde_json::Value>"));
assert!(code.contains("pub items: Vec<serde_json::Value>"));
assert!(code.contains("pub tags: std::collections::HashMap<String, serde_json::Value>"));
assert!(code.contains("data: &serde_json::Value"));
assert!(code.contains("serde_json::Value"));
assert!(code.contains("Vec<serde_json::Value>"));
assert!(code.contains("std::collections::HashMap<"));
}
#[test]
fn flags_proxy_methods() {
let code = flags_code();
assert_contains(
&code,
concat!(
" /// Lists the items.\n",
" async fn list(&mut self) -> zlink::Result<Result<ListOutput, FlagsError>>;\n",
" /// Lists the items.\n",
" ///\n",
" /// Calls the method with the `more` flag, streaming its replies.\n",
" #[zlink(more, rename = \"List\")]\n",
" async fn list_more(&mut self) -> zlink::Result<impl zlink::futures_util::Stream<",
"Item = zlink::Result<Result<ListOutput, FlagsError>>>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// Watches the items for changes.\n",
" #[zlink(more)]\n",
" async fn watch(&mut self) -> zlink::Result<impl zlink::futures_util::Stream<",
"Item = zlink::Result<Result<WatchOutput, FlagsError>>>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// Downloads an item over the upgraded connection.\n",
" #[zlink(upgrade)]\n",
" async fn download(&mut self, name: &str) -> ",
"zlink::Result<Result<DownloadOutput, FlagsError>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// Gets an item, or downloads it over the upgraded connection.\n",
" async fn fetch(&mut self, name: &str) -> ",
"zlink::Result<Result<FetchOutput, FlagsError>>;\n",
" /// Gets an item, or downloads it over the upgraded connection.\n",
" ///\n",
" /// Calls the method with the `upgrade` flag, so a successful reply upgrades the ",
"connection.\n",
" #[zlink(upgrade, rename = \"Fetch\")]\n",
" async fn fetch_upgrade(&mut self, name: &str) -> ",
"zlink::Result<Result<FetchOutput, FlagsError>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// Pings the service.\n",
" async fn ping(&mut self) -> zlink::Result<Result<(), FlagsError>>;\n",
),
);
assert_eq!(code.matches("async fn ").count(), 7);
}
#[test]
fn flags_owned_outputs() {
let code = flags_code();
assert_contains(&code, "pub struct ListOutput {\n pub item: Item,\n}");
assert_contains(
&code,
"pub struct WatchOutput {\n pub name: String,\n pub removed: bool,\n}",
);
assert_contains(
&code,
"pub struct DownloadOutput {\n pub size: i64,\n pub checksum: String,\n}",
);
assert!(!code.contains("'a"));
assert!(!code.contains("'_"));
assert!(!code.contains("serde(borrow)"));
let idl = r#"
interface org.example.mixed
# [Requires 'more' flag]
method Watch() -> (name: string)
# [Supports 'upgrade' flag]
method Fetch() -> (names: []string, kinds: [string]string)
method Name() -> (name: string)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(&code, "pub struct WatchOutput {\n pub name: String,\n}");
assert_contains(
&code,
concat!(
"pub struct FetchOutput {\n",
" pub names: Vec<String>,\n",
" pub kinds: std::collections::HashMap<String, String>,\n",
"}",
),
);
assert_contains(
&code,
"pub struct NameOutput<'a> {\n #[serde(borrow)]\n pub name: &'a str,\n}",
);
assert_contains(
&code,
"Item = zlink::Result<Result<WatchOutput, MixedError>>>>;",
);
assert_contains(
&code,
"async fn fetch(&mut self) -> zlink::Result<Result<FetchOutput, MixedError>>;",
);
assert_contains(
&code,
"async fn fetch_upgrade(&mut self) -> zlink::Result<Result<FetchOutput, MixedError>>;",
);
assert_contains(
&code,
"async fn name(&mut self) -> zlink::Result<Result<NameOutput<'_>, MixedError>>;",
);
}
#[test]
fn flags_comments_are_not_docs() {
let code = flags_code();
for comment in [
"[Supports 'more' flag]",
"[Requires 'more' flag]",
"[Supports 'upgrade' flag]",
"[Requires 'upgrade' flag]",
] {
assert!(!code.contains(comment), "`{comment}` found in:\n{code}");
}
let idl = concat!(
"interface org.example.docs\n",
"\n",
"# Opens the file.\n",
"# [Supports 'more' flag]\t\n",
"# It stays open until it's closed.\n",
"method Open() -> ()\n",
"\n",
"# [supports 'more' flag]\n",
"# [Supports 'more' flags]\n",
"# Supports 'more' flag\n",
"method Close() -> ()\n",
);
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" /// Opens the file.\n",
" /// It stays open until it's closed.\n",
" async fn open(&mut self) -> zlink::Result<Result<(), DocsError>>;\n",
" /// Opens the file.\n",
" /// It stays open until it's closed.\n",
" ///\n",
" /// Calls the method with the `more` flag, streaming its replies.\n",
" #[zlink(more, rename = \"Open\")]\n",
" async fn open_more(&mut self) -> zlink::Result<impl zlink::futures_util::Stream<",
"Item = zlink::Result<Result<(), DocsError>>>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// [supports 'more' flag]\n",
" /// [Supports 'more' flags]\n",
" /// Supports 'more' flag\n",
" async fn close(&mut self) -> zlink::Result<Result<(), DocsError>>;\n",
),
);
assert_eq!(code.matches("async fn ").count(), 3);
}
#[test]
fn flags_proxy_method_names() {
let idl = r#"
interface org.example.names
# [Supports 'more' flag]
# [Supports 'upgrade' flag]
method Open(path: string) -> (fd: int)
# [Requires 'more' flag]
# [Requires 'upgrade' flag]
method Tunnel() -> ()
# [Supports 'more' flag]
method Type() -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" async fn open(&mut self, path: &str) -> ",
"zlink::Result<Result<OpenOutput, NamesError>>;\n",
" /// Calls the method with the `more` flag, streaming its replies.\n",
" #[zlink(more, rename = \"Open\")]\n",
" async fn open_more(&mut self, path: &str) -> ",
"zlink::Result<impl zlink::futures_util::Stream<",
"Item = zlink::Result<Result<OpenOutput, NamesError>>>>;\n",
" /// Calls the method with the `upgrade` flag, so a successful reply upgrades the ",
"connection.\n",
" #[zlink(upgrade, rename = \"Open\")]\n",
" async fn open_upgrade(&mut self, path: &str) -> ",
"zlink::Result<Result<OpenOutput, NamesError>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// Calls the method with the `more` flag, streaming its replies.\n",
" #[zlink(more, rename = \"Tunnel\")]\n",
" async fn tunnel_more(&mut self) -> ",
"zlink::Result<impl zlink::futures_util::Stream<",
"Item = zlink::Result<Result<(), NamesError>>>>;\n",
" /// Calls the method with the `upgrade` flag, so a successful reply upgrades the ",
"connection.\n",
" #[zlink(upgrade, rename = \"Tunnel\")]\n",
" async fn tunnel_upgrade(&mut self) -> zlink::Result<Result<(), NamesError>>;\n",
),
);
assert!(!code.contains("fn tunnel("));
assert_contains(
&code,
" async fn r#type(&mut self) -> zlink::Result<Result<(), NamesError>>;\n",
);
assert_contains(
&code,
" #[zlink(more, rename = \"Type\")]\n async fn type_more(&mut self)",
);
}
#[test]
fn keyword_struct_fields() {
let code = names_code();
assert_contains(
&code,
concat!(
"pub struct Node {\n",
" #[serde(rename = \"self\")]\n",
" pub self_: String,\n",
" #[serde(rename = \"crate\")]\n",
" pub crate_: i64,\n",
" #[serde(rename = \"super\")]\n",
" pub super_: Option<String>,\n",
" #[serde(rename = \"type\")]\n",
" pub r#type: String,\n",
" #[serde(rename = \"try\")]\n",
" pub r#try: bool,\n",
" #[serde(rename = \"gen\")]\n",
" pub r#gen: Option<String>,\n",
"}\n",
),
);
}
#[test]
fn keyword_params_and_outputs() {
let code = names_code();
assert_contains(
&code,
concat!(
"async fn get_uuid(&mut self, #[zlink(rename = \"self\")] self_: &str, ",
"#[zlink(rename = \"crate\")] crate_: i64) -> ",
"zlink::Result<Result<GetUuidOutput<'_>, NamesError>>;\n",
),
);
assert_contains(
&code,
concat!(
"pub struct GetUuidOutput<'a> {\n",
" #[serde(borrow)]\n",
" #[serde(rename = \"super\")]\n",
" pub super_: &'a str,\n",
"}\n",
),
);
let idl = r#"
interface org.example.outputs
method Get() -> (type: string, super: int, try: bool)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
"pub struct GetOutput<'a> {\n",
" #[serde(borrow)]\n",
" pub r#type: &'a str,\n",
" #[serde(rename = \"super\")]\n",
" pub super_: i64,\n",
" pub r#try: bool,\n",
"}\n",
),
);
}
#[test]
fn keyword_error_fields() {
let code = names_code();
assert_contains(
&code,
concat!(
" HttpError {\n",
" #[zlink(rename = \"self\")]\n",
" self_: String,\n",
" },\n",
),
);
let idl = r#"
interface org.example.errors
error Failed(type: string, crate: int)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" Failed {\n",
" #[zlink(rename = \"type\")]\n",
" r#type: String,\n",
" #[zlink(rename = \"crate\")]\n",
" crate_: i64,\n",
" },\n",
),
);
}
#[test]
fn keyword_method_names() {
let code = names_code();
assert_contains(
&code,
" async fn self_(&mut self) -> zlink::Result<Result<(), NamesError>>;\n",
);
let idl = r#"
interface org.example.keywords
method Super() -> ()
method Crate() -> ()
method Type() -> ()
method Try() -> ()
# [Supports 'more' flag]
method Self() -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" async fn super_(&mut self) -> zlink::Result<Result<(), KeywordsError>>;\n",
" async fn crate_(&mut self) -> zlink::Result<Result<(), KeywordsError>>;\n",
" async fn r#type(&mut self) -> zlink::Result<Result<(), KeywordsError>>;\n",
" async fn r#try(&mut self) -> zlink::Result<Result<(), KeywordsError>>;\n",
" async fn self_(&mut self) -> zlink::Result<Result<(), KeywordsError>>;\n",
" /// Calls the method with the `more` flag, streaming its replies.\n",
" #[zlink(more, rename = \"Self\")]\n",
" async fn self_more(&mut self) -> ",
),
);
}
#[test]
fn keyword_type_names() {
let idl = r#"
interface org.example.keywords
type Self (value: int)
type Kind (Self, other)
error Self(self: string)
method Make(value: Self, values: []Self) -> (value: ?Self)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(&code, "pub struct Self_ {\n pub value: i64,\n}\n");
assert_contains(
&code,
concat!(
"async fn make(&mut self, value: &Self_, values: &[Self_]) -> ",
"zlink::Result<Result<MakeOutput, KeywordsError>>;\n",
),
);
assert_contains(
&code,
"pub struct MakeOutput {\n pub value: Option<Self_>,\n}\n",
);
assert_contains(
&code,
concat!(
"pub enum Kind {\n",
" #[serde(rename = \"Self\")]\n",
" Self_,\n",
" Other,\n",
"}\n",
),
);
assert_contains(
&code,
concat!(
" #[zlink(rename = \"Self\")]\n",
" Self_ {\n",
" #[zlink(rename = \"self\")]\n",
" self_: String,\n",
" },\n",
),
);
}
#[test]
fn keyword_trait_name() {
let idl = r#"
interface org.example.Self
method Ping() -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(&code, "#[proxy(\"org.example.Self\")]\npub trait Self_ {\n");
assert_contains(&code, "pub enum SelfError {}\n");
assert_contains(&code, "zlink::Result<Result<(), SelfError>>;\n");
}
#[test]
fn reserved_keywords() {
let reserved = [
"abstract", "become", "box", "do", "final", "gen", "macro", "override", "priv", "try",
"typeof", "unsized", "virtual", "yield",
];
let contextual = ["union", "macro_rules", "safe", "raw"];
let fields: Vec<_> = reserved
.iter()
.chain(&contextual)
.map(|name| format!("{name}: int"))
.collect();
let idl = format!(
"interface org.example.reserved\n\ntype Names ({})\n",
fields.join(", ")
);
let interface = Interface::try_from(idl.as_str()).unwrap();
let code = generate_interface(&interface).unwrap();
for name in reserved {
assert_contains(
&code,
&format!(" #[serde(rename = \"{name}\")]\n pub r#{name}: i64,\n"),
);
}
for name in contextual {
assert_contains(&code, &format!(" pub {name}: i64,\n"));
}
}
#[test]
fn method_names_that_map_back() {
let code = names_code();
assert_contains(
&code,
concat!(
" #[zlink(rename = \"GetUUID\")]\n",
" async fn get_uuid(&mut self, #[zlink(rename = \"self\")] self_: &str, ",
"#[zlink(rename = \"crate\")] crate_: i64) -> ",
"zlink::Result<Result<GetUuidOutput<'_>, NamesError>>;\n",
),
);
assert_contains(
&code,
concat!(
" /// A method that is named after a keyword.\n",
" async fn self_(&mut self) -> zlink::Result<Result<(), NamesError>>;\n",
),
);
let idl = r#"
interface org.example.methods
method GetUuid() -> ()
method HTTPGet() -> ()
method IPv4Address() -> ()
method ListUUIDs() -> ()
method Get2Things() -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" async fn get_uuid(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
" #[zlink(rename = \"HTTPGet\")]\n",
" async fn http_get(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
" async fn i_pv4_address(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
" #[zlink(rename = \"ListUUIDs\")]\n",
" async fn list_uui_ds(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
" async fn get2_things(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
),
);
}
#[test]
fn method_names_of_keywords_in_other_case() {
let idl = r#"
interface org.example.methods
method TYPE() -> ()
method SELF() -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" #[zlink(rename = \"TYPE\")]\n",
" async fn r#type(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
" #[zlink(rename = \"SELF\")]\n",
" async fn self_(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
),
);
}
#[test]
fn method_names_with_flags_that_map_back() {
let idl = r#"
interface org.example.methods
# [Supports 'more' flag]
method GetUUID() -> ()
# [Requires 'upgrade' flag]
method OpenFD() -> ()
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" #[zlink(rename = \"GetUUID\")]\n",
" async fn get_uuid(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
" /// Calls the method with the `more` flag, streaming its replies.\n",
" #[zlink(more, rename = \"GetUUID\")]\n",
" async fn get_uuid_more(&mut self) -> ",
"zlink::Result<impl zlink::futures_util::Stream<",
"Item = zlink::Result<Result<(), MethodsError>>>>;\n",
" #[zlink(upgrade, rename = \"OpenFD\")]\n",
" async fn open_fd(&mut self) -> zlink::Result<Result<(), MethodsError>>;\n",
),
);
}
#[test]
fn error_names_that_map_back() {
let code = names_code();
assert_contains(
&code,
concat!(
" #[zlink(rename = \"HTTPError\")]\n",
" HttpError {\n",
" #[zlink(rename = \"self\")]\n",
" self_: String,\n",
" },\n",
),
);
let idl = r#"
interface org.example.errors
# Not found.
error NotFound()
# Bad UTF-8.
error InvalidUTF8(offset: int)
error IOError()
error HTTP2Error(code: int)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
" /// Not found.\n",
" NotFound,\n",
" /// Bad UTF-8.\n",
" #[zlink(rename = \"InvalidUTF8\")]\n",
" InvalidUtf8 {\n",
" offset: i64,\n",
" },\n",
" #[zlink(rename = \"IOError\")]\n",
" IoError,\n",
" #[zlink(rename = \"HTTP2Error\")]\n",
" Http2Error {\n",
" code: i64,\n",
" },\n",
),
);
}
#[test]
fn enum_values_that_map_back() {
let code = names_code();
assert_contains(
&code,
concat!(
"#[serde(rename_all = \"snake_case\")]\n",
"pub enum Mode {\n",
" #[serde(rename = \"Fast\")]\n",
" Fast,\n",
" SlowDown,\n",
" #[serde(rename = \"HTTP\")]\n",
" Http,\n",
"}\n",
),
);
let idl = r#"
interface org.example.enums
type Kind (
foo,
foo_bar,
# The fast one.
Fast,
camelCase,
x_1,
a1,
UPPER
)
"#;
let interface = Interface::try_from(idl).unwrap();
let code = generate_interface(&interface).unwrap();
assert_contains(
&code,
concat!(
"#[serde(rename_all = \"snake_case\")]\n",
"pub enum Kind {\n",
" Foo,\n",
" FooBar,\n",
" /// The fast one.\n",
" #[serde(rename = \"Fast\")]\n",
" Fast,\n",
" #[serde(rename = \"camelCase\")]\n",
" CamelCase,\n",
" #[serde(rename = \"x_1\")]\n",
" X1,\n",
" A1,\n",
" #[serde(rename = \"UPPER\")]\n",
" Upper,\n",
"}\n",
),
);
}
fn flags_code() -> String {
let idl_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("test-integration")
.join("flags.idl");
let idl = std::fs::read_to_string(idl_path).expect("Failed to read flags.idl");
let interface = Interface::try_from(idl.as_str()).unwrap();
generate_interface(&interface).unwrap()
}
fn names_code() -> String {
let idl_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("test-integration")
.join("names.idl");
let idl = std::fs::read_to_string(idl_path).expect("Failed to read names.idl");
let interface = Interface::try_from(idl.as_str()).unwrap();
generate_interface(&interface).unwrap()
}
#[track_caller]
fn assert_contains(code: &str, expected: &str) {
assert!(
code.contains(expected),
"`{expected}` not found in:\n{code}"
);
}