cull-gmail Library Documentation
Library documentation of cull-gmail.
The cull-gmail library provides a Rust API for managing Gmail messages through the Gmail API. It enables programmatic email culling operations including authentication, message querying, filtering, and batch operations (trash/delete).
Installation
Add the library to your Cargo.toml:
[]
= "0.0.10"
= { = "1.0", = ["macros", "rt-multi-thread"] }
Quick Start
Here's a minimal example to get started:
use ;
async
Core Types
GmailClient
The main client for interacting with Gmail API:
use ;
// Create client with configuration
let mut client = new_with_config.await?;
// Query messages with Gmail search syntax
client.set_query;
client.add_labels?;
client.set_max_results;
// Get messages (0 = all pages, 1 = first page only)
client.get_messages.await?;
// Access message data
let messages = client.messages;
let message_ids = client.message_ids;
ClientConfig
Handles authentication and configuration:
use ClientConfig;
// From credential file
let config = builder
.with_credential_file
.with_config_path
.build;
// From individual OAuth2 parameters
let config = builder
.with_client_id
.with_client_secret
.with_auth_uri
.with_token_uri
.add_redirect_uri
.build;
Rules and Retention Policies
Define automated message lifecycle rules:
use ;
// Create a rule set
let mut rules = new;
// Add retention rules
rules.add_rule;
rules.add_rule;
// Save rules to file
rules.save?;
// Load existing rules
let loaded_rules = load?;
Message Operations
Batch operations on messages:
use ;
// Set up rule and dry-run mode
client.set_execute; // Dry run - no actual changes
let rule = rules.get_rule.unwrap;
client.set_rule;
// Find messages matching rule for a label
client.find_rule_and_messages_for_label.await?;
// Check what action would be performed
if let Some = client.action
// Execute for real
client.set_execute;
match client.action
Configuration
OAuth2 Setup
- Create OAuth2 credentials in Google Cloud Console
- Download the credential JSON file
- Configure the client:
let config = builder
.with_credential_file
.build;
Configuration File
The library supports TOML configuration files (default: ~/.cull-gmail/cull-gmail.toml):
= "credential.json"
= "~/.cull-gmail"
= "rules.toml"
= false
# Alternative: direct OAuth2 parameters
# client_id = "your-client-id"
# client_secret = "your-client-secret"
# token_uri = "https://oauth2.googleapis.com/token"
# auth_uri = "https://accounts.google.com/o/oauth2/auth"
Environment Variables
Override configuration with environment variables:
Error Handling
The library uses a comprehensive error type:
use ;
match client.get_messages.await
Common error types:
NoLabelsFound: Mailbox has no labelsLabelNotFoundInMailbox(String): Specific label not foundRuleNotFound(usize): Rule ID doesn't existGoogleGmail1(Box<google_gmail1::Error>): Gmail API errorsStdIO(std::io::Error): File I/O errorsConfig(config::ConfigError): Configuration errors
Async Runtime
The library requires an async runtime (Tokio recommended):
[]
= { = "1.0", = ["macros", "rt-multi-thread"] }
async
Gmail Query Syntax
The library supports Gmail's search syntax for message queries:
// Date-based queries
client.set_query; // Older than 1 year
client.set_query; // Newer than 30 days
client.set_query; // After specific date
// Label-based queries
client.set_query; // Has promotions label
client.set_query; // Does NOT have important label
// Content queries
client.set_query; // Subject contains "newsletter"
client.set_query; // From specific sender
// Combined queries
client.set_query;
Performance & Limits
Pagination
- Default page size: 200 messages
- Use
client.set_max_results(n)to adjust - Use
client.get_messages(0)to get all pages - Use
client.get_messages(n)to limit to n pages
Rate Limits
- The library uses the official
google-gmail1crate - Built-in retry logic for transient errors
- Respects Gmail API quotas and limits
Batch Operations
- Batch delete/trash operations are more efficient than individual calls
- Operations are atomic - either all succeed or all fail
Logging
The library uses the log crate for logging:
use env_logger;
// Initialize logging
init;
# Set log level via environment variable
# RUST_LOG=cull_gmail=debug cargo run
Log levels:
error: Critical errorswarn: Warnings (e.g., missing labels, dry-run mode)info: General information (e.g., message subjects, action results)debug: Detailed operation infotrace: Very detailed debugging info
Security Considerations
OAuth2 Token Storage
- Tokens are stored in
~/.cull-gmail/gmail1by default - Tokens are automatically refreshed when expired
- Revoke access in Google Account settings
Required Scopes
The library requires the https://mail.google.com/ scope for full Gmail access.
Credential File Security
- Store credential files securely (not in version control)
- Use restrictive file permissions (600)
- Consider using environment variables in production
Troubleshooting
Authentication Issues
- Verify credential file path and format
- Check OAuth2 client is configured for "Desktop Application"
- Ensure redirect URI matches configuration
- Clear token cache:
rm -rf ~/.cull-gmail/gmail1
No Messages Found
- Verify label names exist:
client.show_label() - Test query syntax in Gmail web interface
- Check for typos in label names or query strings
Rate Limiting
- Reduce page size:
client.set_max_results(100) - Add delays between operations
- Check Gmail API quotas
See Also
- CLI Documentation - Complete guide to the command-line interface
- API Documentation - Generated API reference
- Repository - Source code and issue tracking