Skip to content

Commit e4f3d72

Browse files
committed
add examples to rustdocs on functions instead of README and add license and credits to readme
1 parent 9535ef8 commit e4f3d72

File tree

5 files changed

+80
-57
lines changed

5 files changed

+80
-57
lines changed

README.md

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1Password IPC Client
1+
# 1Password IPC Client
22

33
## Overview
44

@@ -68,62 +68,6 @@ let response2 = send_with_pipe(&mut pipe, request2).unwrap();
6868

6969
Note: Windows and Linux have a limitation in multi-threaded environments with message integrity. Users must ensure they send and receive one at a time.
7070

71-
## Public API
72-
73-
### `send_to`
74-
75-
```rust
76-
pub fn send_to(endpoint_name: &str, message: Vec<u8>) -> Result<Vec<u8>, ErrorCode>
77-
```
78-
79-
Sends a message to the IPC server at the given endpoint and returns the response. This is a synchronous, blocking call that creates a fresh connection per invocation.
80-
81-
| Parameter | Type | Description |
82-
| :-------- | :--- | :---------- |
83-
| endpoint_name | `&str` | The endpoint to connect to. Interpreted as a Mach port name on macOS, an abstract Unix socket name on Linux, or a named pipe path on Windows. |
84-
| message | `Vec<u8>` | The raw request bytes to send |
85-
86-
**Returns:** `Ok(Vec<u8>)` containing the response bytes, or `Err(ErrorCode)` on failure.
87-
88-
### `send_with_client` (macOS only)
89-
90-
```rust
91-
pub fn send_with_client(client: &mut Client, request: Vec<u8>) -> Result<Vec<u8>, ErrorCode>
92-
```
93-
94-
Sends a message over an existing Mach port client. Allows reusing the same connection across multiple calls.
95-
96-
### `send_with_stream` (Linux only)
97-
98-
```rust
99-
pub fn send_with_stream(stream: &mut UnixStream, message: Vec<u8>) -> Result<Vec<u8>, ErrorCode>
100-
```
101-
102-
Sends a message over an existing Unix stream. Allows reusing the same connection across multiple calls.
103-
104-
### `send_with_pipe` (Windows only)
105-
106-
```rust
107-
pub fn send_with_pipe(pipe: &mut File, message: Vec<u8>) -> Result<Vec<u8>, ErrorCode>
108-
```
109-
110-
Sends a message over an existing named pipe. Allows reusing the same connection across multiple calls.
111-
112-
## Error Handling
113-
114-
All platforms share a single `ErrorCode` enum, re-exported from the crate root as `onepassword_ipc_client::ErrorCode`.
115-
116-
| Value | Description |
117-
| :---- | :---------- |
118-
| `InvalidArguments` | Invalid arguments were provided (e.g. bad endpoint name). |
119-
| `FailedToConnect` | Failed to connect to the IPC endpoint. |
120-
| `FailedToSend` | Failed to send the message. |
121-
| `FailedToReceive` | Failed to receive a response from the server. |
122-
| `FailedToEncode` | Failed to encode the message into the wire format. |
123-
| `FailedToDecode` | Failed to decode the response from the wire format. |
124-
| `ServerClosedConnection` | The server closed the connection unexpectedly. |
125-
| `Internal` | An internal error occurred. |
126-
12771
## Wire Protocol
12872

