zlink_macros/lib.rs
1#![doc(
2 html_logo_url = "https://raw.githubusercontent.com/z-galaxy/zlink/3660d731d7de8f60c8d82e122b3ece15617185e4/data/logo.png"
3)]
4#![deny(
5 missing_debug_implementations,
6 nonstandard_style,
7 rust_2018_idioms,
8 missing_docs
9)]
10#![warn(unreachable_pub)]
11#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
12
13mod utils;
14
15#[cfg(feature = "introspection")]
16mod introspect;
17
18mod reply_error;
19
20#[cfg(feature = "proxy")]
21mod proxy;
22
23#[cfg(feature = "service")]
24mod service;
25
26/// Derives `Type` for structs and enums, generating appropriate `Type::Object` or `Type::Enum`
27/// representation.
28///
29/// **Requires the `introspection` feature to be enabled.**
30///
31/// ## Structs
32///
33/// For structs, this macro supports named fields and unit structs. It will generate a
34/// `Type` implementation that creates a `Type::Object` containing all the fields with their
35/// names and types. Tuple structs are not supported as Varlink does not support unnamed fields.
36///
37/// ## Enums
38///
39/// For enums, this macro only supports unit variants (variants without associated data). It will
40/// generate a `Type` implementation that creates a `Type::Enum` containing all the variant
41/// names.
42///
43/// # Supported Attributes
44///
45/// The following attributes can be used to customize the behavior of this derive macro:
46///
47/// * `#[zlink(crate = "path")]` - Specifies the crate path to use for zlink types. Defaults to
48/// `::zlink`.
49///
50/// # Limitations
51///
52/// The following types are **not** supported by this macro:
53///
54/// - **Tuple structs**: Varlink does not support unnamed fields
55/// - **Enums with data**: Only unit enums (variants without associated data) are supported
56/// - **Unions**: Not supported by Varlink
57///
58/// ```rust,compile_fail
59/// # use zlink::introspect::Type;
60/// #[derive(Type)] // This will fail to compile
61/// struct Point(f32, f32, f32);
62/// ```
63///
64/// ```rust,compile_fail
65/// # use zlink::introspect::Type;
66/// #[derive(Type)] // This will fail to compile
67/// enum Status {
68/// Active(String), // Variants with data are not supported
69/// Inactive,
70/// }
71/// ```
72///
73/// # Examples
74///
75/// ## Named Structs
76///
77/// ```rust
78/// use zlink::introspect::Type;
79/// use zlink::idl;
80///
81/// #[derive(Type)]
82/// struct Person {
83/// name: String,
84/// age: i32,
85/// active: bool,
86/// }
87///
88/// // Access the generated type information
89/// match Person::TYPE {
90/// idl::Type::Object(fields) => {
91/// let field_vec: Vec<_> = fields.iter().collect();
92/// assert_eq!(field_vec.len(), 3);
93///
94/// assert_eq!(field_vec[0].name(), "name");
95/// assert_eq!(field_vec[0].ty(), &idl::Type::String);
96///
97/// assert_eq!(field_vec[1].name(), "age");
98/// assert_eq!(field_vec[1].ty(), &idl::Type::Int);
99///
100/// assert_eq!(field_vec[2].name(), "active");
101/// assert_eq!(field_vec[2].ty(), &idl::Type::Bool);
102/// }
103/// _ => panic!("Expected struct type"),
104/// }
105/// ```
106///
107/// ## Unit Structs
108///
109/// ```rust
110/// # use zlink::introspect::Type;
111/// # use zlink::idl;
112/// #[derive(Type)]
113/// struct Unit;
114///
115/// // Unit structs generate empty field lists
116/// match Unit::TYPE {
117/// idl::Type::Object(fields) => {
118/// assert_eq!(fields.len(), 0);
119/// }
120/// _ => panic!("Expected struct type"),
121/// }
122/// ```
123///
124/// ## Complex Types
125///
126/// ```rust
127/// # use zlink::introspect::Type;
128/// # use zlink::idl;
129/// #[derive(Type)]
130/// struct Complex {
131/// id: u64,
132/// description: Option<String>,
133/// tags: Vec<String>,
134/// }
135///
136/// // The macro handles nested types like Option<T> and Vec<T>
137/// match Complex::TYPE {
138/// idl::Type::Object(fields) => {
139/// let field_vec: Vec<_> = fields.iter().collect();
140///
141/// // Optional field becomes Type::Optional
142/// match field_vec[1].ty() {
143/// idl::Type::Optional(inner) => assert_eq!(inner.inner(), &idl::Type::String),
144/// _ => panic!("Expected optional type"),
145/// }
146///
147/// // Vec field becomes Type::Array
148/// match field_vec[2].ty() {
149/// idl::Type::Array(inner) => assert_eq!(inner.inner(), &idl::Type::String),
150/// _ => panic!("Expected array type"),
151/// }
152/// }
153/// _ => panic!("Expected struct type"),
154/// }
155/// ```
156///
157/// ## Unit Enums
158///
159/// ```rust
160/// # use zlink::introspect::Type;
161/// # use zlink::idl;
162/// #[derive(Type)]
163/// enum Status {
164/// Active,
165/// Inactive,
166/// Pending,
167/// }
168///
169/// // Unit enums generate variant lists
170/// match Status::TYPE {
171/// idl::Type::Enum(variants) => {
172/// let variant_vec: Vec<_> = variants.iter().collect();
173/// assert_eq!(variant_vec.len(), 3);
174/// assert_eq!(variant_vec[0].name(), "Active");
175/// assert_eq!(variant_vec[1].name(), "Inactive");
176/// assert_eq!(variant_vec[2].name(), "Pending");
177/// }
178/// _ => panic!("Expected enum type"),
179/// }
180/// ```
181#[cfg(feature = "introspection")]
182#[proc_macro_derive(IntrospectType, attributes(zlink))]
183pub fn derive_introspect_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
184 introspect::r#type::derive_type(input)
185}
186
187/// Derives `Type` for structs and enums, generating named custom type definitions.
188///
189/// **Requires the `introspection` feature to be enabled.**
190///
191/// This macro generates implementations of the `CustomType` trait, which provides named
192/// custom type definitions suitable for IDL generation. It also generates a `Type` implementation
193/// and therefore is mutually exclusive to `zlink::introspect::Type` derive macro.
194///
195/// ## Structs
196///
197/// For structs, this macro generates a `custom::Type::Object` containing the struct name and
198/// all fields with their names and types.
199///
200/// ## Enums
201///
202/// For enums, this macro only supports unit variants and generates a `custom::Type::Enum`
203/// containing the enum name and all variant names.
204///
205/// # Supported Attributes
206///
207/// The following attributes can be used to customize the behavior of this derive macro:
208///
209/// * `#[zlink(crate = "path")]` - Specifies the crate path to use for zlink types. Defaults to
210/// `::zlink`.
211///
212/// # Examples
213///
214/// ## Named Structs
215///
216/// ```rust
217/// use zlink::introspect::{CustomType, Type};
218/// use zlink::idl;
219///
220/// #[derive(CustomType)]
221/// struct Point {
222/// x: f64,
223/// y: f64,
224/// }
225///
226/// // Access the generated custom type information
227/// match Point::CUSTOM_TYPE {
228/// idl::CustomType::Object(obj) => {
229/// assert_eq!(obj.name(), "Point");
230/// let fields: Vec<_> = obj.fields().collect();
231/// assert_eq!(fields.len(), 2);
232/// assert_eq!(fields[0].name(), "x");
233/// assert_eq!(fields[1].name(), "y");
234/// }
235/// _ => panic!("Expected custom object type"),
236/// }
237///
238/// match Point::TYPE {
239/// idl::Type::Custom(name) => {
240/// assert_eq!(*name, "Point");
241/// }
242/// _ => panic!("Expected custom type"),
243/// }
244/// ```
245///
246/// ## Unit Enums
247///
248/// ```rust
249/// # use zlink::introspect::{CustomType, Type};
250/// # use zlink::idl;
251/// #[derive(CustomType)]
252/// enum Status {
253/// Active,
254/// Inactive,
255/// Pending,
256/// }
257///
258/// // Access the generated custom enum type information
259/// match Status::CUSTOM_TYPE {
260/// idl::CustomType::Enum(enm) => {
261/// assert_eq!(enm.name(), "Status");
262/// let variants: Vec<_> = enm.variants().collect();
263/// assert_eq!(variants.len(), 3);
264/// assert_eq!(variants[0].name(), "Active");
265/// assert_eq!(variants[1].name(), "Inactive");
266/// assert_eq!(variants[2].name(), "Pending");
267/// }
268/// _ => panic!("Expected custom enum type"),
269/// }
270///
271/// match Status::TYPE {
272/// idl::Type::Custom(name) => {
273/// assert_eq!(*name, "Status");
274/// }
275/// _ => panic!("Expected custom type"),
276/// }
277/// ```
278#[cfg(feature = "introspection")]
279#[proc_macro_derive(IntrospectCustomType, attributes(zlink))]
280pub fn derive_introspect_custom_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
281 introspect::custom_type::derive_custom_type(input)
282}
283
284/// Derives `ReplyError` for enums, generating error definitions for Varlink service errors.
285///
286/// **Requires the `introspection` feature to be enabled.**
287///
288/// This macro generates implementations of the `ReplyError` trait, which provides a list of
289/// error variants that can be returned by a Varlink service method. It supports unit variants,
290/// variants with named fields, and single-field tuple variants (where the field type implements
291/// `Type` and has a `Type::Object`).
292///
293/// # Supported Attributes
294///
295/// The following attributes can be used to customize the behavior of this derive macro:
296///
297/// * `#[zlink(crate = "path")]` - Specifies the crate path to use for zlink types. Defaults to
298/// `::zlink`.
299///
300/// # Example
301///
302/// ```rust
303/// use zlink::introspect::ReplyError;
304///
305/// #[derive(ReplyError)]
306/// enum ServiceError {
307/// // Unit variant - no parameters
308/// NotFound,
309///
310/// // Named field variant - multiple parameters
311/// InvalidQuery {
312/// message: String,
313/// line: u32,
314/// },
315///
316/// // Single tuple variant - uses fields from the wrapped type
317/// ValidationFailed(ValidationDetails),
318/// }
319///
320/// // Example struct for tuple variant
321/// #[derive(zlink::introspect::Type)]
322/// struct ValidationDetails {
323/// field_name: String,
324/// expected: String,
325/// }
326///
327/// // Access the generated error variants
328/// assert_eq!(ServiceError::VARIANTS.len(), 3);
329/// assert_eq!(ServiceError::VARIANTS[0].name(), "NotFound");
330/// assert!(ServiceError::VARIANTS[0].has_no_fields());
331///
332/// assert_eq!(ServiceError::VARIANTS[1].name(), "InvalidQuery");
333/// assert!(!ServiceError::VARIANTS[1].has_no_fields());
334/// ```
335#[cfg(feature = "introspection")]
336#[proc_macro_derive(IntrospectReplyError, attributes(zlink))]
337pub fn derive_introspect_reply_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
338 introspect::reply_error::derive_reply_error(input)
339}
340
341/// Creates a client-side proxy for calling Varlink methods on a connection.
342///
343/// **Requires the `proxy` feature to be enabled.**
344///
345/// This attribute macro generates an implementation of the provided trait for `Connection<S>`,
346/// automatically handling the serialization of method calls and deserialization of responses.
347/// Each proxy trait targets a single Varlink interface.
348///
349/// The macro also generates a chain extension trait that allows you to chain multiple method
350/// calls together for efficient batching across multiple interfaces.
351///
352/// # Supported Attributes
353///
354/// The following attributes can be used to customize the behavior of this macro:
355///
356/// * `interface` (required) - The Varlink interface name (e.g., `"org.varlink.service"`).
357/// * `crate` - Specifies the crate path to use for zlink types. Defaults to `::zlink`.
358/// * `chain_name` - Custom name for the generated chain extension trait. Defaults to
359/// `{TraitName}Chain`.
360///
361/// # Example
362///
363/// ```rust
364/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
365/// use zlink::proxy;
366/// use serde::{Deserialize, Serialize};
367/// use serde_prefix_all::prefix_all;
368/// use futures_util::stream::Stream;
369///
370/// #[proxy("org.example.MyService")]
371/// trait MyServiceProxy {
372/// // Non-streaming methods can use borrowed types.
373/// async fn get_status(&mut self) -> zlink::Result<Result<Status<'_>, MyError<'_>>>;
374/// async fn set_value(
375/// &mut self,
376/// key: &str,
377/// value: i32,
378/// ) -> zlink::Result<Result<(), MyError<'_>>>;
379/// #[zlink(rename = "ListMachines")]
380/// async fn list_machines(&mut self) -> zlink::Result<Result<Vec<Machine<'_>>, MyError<'_>>>;
381/// // Streaming methods must use owned types (DeserializeOwned) because the internal buffer may
382/// // be reused between stream iterations.
383/// #[zlink(rename = "GetStatus", more)]
384/// async fn stream_status(
385/// &mut self,
386/// ) -> zlink::Result<
387/// impl Stream<Item = zlink::Result<Result<OwnedStatus, OwnedMyError>>>,
388/// >;
389/// }
390///
391/// // The macro generates:
392/// // impl<S: Socket> MyServiceProxy for Connection<S> { ... }
393///
394/// // Borrowed types for non-streaming methods.
395/// #[derive(Debug, Serialize, Deserialize)]
396/// struct Status<'m> {
397/// active: bool,
398/// message: &'m str,
399/// }
400///
401/// #[derive(Debug, Serialize, Deserialize)]
402/// struct Machine<'m> { name: &'m str }
403///
404/// #[prefix_all("org.example.MyService.")]
405/// #[derive(Debug, Serialize, Deserialize)]
406/// #[serde(tag = "error", content = "parameters")]
407/// enum MyError<'a> {
408/// NotFound,
409/// InvalidRequest,
410/// // Parameters must be named.
411/// CodedError { code: u32, message: &'a str },
412/// }
413///
414/// // Owned types for streaming methods (required by the `more` attribute).
415/// #[derive(Debug, Serialize, Deserialize)]
416/// struct OwnedStatus {
417/// active: bool,
418/// message: String,
419/// }
420///
421/// #[prefix_all("org.example.MyService.")]
422/// #[derive(Debug, Serialize, Deserialize)]
423/// #[serde(tag = "error", content = "parameters")]
424/// enum OwnedMyError {
425/// NotFound,
426/// InvalidRequest,
427/// CodedError { code: u32, message: String },
428/// }
429///
430/// // Example usage:
431/// # use zlink::test_utils::mock_socket::MockSocket;
432/// # let responses = vec![
433/// # r#"{"parameters":{"active":true,"message":"System running"}}"#,
434/// # ];
435/// # let socket = MockSocket::with_responses(&responses);
436/// # let mut conn = zlink::Connection::new(socket);
437/// let result = conn.get_status().await?.unwrap();
438/// assert_eq!(result.active, true);
439/// assert_eq!(result.message, "System running");
440/// # Ok::<(), Box<dyn std::error::Error>>(())
441/// # }).unwrap();
442/// ```
443///
444/// # Chaining Method Calls
445///
446/// The proxy macro generates chain extension traits that allow you to batch multiple method calls
447/// together. This is useful for reducing round trips and efficiently calling methods across
448/// multiple interfaces. Each method gets a `chain_` prefixed variant that starts a chain.
449///
450/// **Important**: Chain methods are only generated for proxy methods that use owned types
451/// (`DeserializeOwned`) in their return type. Methods with borrowed types (non-static lifetimes)
452/// don't get chain variants since the internal buffer may be reused between stream iterations.
453/// Input arguments can still use borrowed types.
454///
455/// ## Example: Chaining Method Calls
456///
457/// ```rust
458/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
459/// # use zlink::proxy;
460/// # use serde::{Deserialize, Serialize};
461/// # use futures_util::{pin_mut, TryStreamExt};
462/// #
463/// // Owned reply types for chain API.
464/// # #[derive(Debug, Serialize, Deserialize)]
465/// # struct User { id: u64, name: String }
466/// # #[derive(Debug, Serialize, Deserialize)]
467/// # struct Post { id: u64, user_id: u64, content: String }
468/// # #[derive(Debug, Serialize, Deserialize)]
469/// # #[serde(untagged)]
470/// # enum BlogReply {
471/// # User(User),
472/// # Post(Post),
473/// # Posts(Vec<Post>)
474/// # }
475/// # #[derive(Debug, Serialize, Deserialize)]
476/// # #[serde(tag = "error")]
477/// # enum BlogError { NotFound, InvalidInput }
478/// # impl std::fmt::Display for BlogError {
479/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
480/// # match self {
481/// # Self::NotFound => write!(f, "Not found"),
482/// # Self::InvalidInput => write!(f, "Invalid input")
483/// # }
484/// # }
485/// # }
486/// # impl std::error::Error for BlogError {}
487/// #
488/// // Define proxies with owned return types - chain methods are generated.
489/// #[proxy("org.example.blog.Users")]
490/// trait UsersProxy {
491/// async fn get_user(&mut self, id: u64)
492/// -> zlink::Result<Result<BlogReply, BlogError>>;
493/// async fn create_user(&mut self, name: &str)
494/// -> zlink::Result<Result<BlogReply, BlogError>>;
495/// }
496///
497/// #[proxy("org.example.blog.Posts")]
498/// trait PostsProxy {
499/// async fn get_posts_by_user(&mut self, user_id: u64)
500/// -> zlink::Result<Result<BlogReply, BlogError>>;
501/// async fn create_post(&mut self, user_id: u64, content: &str)
502/// -> zlink::Result<Result<BlogReply, BlogError>>;
503/// }
504///
505/// # use zlink::test_utils::mock_socket::MockSocket;
506/// # let responses = vec![
507/// # r#"{"parameters":{"id":1,"name":"Alice"}}"#,
508/// # r#"{"parameters":{"id":1,"user_id":1,"content":"My first post!"}}"#,
509/// # r#"{"parameters":[{"id":1,"user_id":1,"content":"My first post!"}]}"#,
510/// # r#"{"parameters":{"id":1,"name":"Alice"}}"#,
511/// # ];
512/// # let socket = MockSocket::with_responses(&responses);
513/// # let mut conn = zlink::Connection::new(socket);
514/// let chain = conn
515/// .chain_create_user("Alice")?
516/// .create_post(1, "My first post!")?
517/// .get_posts_by_user(1)?
518/// .get_user(1)?;
519///
520/// // Send all calls in a single batch.
521/// let replies = chain.send::<BlogReply, BlogError>().await?;
522/// pin_mut!(replies);
523///
524/// // Process replies in order.
525/// let mut reply_count = 0;
526/// while let Some((reply, _fds)) = replies.try_next().await? {
527/// reply_count += 1;
528/// if let Ok(response) = reply {
529/// match response.parameters() {
530/// Some(BlogReply::User(user)) => assert_eq!(user.name, "Alice"),
531/// Some(BlogReply::Post(post)) => assert_eq!(post.content, "My first post!"),
532/// Some(BlogReply::Posts(posts)) => assert_eq!(posts.len(), 1),
533/// None => {} // set_value returns empty response
534/// }
535/// }
536/// }
537/// assert_eq!(reply_count, 4); // We made 4 calls
538/// # Ok::<(), Box<dyn std::error::Error>>(())
539/// # }).unwrap();
540/// ```
541///
542/// ## Combining Multiple Services
543///
544/// You can chain calls across multiple custom services. Define a combined reply type that can
545/// deserialize responses from all interfaces:
546///
547/// ```rust
548/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
549/// # use zlink::proxy;
550/// # use serde::{Deserialize, Serialize};
551/// # use futures_util::{pin_mut, TryStreamExt};
552/// #
553/// # #[derive(Debug, Serialize, Deserialize)]
554/// # struct Status { active: bool, message: String }
555/// # #[derive(Debug, Serialize, Deserialize)]
556/// # struct HealthInfo { healthy: bool, uptime: u64 }
557/// # #[derive(Debug, Serialize, Deserialize)]
558/// # #[serde(tag = "error")]
559/// # enum ServiceError { NotFound, InvalidRequest }
560/// # impl std::fmt::Display for ServiceError {
561/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
562/// # match self {
563/// # Self::NotFound => write!(f, "Not found"),
564/// # Self::InvalidRequest => write!(f, "Invalid input")
565/// # }
566/// # }
567/// # }
568/// # impl std::error::Error for ServiceError {}
569/// #
570/// // Multiple proxies with owned return types.
571/// #[proxy("com.example.StatusService")]
572/// trait StatusProxy {
573/// async fn get_status(&mut self) -> zlink::Result<Result<Status, ServiceError>>;
574/// }
575///
576/// #[proxy("com.example.HealthService")]
577/// trait HealthProxy {
578/// async fn get_health(&mut self) -> zlink::Result<Result<HealthInfo, ServiceError>>;
579/// }
580///
581/// // Combined reply type for cross-interface chaining.
582/// #[derive(Debug, Deserialize)]
583/// #[serde(untagged)]
584/// enum CombinedReply {
585/// Status(Status),
586/// Health(HealthInfo),
587/// }
588///
589/// # use zlink::test_utils::mock_socket::MockSocket;
590/// # let responses = vec![
591/// # r#"{"parameters":{"active":true,"message":"Running"}}"#,
592/// # r#"{"parameters":{"healthy":true,"uptime":12345}}"#,
593/// # ];
594/// # let socket = MockSocket::with_responses(&responses);
595/// # let mut conn = zlink::Connection::new(socket);
596/// // Chain calls across both services.
597/// let chain = conn
598/// .chain_get_status()?
599/// .get_health()?;
600///
601/// let replies = chain.send::<CombinedReply, ServiceError>().await?;
602/// pin_mut!(replies);
603///
604/// let mut count = 0;
605/// while let Some((reply, _fds)) = replies.try_next().await? {
606/// count += 1;
607/// if let Ok(response) = reply {
608/// match response.parameters() {
609/// Some(CombinedReply::Status(s)) => println!("Status: {}", s.message),
610/// Some(CombinedReply::Health(h)) => println!("Uptime: {}", h.uptime),
611/// None => {}
612/// }
613/// }
614/// }
615/// assert_eq!(count, 2);
616/// # Ok::<(), Box<dyn std::error::Error>>(())
617/// # }).unwrap();
618/// ```
619///
620/// ## Chain Extension Traits
621///
622/// For each proxy trait, the macro generates a corresponding chain extension trait. For example,
623/// `FtlProxy` gets `FtlProxyChain`. This trait is automatically implemented for `Chain` types,
624/// allowing seamless method chaining across interfaces.
625///
626/// # Method Requirements
627///
628/// Proxy methods must:
629/// - Take `&mut self` as the first parameter
630/// - Can be either `async fn` or return `impl Future`
631/// - Return `zlink::Result<Result<ReplyType, ErrorType>>` (outer Result for connection errors,
632/// inner for method errors)
633/// - The arguments can be any type that implement `serde::Serialize`
634/// - The reply type (`Ok` case of the inner `Result`) must be a type that implements
635/// `serde::Deserialize` and deserializes itself from a JSON object. Typically you'd just use a
636/// struct that derives `serde::Deserialize`.
637/// - The reply error type (`Err` case of the inner `Result`) must be a type `serde::Deserialize`
638/// that deserializes itself from a JSON object with two fields:
639/// - `error`: a string containing the fully qualified error name
640/// - `parameters`: an optional object containing all the fields of the error
641///
642/// # Method Names
643///
644/// By default, method names are converted from snake_case to PascalCase for the Varlink call.
645/// To specify a different Varlink method name, use the `#[zlink(rename = "...")]` attribute. See
646/// `list_machines` in the example above.
647///
648/// # Streaming Methods
649///
650/// For methods that support streaming (the 'more' flag), use the `#[zlink(more)]` attribute.
651/// Streaming methods must return `Result<impl Stream<Item = Result<Result<ReplyType,
652/// ErrorType>>>>`. The proxy will automatically set the 'more' flag on the call and return a
653/// stream of replies.
654///
655/// # One-way Methods
656///
657/// For fire-and-forget methods that don't expect a reply, use the `#[zlink(oneway)]` attribute.
658/// One-way methods send the call and return immediately without waiting for a response. The method
659/// must return `zlink::Result<()>` (just the outer Result for connection errors, no inner Result
660/// since there's no reply to process).
661///
662/// One-way methods cannot be combined with `#[zlink(more)]` or `#[zlink(return_fds)]`.
663///
664/// This attribute is particularly useful in combination with chaining method calls. When you chain
665/// oneway methods with regular methods, the oneway calls are sent but don't contribute to the reply
666/// stream. For example, if you chain 4 calls where 2 are regular and 2 are oneway, you'll only
667/// receive 2 replies. This allows you to efficiently batch side-effect operations (like resets or
668/// notifications) alongside queries in a single round-trip.
669///
670/// ```rust
671/// # use zlink::proxy;
672/// # use serde::{Deserialize, Serialize};
673/// #[proxy("org.example.Notifications")]
674/// trait NotificationsProxy {
675/// /// Fire-and-forget notification - returns immediately without waiting for a reply.
676/// #[zlink(oneway)]
677/// async fn notify(&mut self, message: &str) -> zlink::Result<()>;
678///
679/// /// Another one-way method with multiple parameters.
680/// #[zlink(oneway)]
681/// async fn log_event(&mut self, level: &str, message: &str, timestamp: u64)
682/// -> zlink::Result<()>;
683/// }
684/// ```
685///
686/// # File Descriptor Passing
687///
688/// **Requires the `std` feature to be enabled.**
689///
690/// Methods can send and receive file descriptors using the following attributes:
691///
692/// ## Sending File Descriptors
693///
694/// Use `#[zlink(fds)]` on a parameter of type `Vec<OwnedFd>` to send file descriptors with the
695/// method call. Only one FD parameter is allowed per method.
696///
697/// ## Receiving File Descriptors
698///
699/// Use `#[zlink(return_fds)]` on a method to indicate it returns file descriptors. The method's
700/// return type must be `Result<(Result<ReplyType, ErrorType>, Vec<OwnedFd>)>` - a tuple containing
701/// both the method result and the received file descriptors. The FDs are available regardless of
702/// whether the method succeeded or failed.
703///
704/// ## Example: File Descriptor Passing
705///
706/// File descriptors are passed out-of-band from the encoded JSON parameters. The typical pattern
707/// is to include integer indexes in your JSON parameters that reference positions in the FD
708/// vector. This is similar to how D-Bus handles FD passing.
709///
710/// ```rust
711/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
712/// use zlink::proxy;
713/// use serde::{Deserialize, Serialize};
714/// use std::os::fd::OwnedFd;
715///
716/// #[proxy("org.example.FileService")]
717/// trait FileServiceProxy {
718/// // Send file descriptors to the service
719/// // The stdin/stdout parameters are indexes into the FDs vector
720/// async fn spawn_process(
721/// &mut self,
722/// command: String,
723/// stdin_fd: u32,
724/// stdout_fd: u32,
725/// #[zlink(fds)] fds: Vec<OwnedFd>,
726/// ) -> zlink::Result<Result<ProcessInfo, FileError>>;
727///
728/// // Receive file descriptors from the service
729/// // Returns metadata with FD indexes and the actual FDs
730/// #[zlink(return_fds)]
731/// async fn open_files(
732/// &mut self,
733/// paths: Vec<String>,
734/// ) -> zlink::Result<(Result<Vec<FileInfo>, FileError>, Vec<OwnedFd>)>;
735/// }
736///
737/// #[derive(Debug, Serialize, Deserialize)]
738/// struct ProcessInfo {
739/// pid: u32,
740/// }
741///
742/// // Response contains FD indexes referencing the FD vector
743/// #[derive(Debug, Serialize, Deserialize)]
744/// struct FileInfo {
745/// path: String,
746/// fd: u32, // Index into the FD vector (0, 1, 2, etc.)
747/// }
748///
749/// #[derive(Debug, Serialize, Deserialize)]
750/// #[serde(tag = "error")]
751/// enum FileError {
752/// NotFound { path: String },
753/// PermissionDenied { path: String },
754/// }
755/// # impl std::error::Error for FileError {}
756/// # impl std::fmt::Display for FileError {
757/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
758/// # match self {
759/// # FileError::NotFound { path } => write!(f, "File not found: {}", path),
760/// # FileError::PermissionDenied { path } =>
761/// # write!(f, "Permission denied: {}", path),
762/// # }
763/// # }
764/// # }
765///
766/// // Example usage:
767/// # use std::os::unix::net::UnixStream;
768/// # use zlink::test_utils::mock_socket::MockSocket;
769/// // Sending FDs: Pass indexes as regular parameters
770/// # let (stdin_pipe, _w1) = UnixStream::pair()?;
771/// # let (stdout_pipe, _w2) = UnixStream::pair()?;
772/// # let send_response = r#"{"parameters":{"pid":1234}}"#;
773/// # let send_socket = MockSocket::with_responses(&[send_response]);
774/// # let mut send_conn = zlink::Connection::new(send_socket);
775/// let fds = vec![stdin_pipe.into(), stdout_pipe.into()];
776/// // Parameters reference FD indexes: stdin_fd=0, stdout_fd=1
777/// let result = send_conn.spawn_process("/bin/cat".to_string(), 0, 1, fds).await?;
778/// let process_info = result?;
779/// assert_eq!(process_info.pid, 1234);
780///
781/// // Receiving FDs: Response contains indexes that reference the FD vector
782/// # let (file1, _w3) = UnixStream::pair()?;
783/// # let (file2, _w4) = UnixStream::pair()?;
784/// # let recv_response = r#"{
785/// # "parameters": [
786/// # {"path": "/etc/config.txt", "fd": 0},
787/// # {"path": "/var/data.bin", "fd": 1}
788/// # ]
789/// # }"#;
790/// # let recv_fds = vec![vec![file1.into(), file2.into()]];
791/// # let recv_socket = MockSocket::new(&[recv_response], recv_fds);
792/// # let mut recv_conn = zlink::Connection::new(recv_socket);
793/// let (result, received_fds) = recv_conn
794/// .open_files(vec!["/etc/config.txt".to_string(), "/var/data.bin".to_string()])
795/// .await?;
796/// let file_list = result?;
797/// assert_eq!(file_list.len(), 2);
798/// assert_eq!(received_fds.len(), 2);
799/// // Use the fd field to match file info with actual FDs
800/// for file_info in &file_list {
801/// let fd = &received_fds[file_info.fd as usize];
802/// println!("File {} has FD at index {}", file_info.path, file_info.fd);
803/// }
804/// # Ok::<(), Box<dyn std::error::Error>>(())
805/// # }).unwrap();
806/// ```
807///
808/// ## Parameter Renaming
809///
810/// Use `#[zlink(rename = "name")]` on parameters to customize their serialized names in the
811/// Varlink protocol. This is useful when the Rust parameter name doesn't match the expected
812/// Varlink parameter name.
813///
814/// ```rust
815/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
816/// # use zlink::proxy;
817/// # use serde::{Deserialize, Serialize};
818/// #[proxy("org.example.Users")]
819/// trait UsersProxy {
820/// async fn create_user(
821/// &mut self,
822/// #[zlink(rename = "user_name")] name: String,
823/// #[zlink(rename = "user_email")] email: String,
824/// ) -> zlink::Result<Result<UserId, UserError>>;
825/// }
826/// # #[derive(Debug, Serialize, Deserialize)]
827/// # struct UserId { id: u32 }
828/// # #[derive(Debug, Serialize, Deserialize)]
829/// # #[serde(tag = "error")]
830/// # enum UserError { InvalidInput }
831/// # Ok::<(), Box<dyn std::error::Error>>(())
832/// # }).unwrap();
833/// ```
834///
835/// # Generic Parameters
836///
837/// The proxy macro supports generic type parameters on individual methods. Note that generic
838/// parameters on the trait itself are not currently supported.
839///
840/// ```rust
841/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
842/// # use zlink::proxy;
843/// # use serde::{Deserialize, Serialize};
844/// # #[derive(Debug, Serialize, Deserialize)]
845/// # struct StoredValue<T> { data: T }
846/// # #[derive(Debug, Serialize, Deserialize)]
847/// # struct ProcessReply<'a> { result: &'a str }
848/// # #[derive(Debug, Serialize, Deserialize)]
849/// # #[serde(tag = "error")]
850/// # enum StorageError { NotFound }
851/// # impl std::fmt::Display for StorageError {
852/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
853/// # write!(f, "Storage error")
854/// # }
855/// # }
856/// # impl std::error::Error for StorageError {}
857/// #
858/// #[proxy("org.example.Storage")]
859/// trait StorageProxy {
860/// // Method-level generics with trait bounds
861/// async fn store<'a, T: Serialize + std::fmt::Debug>(
862/// &mut self,
863/// key: &'a str,
864/// value: T,
865/// ) -> zlink::Result<Result<(), StorageError>>;
866///
867/// // Generic methods with where clauses
868/// async fn process<T>(&mut self, data: T)
869/// -> zlink::Result<Result<ProcessReply<'_>, StorageError>>
870/// where
871/// T: Serialize + std::fmt::Debug;
872///
873/// // Methods can use generic type parameters in both input and output
874/// async fn store_and_return<'a, T>(&mut self, key: &'a str, value: T)
875/// -> zlink::Result<Result<StoredValue<T>, StorageError>>
876/// where
877/// T: Serialize + for<'de> Deserialize<'de> + std::fmt::Debug;
878/// }
879///
880/// // Example usage:
881/// # use zlink::test_utils::mock_socket::MockSocket;
882/// # let responses = vec![
883/// # r#"{"parameters":null}"#, // store returns empty Ok
884/// # ];
885/// # let socket = MockSocket::with_responses(&responses);
886/// # let mut conn = zlink::Connection::new(socket);
887/// // Store a value with generic type
888/// let result = conn.store("my-key", 42i32).await?;
889/// assert!(result.is_ok());
890/// # Ok::<(), Box<dyn std::error::Error>>(())
891/// # }).unwrap();
892/// ```
893#[cfg(feature = "proxy")]
894#[proc_macro_attribute]
895pub fn proxy(
896 attr: proc_macro::TokenStream,
897 input: proc_macro::TokenStream,
898) -> proc_macro::TokenStream {
899 proxy::proxy(attr.into(), input.into()).into()
900}
901
902/// Implements `serde::{Serialize, Deserialize}` for service error enums.
903///
904/// This macro automatically generates both `Serialize` and `Deserialize` implementations for error
905/// types that are used in Varlink service replies.
906///
907/// The macro works in both `std` and `no_std` environments and requires the "error" field
908/// to appear before "parameters" field in JSON for efficient parsing.
909///
910/// # Supported Enum Variants
911///
912/// The macro supports:
913/// - **Unit variants**: Variants without any data
914/// - **Named field variants**: Variants with named fields
915///
916/// Tuple variants are **not** supported.
917///
918/// # Attributes
919///
920/// ## Enum-level attributes
921///
922/// - `interface` - This mandatory attribute specifies the Varlink interface name (e.g.,
923/// "org.varlink.service")
924///
925/// ## Field-level attributes
926///
927/// - `rename = "..."` - Specifies a custom name for the field in the JSON representation
928/// - `borrow` - Enables zero-copy deserialization for types like `Cow<'_, str>`
929///
930/// # Example
931///
932/// ```rust
933/// use std::borrow::Cow;
934/// use zlink::ReplyError;
935///
936/// #[derive(ReplyError)]
937/// #[zlink(interface = "com.example.MyService")]
938/// enum ServiceError<'a> {
939/// // Unit variant - no parameters
940/// NotFound,
941/// PermissionDenied,
942///
943/// // Named field variant - multiple parameters
944/// InvalidInput {
945/// field: String,
946/// reason: String,
947/// },
948///
949/// // Variant with zero-copy deserialization using borrow
950/// CustomError {
951/// #[zlink(borrow)]
952/// message: Cow<'a, str>,
953/// },
954///
955/// // Variant with renamed field
956/// Timeout {
957/// #[zlink(rename = "timeoutSeconds")]
958/// seconds: u32,
959/// },
960/// }
961///
962/// // The macro generates:
963/// // - `Serialize` impl that creates properly tagged enum format
964/// // - `Deserialize` impl that handles the tagged enum format efficiently
965/// ```
966///
967/// # Serialization Format
968///
969/// The generated serialization uses a tagged enum format:
970///
971/// ```json
972/// // Unit variant:
973/// {"error": "NotFound"}
974/// // or with empty parameters:
975/// {"error": "NotFound", "parameters": null}
976///
977/// // Variant with fields:
978/// {
979/// "error": "InvalidInput",
980/// "parameters": {
981/// "field": "username",
982/// "reason": "too short"
983/// }
984/// }
985/// ```
986#[proc_macro_derive(ReplyError, attributes(zlink))]
987pub fn derive_reply_error(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
988 reply_error::derive_reply_error(input)
989}
990
991/// Transforms an impl block into a `Service` trait implementation.
992///
993/// **Requires the `service` feature to be enabled.** The `service` feature automatically enables
994/// the `introspection` feature, allowing the macro to generate interface descriptions and handle
995/// the standard `org.varlink.service` interface automatically.
996///
997/// This attribute macro takes a regular impl block and generates the necessary code to implement
998/// the `Service` trait, enabling the type to handle Varlink method calls.
999///
1000/// # Automatic Introspection Support
1001///
1002/// The generated service automatically handles the `org.varlink.service` interface:
1003///
1004/// - **`GetInfo`**: Returns service metadata (vendor, product, version, URL) and a list of all
1005/// implemented interfaces.
1006/// - **`GetInterfaceDescription`**: Returns the IDL description for any implemented interface,
1007/// generated at compile time from the method signatures and types.
1008/// - **Unknown methods**: Return `MethodNotFound` error with the method name.
1009/// - **Unknown interfaces** (in `GetInterfaceDescription`): Return `InterfaceNotFound` error.
1010///
1011/// # Supported Attributes
1012///
1013/// ## On the impl block:
1014///
1015/// * `crate = "path"` - Specifies the crate path to use for zlink types. Defaults to `::zlink`.
1016/// * `interface = "..."` - Sets the default interface name for all methods. Useful for services
1017/// that implement a single interface. Methods can still override this with method-level
1018/// `#[zlink(interface = "...")]`.
1019/// * `types = [Type1, Type2, ...]` - Custom types to include in interface descriptions. These types
1020/// must implement `CustomType` (typically via `#[derive(CustomType)]`). The types are included in
1021/// the IDL for any interface that uses them.
1022/// * `vendor = "..."` - The vendor name for `GetInfo` response. Defaults to empty string.
1023/// * `product = "..."` - The product name for `GetInfo` response. Defaults to empty string.
1024/// * `version = "..."` - The version string for `GetInfo` response. Defaults to empty string.
1025/// * `url = "..."` - The URL for `GetInfo` response. Defaults to empty string.
1026///
1027/// ## On methods:
1028///
1029/// * `#[zlink(interface = "...")]` - Set the interface name for this and subsequent methods. If an
1030/// interface is specified at the impl block level, this overrides it for the current method.
1031/// * `#[zlink(rename = "MethodName")]` - Custom Varlink method name.
1032///
1033/// ## On parameters:
1034///
1035/// * `#[zlink(rename = "paramName")]` - Custom serialized parameter name.
1036/// * `#[zlink(connection)]` - Mark this parameter to receive a mutable reference to the connection.
1037/// This is useful for accessing peer credentials or other connection-specific functionality.
1038/// **Requires an explicit generic socket type parameter** (e.g., `impl<Sock> MyService`).
1039///
1040/// # Generated Code
1041///
1042/// The macro generates an `impl<Sock: Socket> Service<Sock> for YourType` with the `handle` method,
1043/// along with internal helper types for serialization/deserialization.
1044///
1045/// # Error Handling
1046///
1047/// Methods can return `Result<T, E>` with any error type `E` that implements `Serialize` and
1048/// `Debug`. Different methods can use different error types - the macro automatically generates
1049/// internal wrapper types to handle all unique error types.
1050///
1051/// When a method returns `Err(e)`, the macro generates code that wraps it in the appropriate
1052/// combo enum variant and returns `MethodReply::Error(...)`.
1053/// When a method returns `Ok(v)`, it returns `MethodReply::Single(Some(v))`.
1054///
1055/// Methods can also return plain values (not wrapped in Result) - these are always treated as
1056/// successful responses.
1057///
1058/// # Custom Socket Bounds
1059///
1060/// By default, the generated `Service` impl uses a generic socket parameter with just the `Socket`
1061/// bound. If you need additional bounds (e.g., for peer credential checking), you can provide your
1062/// own generics on the impl block:
1063///
1064/// ```rust
1065/// use zlink::{service, connection::socket::FetchPeerCredentials, introspect};
1066///
1067/// struct MyService;
1068///
1069/// #[derive(Debug, Clone, PartialEq, zlink::ReplyError, introspect::ReplyError)]
1070/// #[zlink(interface = "org.example.service")]
1071/// enum MyError {
1072/// ServiceError,
1073/// }
1074///
1075/// #[service]
1076/// impl<Sock> MyService
1077/// where
1078/// Sock::ReadHalf: FetchPeerCredentials,
1079/// {
1080/// #[zlink(interface = "org.example.service")]
1081/// async fn get_status(&self) -> Result<(), MyError> {
1082/// Ok(())
1083/// }
1084/// }
1085/// ```
1086///
1087/// The first type parameter is used as the socket type for the generated `Service` impl. The
1088/// `Socket` bound is automatically added, so you only need to specify additional bounds.
1089///
1090/// # Connection Parameter
1091///
1092/// Methods can receive a mutable reference to the connection using `#[zlink(connection)]`:
1093///
1094/// ```rust
1095/// use zlink::{service, Connection, connection::socket::FetchPeerCredentials, introspect};
1096///
1097/// struct MyService;
1098///
1099/// #[derive(Debug, Clone, PartialEq, zlink::ReplyError, introspect::ReplyError)]
1100/// #[zlink(interface = "org.example.service")]
1101/// enum MyError {
1102/// ServiceError,
1103/// }
1104///
1105/// #[service]
1106/// impl<Sock> MyService
1107/// where
1108/// Sock::ReadHalf: FetchPeerCredentials,
1109/// {
1110/// #[zlink(interface = "org.example.service")]
1111/// async fn check_credentials(
1112/// &self,
1113/// #[zlink(connection)] conn: &mut Connection<Sock>,
1114/// ) -> Result<(), MyError> {
1115/// let _creds = conn.peer_credentials().await;
1116/// Ok(())
1117/// }
1118/// }
1119/// ```
1120///
1121/// Methods with connection parameters are only callable through the `Service` trait (not directly
1122/// on the type), since they require the socket type to be known.
1123///
1124/// # Example
1125///
1126/// ```rust
1127/// use zlink::{
1128/// introspect::{self, Type},
1129/// service,
1130/// unix::{bind, connect},
1131/// Server,
1132/// };
1133/// use serde::{Deserialize, Serialize};
1134///
1135/// // Response type for balance operations.
1136/// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Type)]
1137/// struct Balance {
1138/// amount: i64,
1139/// }
1140///
1141/// // Error type - must derive zlink::ReplyError for proper serialization.
1142/// #[derive(Debug, Clone, PartialEq, zlink::ReplyError, introspect::ReplyError)]
1143/// #[zlink(interface = "org.example.bank")]
1144/// enum BankError {
1145/// InsufficientFunds { available: i64, requested: i64 },
1146/// InvalidAmount { amount: i64 },
1147/// AccountLocked,
1148/// }
1149///
1150/// struct BankAccount {
1151/// balance: i64,
1152/// locked: bool,
1153/// }
1154///
1155/// impl BankAccount {
1156/// fn new(initial_balance: i64) -> Self {
1157/// Self { balance: initial_balance, locked: false }
1158/// }
1159/// }
1160///
1161/// // Service implementation with error handling.
1162/// #[service]
1163/// impl BankAccount {
1164/// // Method that returns a plain value (not Result) - always succeeds.
1165/// #[zlink(interface = "org.example.bank")]
1166/// async fn get_balance(&self) -> Balance {
1167/// Balance { amount: self.balance }
1168/// }
1169///
1170/// // Method that can fail - returns Result<Balance, BankError>.
1171/// async fn deposit(&mut self, amount: i64) -> Result<Balance, BankError> {
1172/// if self.locked {
1173/// return Err(BankError::AccountLocked);
1174/// }
1175/// if amount <= 0 {
1176/// return Err(BankError::InvalidAmount { amount });
1177/// }
1178/// self.balance += amount;
1179/// Ok(Balance { amount: self.balance })
1180/// }
1181///
1182/// async fn withdraw(&mut self, amount: i64) -> Result<Balance, BankError> {
1183/// if self.locked {
1184/// return Err(BankError::AccountLocked);
1185/// }
1186/// if amount <= 0 {
1187/// return Err(BankError::InvalidAmount { amount });
1188/// }
1189/// if amount > self.balance {
1190/// return Err(BankError::InsufficientFunds {
1191/// available: self.balance,
1192/// requested: amount,
1193/// });
1194/// }
1195/// self.balance -= amount;
1196/// Ok(Balance { amount: self.balance })
1197/// }
1198///
1199/// // Method returning Result<(), E> - void success, can fail.
1200/// async fn lock_account(&mut self) -> Result<(), BankError> {
1201/// if self.locked {
1202/// return Err(BankError::AccountLocked);
1203/// }
1204/// self.locked = true;
1205/// Ok(())
1206/// }
1207/// }
1208///
1209/// // Client-side proxy definition.
1210/// #[zlink::proxy("org.example.bank")]
1211/// trait BankProxy {
1212/// async fn get_balance(&mut self) -> zlink::Result<Result<Balance, BankError>>;
1213/// async fn deposit(&mut self, amount: i64) -> zlink::Result<Result<Balance, BankError>>;
1214/// async fn withdraw(&mut self, amount: i64) -> zlink::Result<Result<Balance, BankError>>;
1215/// async fn lock_account(&mut self) -> zlink::Result<Result<(), BankError>>;
1216/// }
1217///
1218/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
1219/// // Server setup.
1220/// let socket_path = "/tmp/zlink-service-example.sock";
1221/// let _ = std::fs::remove_file(socket_path);
1222/// let listener = bind(socket_path)?;
1223/// let service = BankAccount::new(1000);
1224/// let server = Server::new(listener, service);
1225///
1226/// // Run server and client concurrently.
1227/// tokio::select! {
1228/// res = server.run() => res?,
1229/// res = async {
1230/// let mut conn = connect(socket_path).await?;
1231///
1232/// // Check initial balance.
1233/// let balance = conn.get_balance().await?.unwrap();
1234/// assert_eq!(balance.amount, 1000);
1235///
1236/// // Successful deposit.
1237/// let balance = conn.deposit(500).await?.unwrap();
1238/// assert_eq!(balance.amount, 1500);
1239///
1240/// // Successful withdrawal.
1241/// let balance = conn.withdraw(200).await?.unwrap();
1242/// assert_eq!(balance.amount, 1300);
1243///
1244/// // Error: withdraw more than available.
1245/// let err = conn.withdraw(5000).await?.unwrap_err();
1246/// assert_eq!(err, BankError::InsufficientFunds { available: 1300, requested: 5000 });
1247///
1248/// // Error: invalid amount.
1249/// let err = conn.deposit(-100).await?.unwrap_err();
1250/// assert_eq!(err, BankError::InvalidAmount { amount: -100 });
1251///
1252/// // Lock account and verify subsequent operations fail.
1253/// conn.lock_account().await?.unwrap();
1254/// let err = conn.withdraw(100).await?.unwrap_err();
1255/// assert_eq!(err, BankError::AccountLocked);
1256///
1257/// Ok::<(), Box<dyn std::error::Error>>(())
1258/// } => res?,
1259/// }
1260/// # Ok::<(), Box<dyn std::error::Error>>(())
1261/// # })?;
1262/// # Ok::<(), Box<dyn std::error::Error>>(())
1263/// ```
1264///
1265/// # Introspection Example
1266///
1267/// The service automatically provides introspection via the `org.varlink.service` interface:
1268///
1269/// ```rust
1270/// use zlink::{
1271/// introspect::{self, CustomType, Type},
1272/// service,
1273/// varlink_service::Proxy,
1274/// };
1275/// use serde::{Deserialize, Serialize};
1276///
1277/// // Custom type - must derive CustomType to be included in IDL.
1278/// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
1279/// struct Balance {
1280/// amount: i64,
1281/// }
1282///
1283/// #[derive(Debug, Clone, PartialEq, zlink::ReplyError, introspect::ReplyError)]
1284/// #[zlink(interface = "org.example.bank")]
1285/// enum BankError {
1286/// InsufficientFunds { available: i64 },
1287/// }
1288///
1289/// struct BankService;
1290///
1291/// // Include custom types in the service for IDL generation.
1292/// #[service(
1293/// types = [Balance],
1294/// vendor = "Example Corp",
1295/// product = "Bank Service",
1296/// version = "1.0.0",
1297/// url = "https://example.com/bank"
1298/// )]
1299/// impl BankService {
1300/// #[zlink(interface = "org.example.bank")]
1301/// async fn get_balance(&self) -> Result<Balance, BankError> {
1302/// Ok(Balance { amount: 1000 })
1303/// }
1304/// }
1305///
1306/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
1307/// # use zlink::test_utils::mock_socket::MockSocket;
1308/// // GetInfo returns metadata and list of interfaces.
1309/// # let responses = [
1310/// # r#"{"parameters":{"vendor":"Example Corp","product":"Bank Service","version":"1.0.0","url":"https://example.com/bank","interfaces":["org.example.bank","org.varlink.service"]}}"#,
1311/// # ];
1312/// # let socket = MockSocket::with_responses(&responses);
1313/// # let mut conn = zlink::Connection::new(socket);
1314/// let info = conn.get_info().await?.unwrap();
1315/// assert_eq!(info.vendor, "Example Corp");
1316/// let interfaces: Vec<&str> = info.interfaces.iter().map(|s| s.as_ref()).collect();
1317/// assert_eq!(interfaces.as_slice(), ["org.example.bank", "org.varlink.service"]);
1318/// # Ok::<(), Box<dyn std::error::Error>>(())
1319/// # }).unwrap();
1320///
1321/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
1322/// # use zlink::test_utils::mock_socket::MockSocket;
1323/// // GetInterfaceDescription returns the IDL, which can be parsed to verify methods and types.
1324/// # let responses = [
1325/// # r#"{"parameters":{"description":"interface org.example.bank\n\ntype Balance (amount: int)\n\nmethod GetBalance() -> (amount: int)\n\nerror InsufficientFunds (available: int)"}}"#,
1326/// # ];
1327/// # let socket = MockSocket::with_responses(&responses);
1328/// # let mut conn = zlink::Connection::new(socket);
1329/// let desc = conn.get_interface_description("org.example.bank").await?.unwrap();
1330/// let interface = desc.parse()?;
1331/// assert_eq!(interface.name(), "org.example.bank");
1332///
1333/// // Verify methods are present.
1334/// let method_names: Vec<_> = interface.methods().map(|m| m.name()).collect();
1335/// assert_eq!(method_names.as_slice(), ["GetBalance"]);
1336///
1337/// // Verify custom types are included.
1338/// let type_names: Vec<_> = interface.custom_types().map(|t| t.name()).collect();
1339/// assert_eq!(type_names.as_slice(), ["Balance"]);
1340///
1341/// // Verify errors are present.
1342/// let error_names: Vec<_> = interface.errors().map(|e| e.name()).collect();
1343/// assert_eq!(error_names.as_slice(), ["InsufficientFunds"]);
1344/// # Ok::<(), Box<dyn std::error::Error>>(())
1345/// # }).unwrap();
1346/// ```
1347///
1348/// # Method Name Conversion
1349///
1350/// By default, method names are converted from snake_case to PascalCase for the Varlink call.
1351/// For example, `get_balance` becomes `GetBalance`. Use `#[zlink(rename = "...")]` to override
1352/// this.
1353///
1354/// # Full Method Path
1355///
1356/// The full Varlink method path is constructed as `{interface}.{MethodName}`. For example,
1357/// if the interface is `org.example.bank` and the method is `GetBalance`, the full path
1358/// will be `org.example.bank.GetBalance`.
1359///
1360/// # Interface Propagation
1361///
1362/// The interface for methods is determined in this order:
1363/// 1. If the method has `#[zlink(interface = "...")]`, that interface is used.
1364/// 2. Otherwise, the interface is inherited from the previous method or from the macro-level
1365/// `interface = "..."` attribute.
1366///
1367/// For services implementing a single interface, specifying `interface = "..."` at the macro level
1368/// is the simplest approach - all methods automatically use that interface without needing
1369/// individual attributes.
1370#[cfg(feature = "service")]
1371#[proc_macro_attribute]
1372pub fn service(
1373 attr: proc_macro::TokenStream,
1374 input: proc_macro::TokenStream,
1375) -> proc_macro::TokenStream {
1376 service::service(attr.into(), input.into()).into()
1377}