updated readme with instructions on how to use as a crate

This commit is contained in:
robojerk 2025-09-13 10:12:35 -07:00
parent 7aaefb9957
commit 534c0e87a8

104
README.md
View file

@ -82,6 +82,110 @@ Add to your `Cargo.toml`:
apt-wrapper = "0.1.0" 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<dyn std::error::Error>> {
// 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<T, anyhow::Error>`:
```rust
use anyhow::Result;
fn example() -> Result<()> {
let mut tx = AptTransaction::new()?; // Returns Result<AptTransaction, anyhow::Error>
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 ## Usage
### Basic Transaction ### Basic Transaction