diff --git a/README.md b/README.md index 21325d3..ec343aa 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,110 @@ Add to your `Cargo.toml`: apt-wrapper = "0.1.0" ``` +## Using as a Crate + +### Adding to Your Project + +1. **Add the dependency** to your `Cargo.toml`: + +```toml +[dependencies] +apt-wrapper = "0.1.0" +``` + +2. **Import the crate** in your Rust code: + +```rust +use apt_wrapper::{AptTransaction, init, search_packages, is_package_installed}; +``` + +3. **Initialize the library** before use: + +```rust +fn main() -> Result<(), Box> { + // Always call init() first + init()?; + + // Now you can use the library + let mut tx = AptTransaction::new()?; + // ... rest of your code +} +``` + +### Crate Features + +The crate provides these main components: + +- **`AptTransaction`**: Main transaction interface for package operations +- **`init()`**: Initialize the library (required before use) +- **`search_packages()`**: Search for available packages +- **`is_package_installed()`**: Check if a package is installed +- **`get_package_info()`**: Get detailed package information + +### Error Handling + +The crate uses `anyhow::Result` for error handling. All functions return `Result`: + +```rust +use anyhow::Result; + +fn example() -> Result<()> { + let mut tx = AptTransaction::new()?; // Returns Result + tx.add_package("vim")?; // Returns Result<(), anyhow::Error> + tx.commit()?; // Returns Result<(), anyhow::Error> + Ok(()) +} +``` + +### Complete Example + +Here's a complete example of using the crate: + +```rust +use apt_wrapper::{AptTransaction, init, search_packages, is_package_installed}; +use anyhow::Result; + +fn main() -> Result<()> { + // Initialize the library + init()?; + + // Search for packages + let editors = search_packages("editor")?; + println!("Available editors: {:?}", editors); + + // Check if vim is installed + if !is_package_installed("vim")? { + println!("vim is not installed, installing..."); + + // Create transaction + let mut tx = AptTransaction::new()?; + tx.add_package("vim")?; + tx.add_package("git")?; + + // Resolve and commit + tx.resolve()?; + tx.commit()?; + + println!("Packages installed successfully!"); + } else { + println!("vim is already installed"); + } + + Ok(()) +} +``` + +### Building Your Project + +Once you've added the dependency, build your project normally: + +```bash +cargo build +cargo run +``` + +The crate will automatically handle APT integration and provide the DNF-like interface for your application. + ## Usage ### Basic Transaction