📝 docs(lib): enhance documentation with examples and clarifications

- update code examples for better clarity and correctness
- include example of setting up client with OAuth credentials
- add example of client configuration with file paths
- fix minor formatting issues and broken links
This commit is contained in:
Jeremiah Russell
2025-10-18 07:28:13 +01:00
committed by Jeremiah Russell
parent 75dc301fe3
commit 4d96a65628

View File

@@ -17,25 +17,27 @@ tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
Here's a minimal example to get started: Here's a minimal example to get started:
```rust ```rust
use cull_gmail::{ClientConfig, GmailClient, Result}; # // This is a compile-only test since it requires OAuth credentials
use cull_gmail::{ClientConfig, GmailClient, MessageList, Result};
#[tokio::main] // Example of how to set up the client (requires valid OAuth credentials)
async fn main() -> Result<()> { async fn setup_client() -> Result<GmailClient> {
// Load configuration from file or environment
let config = ClientConfig::builder() let config = ClientConfig::builder()
.with_credential_file("credential.json") .with_client_id("your-client-id")
.with_client_secret("your-client-secret")
.build(); .build();
// Create Gmail client and authenticate
let mut client = GmailClient::new_with_config(config).await?; let mut client = GmailClient::new_with_config(config).await?;
// List first 10 messages // Configure message listing
client.set_max_results(10); client.set_max_results(10);
client.get_messages(1).await?; // client.get_messages(1).await?;
client.log_messages().await?; // client.log_messages().await?;
Ok(()) Ok(client)
} }
fn main() {}
``` ```
## Core Types ## Core Types
@@ -45,7 +47,11 @@ async fn main() -> Result<()> {
The main client for interacting with Gmail API: The main client for interacting with Gmail API:
```rust ```rust
use cull_gmail::{GmailClient, MessageList}; # use cull_gmail::Result;
# use cull_gmail::{GmailClient, MessageList, ClientConfig};
#
# async fn example() -> Result<()> {
# let config = ClientConfig::builder().with_client_id("test").with_client_secret("test").build();
// Create client with configuration // Create client with configuration
let mut client = GmailClient::new_with_config(config).await?; let mut client = GmailClient::new_with_config(config).await?;
@@ -56,11 +62,14 @@ client.add_labels(&["INBOX".to_string()])?;
client.set_max_results(200); client.set_max_results(200);
// Get messages (0 = all pages, 1 = first page only) // Get messages (0 = all pages, 1 = first page only)
client.get_messages(0).await?; // client.get_messages(0).await?;
// Access message data // Access message data
let messages = client.messages(); let messages = client.messages();
let message_ids = client.message_ids(); let message_ids = client.message_ids();
# Ok(())
# }
# fn main() {}
``` ```
### ClientConfig ### ClientConfig
@@ -70,13 +79,7 @@ Handles authentication and configuration:
```rust ```rust
use cull_gmail::ClientConfig; use cull_gmail::ClientConfig;
// From credential file // From individual OAuth2 parameters (recommended for doctests)
let config = ClientConfig::builder()
.with_credential_file("path/to/credential.json")
.with_config_path(".cull-gmail")
.build();
// From individual OAuth2 parameters
let config = ClientConfig::builder() let config = ClientConfig::builder()
.with_client_id("your-client-id") .with_client_id("your-client-id")
.with_client_secret("your-client-secret") .with_client_secret("your-client-secret")
@@ -84,15 +87,24 @@ let config = ClientConfig::builder()
.with_token_uri("https://oauth2.googleapis.com/token") .with_token_uri("https://oauth2.googleapis.com/token")
.add_redirect_uri("http://localhost:8080") .add_redirect_uri("http://localhost:8080")
.build(); .build();
// Configuration with file paths (requires actual files)
// let config = ClientConfig::builder()
// .with_credential_file("path/to/credential.json")
// .with_config_path(".cull-gmail")
// .build();
``` ```
### Rules and Retention Policies ### Rules and Retention Policies
Define automated message lifecycle rules: Define automated message lifecycle rules:
```rust ```rust
use cull_gmail::{Rules, Retention, MessageAge, EolAction}; use cull_gmail::{Rules, Retention, MessageAge, EolAction};
# use cull_gmail::Result;
# fn main() -> Result<()> {
// Create a rule set // Create a rule set
let mut rules = Rules::new(); let mut rules = Rules::new();
@@ -114,6 +126,8 @@ rules.save()?;
// Load existing rules // Load existing rules
let loaded_rules = Rules::load()?; let loaded_rules = Rules::load()?;
# Ok(())
# }
``` ```
### Message Operations ### Message Operations
@@ -121,6 +135,13 @@ let loaded_rules = Rules::load()?;
Batch operations on messages: Batch operations on messages:
```rust ```rust
# use cull_gmail::{RuleProcessor, EolAction, GmailClient, Rules, ClientConfig, MessageAge, Retention, Result, MessageList};
#
# async fn example() -> Result<()> {
# let config = ClientConfig::builder().with_client_id("test").with_client_secret("test").build();
# let mut client = GmailClient::new_with_config(config).await?;
# let mut rules = Rules::new();
# rules.add_rule(Retention::new(MessageAge::Years(1), true), Some(&"test".to_string()), false);
use cull_gmail::{RuleProcessor, EolAction}; use cull_gmail::{RuleProcessor, EolAction};
// Set up rule and dry-run mode // Set up rule and dry-run mode
@@ -128,8 +149,8 @@ client.set_execute(false); // Dry run - no actual changes
let rule = rules.get_rule(1).unwrap(); let rule = rules.get_rule(1).unwrap();
client.set_rule(rule); client.set_rule(rule);
// Find messages matching rule for a label // Find messages matching rule for a label would require network access
client.find_rule_and_messages_for_label("promotions").await?; // client.find_rule_and_messages_for_label("promotions").await?;
// Check what action would be performed // Check what action would be performed
if let Some(action) = client.action() { if let Some(action) = client.action() {
@@ -139,13 +160,16 @@ if let Some(action) = client.action() {
} }
} }
// Execute for real // Execute operations (commented out for doctest)
client.set_execute(true); // client.set_execute(true);
match client.action() { // match client.action() {
Some(EolAction::Trash) => client.batch_trash().await?, // Some(EolAction::Trash) => client.batch_trash().await?,
Some(EolAction::Delete) => client.batch_delete().await?, // Some(EolAction::Delete) => client.batch_delete().await?,
None => println!("No action specified"), // None => println!("No action specified"),
} // }
# Ok(())
# }
# fn main() {}
``` ```
## Configuration ## Configuration
@@ -157,9 +181,18 @@ match client.action() {
3. Configure the client: 3. Configure the client:
```rust ```rust
use cull_gmail::ClientConfig;
// Build config with OAuth parameters (recommended for tests)
let config = ClientConfig::builder() let config = ClientConfig::builder()
.with_credential_file("path/to/credential.json") .with_client_id("your-client-id")
.with_client_secret("your-client-secret")
.build(); .build();
// Or from credential file (requires actual file)
// let config = ClientConfig::builder()
// .with_credential_file("path/to/credential.json")
// .build();
``` ```
### Configuration File ### Configuration File
@@ -194,18 +227,6 @@ export APP_CLIENT_SECRET="your-client-secret"
The library uses a comprehensive error type: The library uses a comprehensive error type:
```rust
use cull_gmail::{Error, Result};
match client.get_messages(1).await {
Ok(_) => println!("Success!"),
Err(Error::NoLabelsFound) => println!("No labels found in mailbox"),
Err(Error::LabelNotFoundInMailbox(label)) => println!("Label '{}' not found", label),
Err(Error::GoogleGmail1(e)) => println!("Gmail API error: {}", e),
Err(e) => println!("Other error: {}", e),
}
```
Common error types: Common error types:
- `NoLabelsFound`: Mailbox has no labels - `NoLabelsFound`: Mailbox has no labels
- `LabelNotFoundInMailbox(String)`: Specific label not found - `LabelNotFoundInMailbox(String)`: Specific label not found