Skip to content

refactor!: lib/bin split, absolute path resolution, and custom path support#89

Open
fractuscontext wants to merge 15 commits intocafkafk:mainfrom
fractuscontext:main
Open

refactor!: lib/bin split, absolute path resolution, and custom path support#89
fractuscontext wants to merge 15 commits intocafkafk:mainfrom
fractuscontext:main

Conversation

@fractuscontext
Copy link
Copy Markdown

@fractuscontext fractuscontext commented Jan 4, 2026

Changelog Entry

This PR refactors the project structure into a library/binary split, resolves runtime panics related to CLI argument parsing, and overhauls path resolution to ensure reliability across environments (Nix, Cargo, System). It also introduces custom path support and modernizes the test suite.

Added

  • Support for optional positional [PATH] argument to load custom fortune files or directories.
  • FORTUNE_OFF_DIR support in the Nix wrapper script.
  • Pre-execution validation to verify fortune path existence before running.
  • Statistical unit tests for weighted file selection in random.rs.
  • Comprehensive Pull Request template (.github/pull_request_template.md).

Changed

  • Refactored src/main.rs into a library (src/lib.rs) and a thin binary wrapper.
  • Updated search_fortunes to return all matching quotes in a file rather than just the first.
  • get_quote now falls back to a random quote if no "short" fortunes are found, preventing panics.
  • Updated Nix flake to use --set instead of --prefix for environment variables to match Rust PathBuf logic.
  • Modernized all docstrings to use CommonMark with reproducible tempfile examples.
  • Build instructions for README.md

Fixed

  • Runtime panic in clap v4 by setting explicit ArgAction::SetTrue for boolean flags.
  • Logic for --short flag to correctly accumulate counts (e.g., -ss).
  • "No such file or directory" (os error 2) by canonicalizing paths and resolving defaults via CARGO_MANIFEST_DIR.
  • Filtering of empty strings caused by trailing % delimiters in fortune files.

Breaking Changes

  • BREAKING CHANGE: The internal API signatures for search_fortunes and get_quote now require a PathBuf and no longer perform internal environment variable lookups.
  • BREAKING CHANGE: Default path resolution now prioritizes absolute paths and resolves relative to the compile-time manifest directory.

Additional Information


Verification & Testing

I have performed the following tests to verify my changes:

Automated Checks

  • Nix Build: Ran nix build and verified the ./result symlink was created successfully.
  • Unit Tests: Ran cargo test (or nix build .#test) and all tests passed.
  • Linting: Ran cargo clippy and ensured no warnings were introduced.
  • Formatting: Ran cargo fmt to ensure code style consistency.
  • License Compliance: Ran reuse lint to verify copyright and license headers.

Manual Verification

  • Binary Run: Ran the binary (e.g., ./result/bin/fortune-kind) and verified basic functionality.
  • Cross-Platform: Verified (if applicable) that paths/env vars work on the target system.

Build Platform

  • x86_64-linux
  • aarch64-linux
  • x86_64-darwin
  • aarch64-darwin

Pre-Submission Checklist

Before submitting, please ensure the following:

  • Target Branch: My pull request targets the main branch.
  • Self-Review: I have performed a self-review of my code.
  • Documentation: I have updated relevant documentation (README, manpages, or EDITORIAL.md) if necessary.
  • Dependencies: I have updated Cargo.toml and flake.nix if I added new dependencies.
  • Commit Messages: My commit messages follow Conventional Commits so git-cliff can auto-generate the changelog.

License Agreement

  • By submitting this pull request, I confirm that my contributions are compliant with the project's license (AGPL-3.0-only) and that I have verified the license headers using reuse.

- Fix CLI argument parsing for short flag (u8 vs bool)
- Update search to return all matches, not just the first
- Fix panic in get_quote when tmp list is empty
- Filter out empty strings from file splits
- Remove unsafe off-by-one indexing
- Refactor `cli.rs` docs to demonstrate `ArgAction::Count` usage.
- Update `fortune.rs` examples (`search_fortunes`, `get_quote`) to use `tempfile` and `env::set_var` instead of relying on system files.
- Ensure examples are self-contained and testable.
- Uncomment integration tests in `fortune.rs`.
- Use `tempfile` to create isolated test environments.
- Inject `FORTUNE_DIR` into child processes to prevent CI failures.
- Verify `u8` overflow logic for the humorous message easter egg.
- Create `src/lib.rs` to export modules (`cli`, `fortune`, `file`, `random`).
- Update `src/main.rs` to consume the crate as a library rather than declaring modules locally.
- Enable better separation of concerns, allowing for future integration tests and documentation generation.
- Added detailed CommonMark docstrings for `random` and `get_random_file_weighted`.
- Implemented a statistical weighted-selection test using `tempfile`.
- Added a test case for single-file path resolution.
- Improved error handling in `get_random_file_weighted` to return `io::Error` instead of crashing on empty selections.
- Split project into library (`src/lib.rs`) and binary (`src/main.rs`).
- Update `fortune` logic to accept `PathBuf` instead of internal env var lookups.
- Implement absolute path resolution in `main` using `fs::canonicalize` and `CARGO_MANIFEST_DIR` defaults.
- Add explicit existence check for fortune paths to prevent runtime panics.
- Fix `clap` v4 panic by setting `ArgAction::SetTrue` for boolean flags.
- Add support for positional path arguments (Closes cafkafk#88)."
- Convert REUSE configuration to `REUSE.toml`.
- Update `CHANGELOG.md` via `git-cliff`.
- Refresh license headers using `reuse` tool.
@fractuscontext fractuscontext marked this pull request as draft January 5, 2026 09:03
- Update installation guide for Nix Flakes and Cargo.
- Add comprehensive build instructions, detailing the Nix wrapper and `FORTUNE_DIR` logic.
- Document new CLI usage patterns, including short flags (`-ss`), unkind mode (`-u`), and custom paths.
- Fix broken badge links and update project metadata.
- Bump package version in `Cargo.toml` and sync `Cargo.lock`.
- Update `CHANGELOG.md` with the latest changes via git-cliff.
@fractuscontext fractuscontext marked this pull request as ready for review January 5, 2026 09:21
- Create .github/pull_request_template.md.
- Include checklists for Nix builds, Rust testing (clippy/fmt/test), and license compliance (reuse).
- Establish guidelines for Conventional Commits and Title Prefixes.
@fractuscontext
Copy link
Copy Markdown
Author

@cafkafk Hi! could you please review the changes? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

provide alternate paths towards fortune ?

1 participant