12973
### Framing (Linux / Windows)
@@ -173,3 +117,19 @@ The maximum chunk size is 500,000 bytes (1-byte header + up to 499,999 bytes of
173117

174118
On macOS, if the server's response spans multiple chunks, the client sends a dummy (empty) chunk after each intermediate response chunk to request the next one. On Linux and Windows, all response chunks are sent by the server without further prompting.
175119

120+
## Credits
121+
122+
Made with ❤️ and ☕ by the [1Password](https://1password.com/) team.
123+
124+
### License
125+
126+
<sup>
127+
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
128+
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
129+
</sup>
130+
131+
<sub>
132+
Unless you explicitly state otherwise, any contribution intentionally submitted
133+
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
134+
be dual licensed as above, without any additional terms or conditions.
135+
</sub>

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ pub enum ErrorCode {
3636
/// Sends a message to the IPC server at the given endpoint and returns the response.
3737
///
3838
/// This is a synchronous, blocking call that creates a fresh connection per invocation.
39+
///
40+
/// The `endpoint_name` is interpreted as a Mach port name on macOS, an abstract Unix socket
41+
/// name on Linux, or a named pipe path on Windows.
42+
///
43+
/// Returns the response bytes on success, or an [`ErrorCode`] on failure.
44+
///
45+
/// # Examples
46+
///
47+
/// ```no_run
48+
/// use onepassword_ipc_client::{send_to, ErrorCode};
49+
///
50+
/// let request = b"my request payload".to_vec();
51+
/// match send_to("your_endpoint_name", request) {
52+
/// Ok(response) => println!("Got {} bytes back", response.len()),
53+
/// Err(err) => eprintln!("IPC failed: {:?}", err),
54+
/// }
55+
/// ```
3956
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
4057
pub fn send_to(endpoint_name: &str, message: Vec<u8>) -> Result<Vec<u8>, ErrorCode> {
4158
platform::send_to(endpoint_name, message)

src/platform/linux.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,23 @@ pub fn send_to(endpoint_name: &str, message: Vec<u8>) -> Result<Vec<u8>, ErrorCo
1818
/// Sends a message over an existing stream and returns the response.
1919
///
2020
/// This allows reusing the same connection across multiple calls.
21+
///
2122
/// NOTE: This requires to send and receive messages one at a time as in multi-threaded
2223
/// contexts, this will ruin the message integrity as chunks can potentially be out of sync.
24+
///
25+
/// # Examples
26+
///
27+
/// ```no_run
28+
/// use std::os::linux::net::SocketAddrExt;
29+
/// use std::os::unix::net::{SocketAddr, UnixStream};
30+
/// use onepassword_ipc_client::send_with_stream;
31+
///
32+
/// let addr = SocketAddr::from_abstract_name("your_endpoint_name").unwrap();
33+
/// let mut stream = UnixStream::connect_addr(&addr).unwrap();
34+
///
35+
/// let response1 = send_with_stream(&mut stream, b"request one".to_vec()).unwrap();
36+
/// let response2 = send_with_stream(&mut stream, b"request two".to_vec()).unwrap();
37+
/// ```
2338
pub fn send_with_stream(stream: &mut UnixStream, message: Vec<u8>) -> Result<Vec<u8>, ErrorCode> {
2439
send_and_receive(stream, message)
2540
}

src/platform/macos.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ pub fn send_to(endpoint_name: &str, request: Vec<u8>) -> Result<Vec<u8>, ErrorCo
1515
send_with_client(&mut client, request)
1616
}
1717

18+
/// Sends a message over an existing Mach port client and returns the response.
19+
///
20+
/// This allows reusing the same connection across multiple calls.
21+
///
22+
/// # Examples
23+
///
24+
/// ```no_run
25+
/// use onepassword_ipc_client::send_with_client;
26+
/// use mach_listener::Client;
27+
///
28+
/// let mut client = Client::connect("your_endpoint_name").unwrap();
29+
/// let response = send_with_client(&mut client, b"hello".to_vec()).unwrap();
30+
/// println!("Got {} bytes back", response.len());
31+
/// ```
1832
pub fn send_with_client(client: &mut Client, request: Vec<u8>) -> Result<Vec<u8>, ErrorCode> {
1933
let chunks = build_chunks(&request);
2034
let mut iter = chunks.into_iter().peekable();

src/platform/windows.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,25 @@ pub fn send_to(endpoint_name: &str, message: Vec<u8>) -> Result<Vec<u8>, ErrorCo
1818
/// Sends a message over an existing named pipe and returns the response.
1919
///
2020
/// This allows reusing the same connection across multiple calls.
21+
///
2122
/// NOTE: This requires to send and receive messages one at a time as in multi-threaded
2223
/// contexts, this will ruin the message integrity as chunks can potentially be out of sync.
24+
///
25+
/// # Examples
26+
///
27+
/// ```no_run
28+
/// use std::fs::OpenOptions;
29+
/// use onepassword_ipc_client::send_with_pipe;
30+
///
31+
/// let mut pipe = OpenOptions::new()
32+
/// .read(true)
33+
/// .write(true)
34+
/// .open(r"\\.\pipe\your_endpoint_name")
35+
/// .unwrap();
36+
///
37+
/// let response1 = send_with_pipe(&mut pipe, b"request one".to_vec()).unwrap();
38+
/// let response2 = send_with_pipe(&mut pipe, b"request two".to_vec()).unwrap();
39+
/// ```
2340
pub fn send_with_pipe(pipe: &mut File, message: Vec<u8>) -> Result<Vec<u8>, ErrorCode> {
2441
send_and_receive(pipe, message)
2542
}

0 commit comments

Comments
 (0)