| Crates.io | trop |
| lib.rs | trop |
| version | 0.1.0 |
| created_at | 2025-10-21 03:32:20.443124+00 |
| updated_at | 2025-10-21 03:32:20.443124+00 |
| description | Library for managing ephemeral port reservations |
| homepage | |
| repository | https://github.com/prb/trop |
| max_upload_size | |
| id | 1893239 |
| size | 1,288,677 |
This is the core library crate for trop, a port reservation management tool. It provides the fundamental types and logic for managing ephemeral port allocations in a directory-aware, idempotent manner.
The trop library provides types for:
Port and PortRange types with compile-time safetyReservation and ReservationKey for tracking port allocationsThis library is designed to be used both by the trop-cli binary and potentially by other tools that need port reservation functionality.
PortA validated network port number (1-65535). Port 0 is explicitly invalid.
use trop::Port;
// Valid port
let port = Port::try_from(8080)?;
assert_eq!(port.value(), 8080);
assert!(!port.is_privileged()); // < 1024 is privileged
// Invalid port (0)
assert!(Port::try_from(0).is_err());
PortRangeAn inclusive range of valid ports with iteration support.
use trop::{Port, PortRange};
let min = Port::try_from(5000)?;
let max = Port::try_from(5010)?;
let range = PortRange::new(min, max)?;
assert_eq!(range.len(), 11);
assert!(range.contains(Port::try_from(5005)?));
// Iterate over ports in range
for port in range {
println!("Port: {}", port);
}
ReservationKeyA unique identifier for a port reservation, combining a filesystem path with an optional tag.
use std::path::PathBuf;
use trop::ReservationKey;
// Untagged reservation
let key = ReservationKey::new(PathBuf::from("/path/to/project"), None)?;
println!("{}", key); // "/path/to/project"
// Tagged reservation (for multiple ports per directory)
let key = ReservationKey::new(
PathBuf::from("/path/to/project"),
Some("web".to_string())
)?;
println!("{}", key); // "/path/to/project:web"
ReservationComplete reservation metadata including port, timestamps, and optional project/task information.
use std::path::PathBuf;
use trop::{Port, Reservation, ReservationKey};
let key = ReservationKey::new(PathBuf::from("/project"), None)?;
let port = Port::try_from(8080)?;
let reservation = Reservation::builder(key, port)
.project(Some("my-app".to_string()))
.task(Some("feature-branch".to_string()))
.sticky(false)
.build()?;
println!("Reserved port: {}", reservation.port());
println!("Project: {:?}", reservation.project());
The library uses a custom Error type with variants for different failure modes:
InvalidPort: Port number validation failuresInvalidPath: Path-related errorsDatabase: SQLite database errors (Phase 2+)Configuration: Config file parsing errors (Phase 2+)Validation: Input validation failuresAll fallible operations return Result<T, Error> or use the type alias trop::Result<T>.
Phase 1 Complete: Core types and error handling implemented with comprehensive unit tests.
Coming in Phase 2: Database layer, configuration parsing, and port allocation algorithms.
use std::path::PathBuf;
use trop::{Port, PortRange, Reservation, ReservationKey};
fn main() -> trop::Result<()> {
// Create a port
let port = Port::try_from(8080)?;
println!("Port: {} (privileged: {})", port, port.is_privileged());
// Create a port range
let min = Port::try_from(5000)?;
let max = Port::try_from(5100)?;
let range = PortRange::new(min, max)?;
println!("Range contains {} ports", range.len());
// Create a reservation key
let key = ReservationKey::new(
PathBuf::from("/my/project"),
Some("api".to_string())
)?;
// Build a reservation
let reservation = Reservation::builder(key, port)
.project(Some("my-app".to_string()))
.build()?;
println!("Created reservation for port {}", reservation.port());
Ok(())
}
Generate and view complete API documentation:
cargo doc --open
The documentation includes:
Run the library's unit tests:
cargo test --lib
Run integration tests (requires full workspace):
cd ..
cargo test --all
If you're looking to use the trop command-line tool, see the trop-cli crate instead. This library is primarily for programmatic use or for understanding the internals.
The library follows these principles:
Key dependencies:
serde: Serialization supportthiserror: Error type derivationchrono: Timestamp handlingrusqlite: SQLite database (Phase 2+)log: Logging facadeMIT
This is part of the larger trop project. See the root README for contribution guidelines and project status.