### Symlink Handling
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Examples demonstrating how to handle symbolic links with path predicates.
```APIDOC
## Symlink Handling
### Description
Examples demonstrating how to handle symbolic links with path predicates.
### Examples
```rust
use std::path::Path;
use predicates::prelude::*
// Treat symlinks as symlinks (default)
let pred = predicate::path::is_symlink();
// Follow symlinks to their targets
let pred = predicate::path::is_file().follow_links(true);
```
```
--------------------------------
### Composition Example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates how to combine comparison predicates using logical AND and OR operations.
```APIDOC
## Composition Example
### Description
Demonstrates how to combine comparison predicates using logical AND and OR operations.
### Example
```rust
use predicates::prelude::*
// Check if value is between 5 and 10
let between = predicate::ge(5).and(predicate::le(10));
assert_eq!(true, between.eval(&7));
assert_eq!(false, between.eval(&11));
assert_eq!(false, between.eval(&4));
```
```
--------------------------------
### Eval Method Example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/core-predicate-trait.md
Demonstrates how to use the `eval` method of a predicate. This example checks if a predicate for equality with 5 correctly evaluates against the values 5 and 3.
```rust
use predicates::prelude::*;
let predicate = predicate::eq(5);
assert!(predicate.eval(&5));
assert!(!predicate.eval(&3));
```
--------------------------------
### Import Predicates Prelude
Source: https://github.com/assert-rs/predicates-rs/blob/master/README.md
Import the necessary prelude for using predicates in your Rust code. This is a common setup step.
```rust
extern crate predicates;
use predicates::prelude::*;
```
--------------------------------
### Basic File Checks
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Examples of basic file system checks using path predicates.
```APIDOC
## Basic File Checks
### Description
Examples of basic file system checks using path predicates.
### Examples
```rust
use std::path::Path;
use predicates::prelude::*
// Check if file exists
let pred = predicate::path::exists();
// Check if path is a directory
let pred = predicate::path::is_dir();
// Check if path is not a file
let pred = predicate::path::is_file().not();
```
```
--------------------------------
### Find Case Method Example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/core-predicate-trait.md
Illustrates the usage of the `find_case` method. This example checks if a 'greater than 5' predicate can find a case that proves it evaluated to true when given the value 10.
```rust
use predicates::prelude::*;
let predicate = predicate::gt(5);
let case = predicate.find_case(true, &10);
assert!(case.is_some());
```
--------------------------------
### starts_with
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Creates a predicate that ensures a string starts with a specified pattern. It checks if the beginning of the string matches the provided prefix.
```APIDOC
## starts_with
### Description
Creates a predicate that ensures a string starts with a pattern.
### Method
`starts_with
(pattern: P)`
### Parameters
#### Path Parameters
- **pattern** (P) - Required - The prefix to match
### Returns
`StartsWithPredicate`
### Example
```rust
use predicates::prelude::*
let predicate = predicate::str::starts_with("Hello");
assert_eq!(true, predicate.eval("Hello World"));
assert_eq!(false, predicate.eval("Goodbye World"));
```
```
--------------------------------
### Minimal Dependencies Configuration
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/00-START-HERE.txt
Example of how to configure Cargo dependencies to use the predicates-rs library with minimal features, reducing build times and binary size.
```toml
[dependencies]
predicates = { version = "3.1", default-features = false }
```
--------------------------------
### StartsWithPredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Checks if a string starts with a pattern.
```APIDOC
## StartsWithPredicate
### Description
Checks if a string starts with a pattern.
### Constructed by
`predicate::str::starts_with(pattern)`
```
--------------------------------
### Complex Boolean Expression Examples
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/boolean-combinators.md
Demonstrates combining multiple boolean combinators to form complex logical expressions. Examples include AND/OR combinations, multiple ANDs, and NOT operations.
```rust
use predicates::prelude::*;
// (x > 5 AND x < 10) OR x == 0
let predicate = predicate::gt(5)
.and(predicate::lt(10))
.or(predicate::eq(0));
// x != 5 AND x != 10
let predicate = predicate::ne(5).and(predicate::ne(10));
// NOT (x > 100)
let predicate = predicate::gt(100).not();
```
--------------------------------
### Successful and Error Case Examples for File Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/errors.md
Demonstrates a successful creation of a file predicate and an error case where the file does not exist. Use `unwrap()` for known existing files or handle the `Result` for potentially inaccessible files.
```rust
use std::path::Path;
use predicates::prelude::*;
// Successful case
let pred = predicate::path::eq_file("Cargo.toml").unwrap();
assert_eq!(true, pred.eval(Path::new("Cargo.toml")));
// Error case - file doesn't exist
let result = predicate::path::eq_file("nonexistent.txt");
assert!(result.is_err());
```
--------------------------------
### Advanced Examples - Floating-Point Arithmetic Precision
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Demonstrates how float predicates handle precision issues in floating-point arithmetic.
```APIDOC
## Advanced Examples - Floating-Point Arithmetic Precision
### Description
Demonstrates how float predicates handle precision issues in floating-point arithmetic.
### Example
```rust
use predicates::prelude::*
fn floating_point_sum() {
// Due to floating-point precision, this doesn't equal 0.45
let a = 0.1 + 0.1 + 0.25;
let b = 0.15 + 0.15 + 0.15;
// Regular equality fails
assert_ne!(a, b);
// But close comparison succeeds
let pred = predicate::float::is_close(a);
assert_eq!(true, pred.eval(&b));
}
```
```
--------------------------------
### Examples of Choosing Collection Membership Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/iterator-predicates.md
Demonstrates how to choose the appropriate iterator predicate based on data type, evaluation frequency, and collection size.
```rust
use predicates::prelude::*;
// For strings with many evaluations
let strings = vec!["apple", "banana", "cherry"];
let predicate = predicate::in_hash(strings);
// For integers with many evaluations
let numbers = vec![1, 5, 10, 15, 20];
let predicate = predicate::in_iter(numbers).sort();
// For one-time checks on small collections
let small = vec![true, false];
let predicate = predicate::in_iter(small);
```
--------------------------------
### Combined File Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Examples of combining multiple path predicates using logical AND and OR.
```APIDOC
## Combined File Predicates
### Description
Examples of combining multiple path predicates using logical AND and OR.
### Examples
```rust
use std::path::Path;
use predicates::prelude::*
// Must be a file that exists
let pred = predicate::path::exists()
.and(predicate::path::is_file());
// Either a file or directory
let pred = predicate::path::is_file()
.or(predicate::path::is_dir());
```
```
--------------------------------
### Combine Path Predicates with AND
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Use the `and` combinator to create a predicate that requires both conditions to be true. This example checks if a path exists and is a file.
```rust
use std::path::Path;
use predicates::prelude::*;
// Must be a file that exists
let pred = predicate::path::exists()
.and(predicate::path::is_file());
```
--------------------------------
### Boxing a Predicate with BoxPredicate::new
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/name-and-boxed.md
Example of creating a `BoxPredicate` using the `new` constructor and evaluating it. This demonstrates dynamic dispatch.
```rust
use predicates::prelude::*;
let boxed = BoxPredicate::new(predicate::always());
assert_eq!(true, boxed.eval(&5));
```
--------------------------------
### Basic Predicate Usage Examples
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Demonstrates the direct availability of factory functions for common predicates like `always`, `gt`, `contains`, and `exists`.
```rust
use predicates::prelude::*;
// Directly available
predicate::always()
predicate::gt(5)
predicate::str::contains("text")
predicate::path::exists()
```
--------------------------------
### Rust predicate composition example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Shows how to combine multiple predicates using logical AND to create more complex conditions, such as checking if a value falls within a specific range.
```rust
use predicates::prelude::*;
// Check if value is between 5 and 10
let between = predicate::ge(5).and(predicate::le(10));
assert_eq!(true, between.eval(&7));
assert_eq!(false, between.eval(&11));
assert_eq!(false, between.eval(&4));
```
--------------------------------
### Usage Examples for Named Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/name-and-boxed.md
Illustrates the usage of named predicates for simple validation, combining named predicates, and constructing complex expressions with named parts.
```rust
use predicates::prelude::*;
// Simple named predicate
let is_valid_username = predicate::function(|s: &str| {
!s.is_empty() && s.len() <= 32 && s.chars().all(|c| c.is_alphanumeric() || c == '_')
}).name("valid_username");
// Combining with names
let is_valid_email = predicate::str::contains("@")
.and(predicate::str::contains("."))
.name("valid_email");
// Complex expression with named parts
let is_valid_password = predicate::function(|s: &str| s.len() >= 8)
.and(predicate::function(|s: &str| s.chars().any(|c| c.is_uppercase())))
.and(predicate::function(|s: &str| s.chars().any(|c| c.is_numeric())))
.name("strong_password");
```
--------------------------------
### Sensor Reading Validation Example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Shows how to use `is_close` with `epsilon()` to validate sensor readings against an expected value within a defined tolerance. This is practical for real-world measurements.
```rust
use predicates::prelude::*;
fn sensor_reading_validation() {
let expected_temp = 98.6; // Fahrenheit
let sensor_tolerance = 0.1; // degrees
let predicate = predicate::float::is_close(expected_temp)
.epsilon(sensor_tolerance);
assert_eq!(true, predicate.eval(&98.65));
assert_eq!(false, predicate.eval(&99.0));
}
```
--------------------------------
### File Content Validation
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Examples for validating file content using predicates.
```APIDOC
## File Content Validation
### Description
Examples for validating file content using predicates.
### Examples
```rust
use std::path::Path;
use predicates::prelude::*
// Check file contents match expected
let pred = predicate::path::eq_file("expected.txt").unwrap();
// Check text file contains string
let pred = predicate::str::contains("TODO")
.from_utf8()
.from_file_path(Path::new("src/main.rs"))
.unwrap();
```
```
--------------------------------
### Type Hinting with `impl Predicate`
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Provides an example of using `impl Predicate` to explicitly define the type when the compiler might not infer it.
```rust
// May need explicit type
let pred: impl Predicate = predicate::gt(5);
```
--------------------------------
### Create an IsEmptyPredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Use this predicate to check if a string is empty. It requires no setup.
```Rust
use predicates::prelude::*;
let predicate = predicate::str::is_empty();
assert_eq!(true, predicate.eval(""));
assert_eq!(false, predicate.eval("Food World"));
```
--------------------------------
### Advanced Examples - Tolerating Measurement Error
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Illustrates using float predicates to validate sensor readings within a specified tolerance.
```APIDOC
## Advanced Examples - Tolerating Measurement Error
### Description
Illustrates using float predicates to validate sensor readings within a specified tolerance.
### Example
```rust
use predicates::prelude::*
fn sensor_reading_validation() {
let expected_temp = 98.6; // Fahrenheit
let sensor_tolerance = 0.1; // degrees
let predicate = predicate::float::is_close(expected_temp)
.epsilon(sensor_tolerance);
assert_eq!(true, predicate.eval(&98.65));
assert_eq!(false, predicate.eval(&99.0));
}
```
```
--------------------------------
### Combined String Predicates for Multi-line Content
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Illustrates combining string predicates with logical AND (.and()) to check for specific patterns at the start and within multi-line content.
```rust
use predicates::prelude::*;
// Multi-line content check
let content = predicate::str::starts_with("#!/bin/sh")
.and(predicate::str::contains("echo"));
```
--------------------------------
### StartsWithPredicate Struct Definition
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Defines a predicate for checking if a string starts with a specified pattern. This predicate is cloneable and comparable.
```rust
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StartsWithPredicate {
// opaque
}
```
--------------------------------
### Valid and Invalid Regex Pattern Examples
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/errors.md
Illustrates the creation of a valid regex predicate and shows how an invalid pattern results in an error. Use `unwrap()` for known valid patterns or handle the `Result` for potentially invalid ones.
```rust
use predicates::prelude::*;
// Valid pattern
let pred = predicate::str::is_match(r"[a-z]+").unwrap();
assert_eq!(true, pred.eval("hello"));
// Invalid pattern - returns error
let result = predicate::str::is_match("[a-");
assert!(result.is_err());
```
--------------------------------
### String Starts With Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Use `predicate::str::starts_with(p)` to create a predicate that checks if a string begins with a specified prefix `p`.
```rust
predicate::str::starts_with(p)
```
--------------------------------
### Combine Predicates using and() method
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/boolean-combinators.md
Computes the logical AND of two predicates using the `and` method from `PredicateBooleanExt`. This example shows combining greater than and less than predicates.
```rust
use predicates::prelude::*;
let p1 = predicate::gt(5);
let p2 = predicate::lt(10);
let combined = p1.and(p2);
assert_eq!(true, combined.eval(&7));
```
--------------------------------
### Combine Path Predicates with OR
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Use the `or` combinator to create a predicate where at least one condition must be true. This example checks if a path is either a file or a directory.
```rust
use std::path::Path;
use predicates::prelude::*;
// Either a file or directory
let pred = predicate::path::is_file()
.or(predicate::path::is_dir());
```
--------------------------------
### Combine Predicates using or() method
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/boolean-combinators.md
Computes the logical OR of two predicates using the `or` method from `PredicateBooleanExt`. This example shows combining equality predicates.
```rust
use predicates::prelude::*;
let p1 = predicate::eq(0);
let p2 = predicate::eq(1);
let combined = p1.or(p2);
assert_eq!(true, combined.eval(&0));
```
--------------------------------
### Rust predicate le example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates using the le predicate to check if a value is less than or equal to a specified constant. This is useful for range checks where the upper bound is inclusive.
```rust
use predicates::prelude::*;
let predicate = predicate::le(5);
assert_eq!(true, predicate.eval(&4));
assert_eq!(true, predicate.eval(&5));
assert_eq!(false, predicate.eval(&6));
```
--------------------------------
### Rust predicate eq example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates using the eq predicate for both numeric and string equality checks. Ensure the type matches the predicate's expected type.
```rust
use predicates::prelude::*;
let predicate = predicate::eq(5);
assert_eq!(true, predicate.eval(&5));
assert_eq!(false, predicate.eval(&10));
let predicate = predicate::eq("Hello");
assert_eq!(true, predicate.eval("Hello"));
assert_eq!(false, predicate.eval("Goodbye"));
```
--------------------------------
### Rust predicate ne example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates using the ne predicate to assert that a value is not equal to a specific constant. This is useful for verifying that a value differs from an expected one.
```rust
use predicates::prelude::*;
let predicate = predicate::ne(5);
assert_eq!(false, predicate.eval(&5));
assert_eq!(true, predicate.eval(&10));
```
--------------------------------
### Creating Predicates with Factory Functions
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/README.md
Predicates are created using factory functions within the `predicate::` namespace. Examples include equality, less than, string containment, and path existence checks.
```rust
use predicates::prelude::*;
let eq = predicate::eq(5);
let lt = predicate::lt(10);
let contains = predicate::str::contains("hello");
let exists = predicate::path::exists();
```
--------------------------------
### Rust predicate ge example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates using the ge predicate to check if a value is greater than or equal to a specified constant. This is useful for range checks where the lower bound is inclusive.
```rust
use predicates::prelude::*;
let predicate = predicate::ge(5);
assert_eq!(false, predicate.eval(&4));
assert_eq!(true, predicate.eval(&5));
assert_eq!(true, predicate.eval(&6));
```
--------------------------------
### Floating-Point Arithmetic Precision Example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Illustrates how standard equality checks can fail due to floating-point precision issues, while `is_close` predicate succeeds. This highlights the importance of using tolerance-based comparisons.
```rust
use predicates::prelude::*;
fn floating_point_sum() {
// Due to floating-point precision, this doesn't equal 0.45
let a = 0.1 + 0.1 + 0.25;
let b = 0.15 + 0.15 + 0.15;
// Regular equality fails
assert_ne!(a, b);
// But close comparison succeeds
let pred = predicate::float::is_close(a);
assert_eq!(true, pred.eval(&b));
}
```
--------------------------------
### Full Prelude Import
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
The recommended way to import all necessary items from the predicates crate for general use.
```rust
use predicates::prelude::*;
```
--------------------------------
### Create and Evaluate a Basic Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Demonstrates how to create a predicate (e.g., greater than 5) and evaluate it against a value.
```rust
use predicates::prelude::*;
let pred = predicate::gt(5);
assert!(pred.eval(&10));
```
--------------------------------
### Parameter<'a>
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Represents a parameter of a predicate. It has methods to get the parameter name and its displayable value.
```APIDOC
## Parameter<'a>
### Description
Represents a parameter of a predicate.
### Methods
- `name(&self) -> &str`
- `value(&self) -> &dyn fmt::Display`
```
--------------------------------
### Handling Reflection System I/O Errors
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/errors.md
Demonstrates how to check for and process potential I/O errors within the reflection system, specifically when using `find_case()`. The `case.products()` can contain error information if file operations fail.
```rust
use std::path::Path;
use predicates::prelude::*;
let pred = predicate::path::is_file();
if let Some(case) = pred.find_case(false, Path::new("nonexistent")) {
// case.products() may contain error information
for product in case.products() {
println!("{}", product);
}
}
```
--------------------------------
### Complete Predicates Configuration with All Features
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/configuration.md
Enable all available features for predicates-rs by listing them explicitly in Cargo.toml.
```toml
[dependencies]
predicates = { version = "3.1", features = ["diff", "regex", "float-cmp", "normalize-line-endings", "color"] }
```
--------------------------------
### Check File System Path Properties
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Shows how to create a predicate for checking if a given path refers to a file.
```rust
let is_file = predicate::path::is_file();
```
--------------------------------
### Programmatic Predicate Configuration
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/configuration.md
Shows how to configure predicates programmatically using chained methods and function closures. This approach avoids external configuration files or environment variables.
```rust
use predicates::prelude::*;
// Configuration example
let my_validator = predicate::function(|x: &i32| x > 0)
.and(predicate::function(|x: &i32| x < 100))
.name("valid_percentage")
.boxed();
```
--------------------------------
### Path Predicate Adapter: Follow Links
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Use the `.follow_links(yes)` adapter method to control whether symbolic links should be followed during path predicate evaluations. `yes` is a boolean.
```rust
follow_links(yes)
```
--------------------------------
### Combine Predicates using not() method
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/boolean-combinators.md
Inverts the result of a predicate using the `not` method from `PredicateBooleanExt`. This example shows inverting an equality predicate.
```rust
use predicates::prelude::*;
let inverted = predicate::eq(5).not();
assert_eq!(true, inverted.eval(&3));
assert_eq!(false, inverted.eval(&5));
```
--------------------------------
### IsClosePredicate with Epsilon for Near-Zero Comparisons
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Demonstrates using the `epsilon()` method to set absolute tolerance for comparisons near zero. This is useful when dealing with very small numbers.
```rust
use predicates::prelude::*;
let predicate = predicate::float::is_close(0.0)
.epsilon(1e-10);
assert_eq!(true, predicate.eval(&1e-11));
```
--------------------------------
### never()
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/constant-predicates.md
Creates a new predicate that always returns false. This predicate can be used with any type and is useful as a starting point for building more complex predicates.
```APIDOC
## never()
### Description
Creates a new predicate that always returns `false`.
### Returns
`BooleanPredicate`
### Example
```rust
use predicates::prelude::*
let predicate = predicate::never();
assert_eq!(false, predicate.eval(&5));
assert_eq!(false, predicate.eval(&"anything"));
assert_eq!(false, predicate.eval(&vec![1, 2, 3]));
```
```
--------------------------------
### always()
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/constant-predicates.md
Creates a new predicate that always returns true. This predicate can be used with any type and is useful as a starting point for building more complex predicates.
```APIDOC
## always()
### Description
Creates a new predicate that always returns `true`.
### Returns
`BooleanPredicate`
### Example
```rust
use predicates::prelude::*
let predicate = predicate::always();
assert_eq!(true, predicate.eval(&5));
assert_eq!(true, predicate.eval(&"anything"));
assert_eq!(true, predicate.eval(&vec![1, 2, 3]));
```
```
--------------------------------
### Type Hinting with Turbofish
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Demonstrates using the turbofish syntax (`::<_>`) for type inference with iterator-based predicates.
```rust
// Or use turbofish
let pred = predicate::in_iter::<_, i32>(vec![1, 2, 3]);
```
--------------------------------
### Fallible Predicate Creation with File Path
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Shows how to create a predicate that compares against a file's path, which can also fail.
```rust
// File reading may fail
let pred = predicate::path::eq_file("expected.txt")?;
```
--------------------------------
### Create a StartsWithPredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Use this predicate to check if a string begins with a specific pattern. Provide the pattern as an argument.
```Rust
use predicates::prelude::*;
let predicate = predicate::str::starts_with("Hello");
assert_eq!(true, predicate.eval("Hello World"));
assert_eq!(false, predicate.eval("Goodbye World"));
```
--------------------------------
### Create a Vector of Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Demonstrates two ways to create a vector of boxed predicates. The first uses explicit boxing, while the second uses the `.boxed()` method for conciseness.
```rust
let preds: Vec + Send + Sync>> = vec![
Box::new(predicate::eq(1)),
Box::new(predicate::gt(10)),
];
// Or:
let preds: Vec<_> = vec![
predicate::eq(1).boxed(),
predicate::gt(10).boxed(),
];
```
--------------------------------
### Accessing predicates via Prelude
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/module-structure.md
Import all necessary items from the `predicates::prelude` module to use predicate factory functions directly.
```rust
use predicates::prelude::*;
let pred = predicate::gt(5);
```
--------------------------------
### Rust Create Never False Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/constant-predicates.md
Creates a predicate that consistently evaluates to false, regardless of the input type. Useful as a starting point for predicate combinators.
```rust
use predicates::prelude::*;
let predicate = predicate::never();
assert_eq!(false, predicate.eval(&5));
assert_eq!(false, predicate.eval(&"anything"));
assert_eq!(false, predicate.eval(&vec![1, 2, 3]));
```
--------------------------------
### Rust Create Always True Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/constant-predicates.md
Creates a predicate that consistently evaluates to true, regardless of the input type. Useful as a starting point for predicate combinators.
```rust
use predicates::prelude::*;
let predicate = predicate::always();
assert_eq!(true, predicate.eval(&5));
assert_eq!(true, predicate.eval(&"anything"));
assert_eq!(true, predicate.eval(&vec![1, 2, 3]));
```
--------------------------------
### Fallible Predicate Creation from File Content
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Demonstrates creating a predicate that compares against a file's content, handling potential file reading errors.
```rust
// File content reading may fail
let pred = str_pred.from_file_path(Path::new("file.txt"))?;
```
--------------------------------
### Check File Content Equality
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Use `predicate::path::eq_file()` to create a predicate that compares the content of a file against a reference file. Ensure the reference file exists and is accessible.
```rust
use std::path::Path;
use predicates::prelude::*;
// Check file contents match expected
let pred = predicate::path::eq_file("expected.txt").unwrap();
```
--------------------------------
### Follow Symbolic Links for Path Checks
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Use `follow_links(true)` on a path predicate to make it resolve symbolic links and operate on the target file or directory. This is useful when you need to check the properties of the linked item.
```rust
use std::path::Path;
use predicates::prelude::*;
// Follow symlinks to their targets
let pred = predicate::path::is_file().follow_links(true);
```
--------------------------------
### Thread-Safe Predicate Usage
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Demonstrates how to use predicates concurrently by spawning a new thread, ensuring they are `Send + Sync`.
```rust
let pred = predicate::gt(5);
std::thread::spawn(move || {
assert!(pred.eval(&10));
});
```
--------------------------------
### Rust predicate gt example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates using the gt predicate to check if a value is strictly greater than a specified constant. This is useful for range checks where the lower bound is exclusive.
```rust
use predicates::prelude::*;
let predicate = predicate::gt(5);
assert_eq!(false, predicate.eval(&4));
assert_eq!(false, predicate.eval(&5));
assert_eq!(true, predicate.eval(&6));
```
--------------------------------
### Create Simple Even Number Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/function-predicate.md
Demonstrates creating a simple predicate using a closure to check if an integer is even. The closure captures the input by reference.
```rust
use predicates::prelude::*
let is_even = predicate::function(|&x: &i32| x % 2 == 0);
assert_eq!(true, is_even.eval(&4));
assert_eq!(false, is_even.eval(&5))
```
--------------------------------
### Rust predicate lt example
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/comparison-predicates.md
Demonstrates using the lt predicate to check if a value is strictly less than a specified constant. This is useful for range checks where the upper bound is exclusive.
```rust
use predicates::prelude::*;
let predicate = predicate::lt(5);
assert_eq!(true, predicate.eval(&4));
assert_eq!(false, predicate.eval(&5));
assert_eq!(false, predicate.eval(&6));
```
--------------------------------
### Tolerance Strategies - For Near-Zero Comparisons
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Use `epsilon()` to set absolute tolerance. This is useful for comparisons near zero.
```APIDOC
## Tolerance Strategies - For Near-Zero Comparisons
### Description
Use `epsilon()` to set absolute tolerance. This is useful for comparisons near zero.
### Example
```rust
use predicates::prelude::*
let predicate = predicate::float::is_close(0.0)
.epsilon(1e-10);
assert_eq!(true, predicate.eval(&1e-11));
```
```
--------------------------------
### Create FileType Predicate - Is File
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Creates a predicate that checks if a path points to a regular file. Ensure `predicates::prelude::*` is imported.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::is_file();
assert_eq!(true, predicate.eval(Path::new("Cargo.toml")));
```
--------------------------------
### Create File Content Predicate - Equal File
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Creates a predicate that compares the content of a path with the content of a reference file. This function returns a `Result` and may fail if the reference file cannot be read.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::eq_file("expected.txt").unwrap();
assert_eq!(true, predicate.eval(Path::new("actual.txt")));
```
--------------------------------
### Create FileType Predicate - Is Directory
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Creates a predicate that checks if a path points to a directory. Ensure `predicates::prelude::*` is imported.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::is_dir();
assert_eq!(true, predicate.eval(Path::new("src")));
```
--------------------------------
### Core Trait Method: Find Mismatch Case
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Use the `.find_case(exp, v)` method to get diagnostic information about why a predicate failed. It returns an `Option` which contains details if the predicate evaluated to `false`.
```rust
find_case(exp, v)
```
--------------------------------
### Fallible Predicate Creation with Regex
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Illustrates creating a regex predicate that can fail, using the `?` operator to propagate errors.
```rust
// Regex compilation may fail
let pred = predicate::str::is_match(r"\d+")?;
```
--------------------------------
### Create AndPredicate using and()
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/boolean-combinators.md
Combines two predicates with logical AND. Use the `and()` method from `PredicateBooleanExt` to create this.
```rust
let combined = predicate::gt(5).and(predicate::lt(10));
```
--------------------------------
### Configure FileType Predicate - Follow Links
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Configures a `FileTypePredicate` to follow symbolic links when determining the file type. By default, symlinks are not followed.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::is_file().follow_links(true);
```
--------------------------------
### Create Existence Predicate - Missing
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Creates a predicate that checks if a given path does not exist. Ensure the `predicates::prelude::*` is imported.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::missing();
assert_eq!(true, predicate.eval(Path::new("non-existent-file.foo")));
```
--------------------------------
### Validate File Configuration Existence and Type
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/README.md
Checks if a given path exists and is a file, useful for validating configuration files.
```rust
use std::path::Path;
use predicates::prelude::*;
let config_exists = predicate::path::exists()
.and(predicate::path::is_file());
assert!(config_exists.eval(Path::new("config.toml")));
```
--------------------------------
### Path Predicate Adapter: Compare File Contents as UTF-8
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Use `.eq_file(...).utf8()` to compare the contents of two paths as UTF-8 strings. This requires reading the files as UTF-8.
```rust
eq_file(...).utf8()
```
--------------------------------
### Handling std::io::Error during File Predicate Creation
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/errors.md
Shows how to handle potential `std::io::Error` when creating file-related predicates like `predicate::path::eq_file()`. This is necessary when file access might fail due to non-existence or permissions.
```rust
use std::path::Path;
use predicates::prelude::*;
match predicate::path::eq_file("expected.txt") {
Ok(predicate) => {
// Use the predicate
println!("Predicate created");
}
Err(e) => {
// Handle I/O error
eprintln!("Cannot read file: {}", e);
}
}
```
--------------------------------
### Store Different Predicate Types Together
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Demonstrates how to store heterogeneous predicate types in a collection using `boxed()` for dynamic dispatch.
```rust
let predicates: Vec<_> = vec![
predicate::gt(0).boxed(),
predicate::lt(100).boxed(),
];
```
--------------------------------
### Existence Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Functions to create predicates for checking the existence of file paths.
```APIDOC
## exists
### Description
Creates a predicate that ensures a path exists.
### Returns
`ExistencePredicate`
### Example
```rust
use std::path::Path;
use predicates::prelude::*
let predicate = predicate::path::exists();
assert_eq!(true, predicate.eval(Path::new("Cargo.toml")));
```
```
```APIDOC
## missing
### Description
Creates a predicate that ensures a path does not exist.
### Returns
`ExistencePredicate`
### Example
```rust
use std::path::Path;
use predicates::prelude::*
let predicate = predicate::path::missing();
assert_eq!(true, predicate.eval(Path::new("non-existent-file.foo")));
```
```
--------------------------------
### Import by Module
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Import specific modules like `constant`, `ord`, `str`, or `path` for more granular control over dependencies.
```rust
use predicates::{constant, ord, str, path};
```
--------------------------------
### Add predicates dependency with all features
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/module-structure.md
Include the predicates library with all features enabled to access all available modules, including optional ones like regex and float comparison.
```toml
[dependencies]
predicates = "3.1"
```
--------------------------------
### Combined String Predicates with OR
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Shows how to combine string predicates using logical OR (.or()) to check for multiple acceptable conditions, like valid file extensions.
```rust
use predicates::prelude::*;
// Valid file extension
let file_pred = predicate::str::ends_with(".rs")
.or(predicate::str::ends_with(".toml"));
```
--------------------------------
### Standard Imports for Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Imports the prelude, which includes the `Predicate` trait, all extension traits, and the `predicate` namespace for factories.
```rust
use predicates::prelude::*
```
--------------------------------
### Path Predicate Adapter: Apply String Predicate to File Contents
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Use `utf8_pred.from_file_path(p)` to apply a string predicate to the contents of a file at path `p`. This returns a `Result` and requires reading the file as UTF-8.
```rust
utf8_pred.from_file_path(p)
```
--------------------------------
### Product
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Represents a by-product generated during predicate evaluation. It holds a name and a displayable value.
```APIDOC
## Product
### Description
A by-product of predicate evaluation. It holds a name and a displayable value.
### Methods
- `name(&self) -> &str`: Returns the name of the by-product.
- `value(&self) -> &dyn fmt::Display`: Returns the value of the by-product, which implements the `fmt::Display` trait.
```
--------------------------------
### StrFilePredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Compares a file's text contents.
```APIDOC
## StrFilePredicate
### Description
Compares a file's text contents.
### Constructed by
`predicate::path::eq_file(path)?.utf8()`
```
--------------------------------
### Converting a Predicate to BoxPredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/name-and-boxed.md
Demonstrates using the `boxed` method from `PredicateBoxExt` to convert a predicate into a dynamically-dispatched `BoxPredicate`. This is useful for storing predicates in collections.
```rust
use predicates::prelude::*;
let predicates = vec![
predicate::always().boxed(),
predicate::never().boxed(),
];
assert_eq!(true, predicates[0].eval(&4));
assert_eq!(false, predicates[1].eval(&4));
```
--------------------------------
### Dynamic Predicate Construction with a Match Statement
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/name-and-boxed.md
Illustrates building a predicate dynamically based on a string input using a `match` statement. The function returns a boxed trait object.
```rust
use predicates::prelude::*;
fn build_validator(field_type: &str) -> Box + Send + Sync> {
match field_type {
"email" => Box::new(predicate::str::contains("@")),
"username" => Box::new(predicate::str::is_empty().not()),
"url" => Box::new(predicate::str::starts_with("http")),
_ => Box::new(predicate::always()),
}
}
```
--------------------------------
### ExistencePredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Checks if a path exists or is missing.
```APIDOC
## ExistencePredicate
### Description
Checks if a path exists or is missing.
### Constructed by
`predicate::path::exists()`, `predicate::path::missing()`
```
--------------------------------
### Combined String Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Demonstrates combining multiple string predicates using logical AND (.and()) and NOT (.not()) for complex validation, such as email format.
```rust
use predicates::prelude::*;
// Email-like validation
let email_predicate = predicate::str::contains("@")
.and(predicate::str::contains("."))
.and(predicate::str::contains(" ").not());
```
--------------------------------
### Use Diff Predicate for String Comparison
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/configuration.md
Demonstrates using the `predicate::str::diff()` function, which is enabled by the 'diff' feature. On mismatch, it displays a unified diff.
```rust
use predicates::prelude::*;
let pred = predicate::str::diff("expected\ncontent");
// On mismatch, displays unified diff
```
--------------------------------
### Create OrPredicate using or()
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/boolean-combinators.md
Combines two predicates with logical OR. Use the `or()` method from `PredicateBooleanExt` to create this.
```rust
let combined = predicate::eq(0).or(predicate::eq(1));
```
--------------------------------
### Use Regex Predicate for String Matching
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/configuration.md
Shows how to use `predicate::str::is_match()`, enabled by the 'regex' feature, to check if a string matches a given regular expression.
```rust
use predicates::prelude::*;
let pred = predicate::str::is_match(r"^\d+$").unwrap();
```
--------------------------------
### Comparison Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/MANIFEST.md
Offers factory functions for creating predicates that perform equality and ordering comparisons.
```APIDOC
## Factory Functions for Comparison Predicates
### Description
Functions to create predicates for comparing items.
### Functions
- **eq(value: T) -> EqPredicate**
Creates a predicate that checks for equality.
- **ne(value: T) -> EqPredicate**
Creates a predicate that checks for inequality.
- **lt(value: T) -> OrdPredicate**
Creates a predicate that checks if an item is less than a value.
- **le(value: T) -> OrdPredicate**
Creates a predicate that checks if an item is less than or equal to a value.
- **gt(value: T) -> OrdPredicate**
Creates a predicate that checks if an item is greater than a value.
- **ge(value: T) -> OrdPredicate**
Creates a predicate that checks if an item is greater than or equal to a value.
```
--------------------------------
### Combine Multiple Conditions with Logical Operators
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Shows how to combine predicates using logical AND and LE operators to define a range.
```rust
let range = predicate::ge(5).and(predicate::le(10));
```
--------------------------------
### Path Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/MANIFEST.md
Provides factory functions for creating predicates that operate on file paths.
```APIDOC
## Factory Functions for Path Predicates
### Description
Functions to create predicates for checking properties of file paths, such as existence, file type, and symbolic link following.
### Functions (Examples)
- **exists() -> ExistencePredicate**
- **is_file() -> FileTypePredicate**
- **is_dir() -> FileTypePredicate**
- **follow_links(follow: bool) -> PathPredicate**
### Methods (Examples)
- **utf8()**
```
--------------------------------
### Tolerance Strategies - For Large Number Comparisons
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Use `ulps()` to set relative tolerance. This is useful for comparisons with large numbers.
```APIDOC
## Tolerance Strategies - For Large Number Comparisons
### Description
Use `ulps()` to set relative tolerance. This is useful for comparisons with large numbers.
### Example
```rust
use predicates::prelude::*
let predicate = predicate::float::is_close(1e10)
.ulps(10);
assert_eq!(true, predicate.eval(&(1e10 + 1.0)));
```
```
--------------------------------
### Apply Byte Predicates to File Contents
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Use `from_file_path` to adapt a byte predicate to operate on the content of a file specified by its path. This is useful for validating file contents against byte patterns.
```rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::str::contains("license")
.from_utf8()
.from_file_path(Path::new("LICENSE.md"))
.unwrap();
```
--------------------------------
### Path Is File Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Use `predicate::path::is_file()` to create a predicate that checks if a given path points to a regular file.
```rust
predicate::path::is_file()
```
--------------------------------
### Wrap a Closure as a Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/INDEX.md
Illustrates how to use custom logic by wrapping a closure that performs an even number check.
```rust
let custom = predicate::function(|x: &i32| x % 2 == 0);
```
--------------------------------
### Type Hinting with `Box`
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/quick-reference.md
Shows how to explicitly type a vector of boxed dynamic predicates, including adding a specific string predicate.
```rust
// Or with context
let mut v: Vec>> = vec![];
v.push(Box::new(predicate::str::is_empty()));
```
--------------------------------
### Enable Float-Cmp Feature
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
To use float predicates, the `float-cmp` feature must be enabled in your `Cargo.toml` file.
```toml
[dependencies]
predicates = { version = "3.1", features = ["float-cmp"] }
```
--------------------------------
### Product Struct Definition
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Represents a by-product generated during predicate evaluation. It exposes the name and displayable value of the product.
```rust
pub struct Product {
// opaque
}
```
--------------------------------
### Add predicates-rs Dependency
Source: https://github.com/assert-rs/predicates-rs/blob/master/README.md
Add this to your Cargo.toml to include the predicates-rs library in your project.
```toml
[dependencies]
predicates = "3.1.4"
```
--------------------------------
### Float Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/MANIFEST.md
Offers a factory function for creating predicates that compare floating-point numbers with tolerance.
```APIDOC
## Factory Function for Float Predicates
### Description
Creates a predicate for comparing floating-point numbers with a specified tolerance.
### Function
- **is_close(expected: f64, tolerance: f64) -> IsClosePredicate**
Creates a predicate that checks if a float is close to an expected value within a given tolerance.
### Methods
- **distance()**
- **epsilon()**
- **ulps()**
```
--------------------------------
### IsClosePredicate with ULPS for Large Number Comparisons
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Demonstrates using the `ulps()` method to set relative tolerance for comparisons involving large numbers. This is useful when the magnitude of the numbers is significant.
```rust
use predicates::prelude::*;
let predicate = predicate::float::is_close(1e10)
.ulps(10);
assert_eq!(true, predicate.eval(&(1e10 + 1.0)));
```
--------------------------------
### Create FileType Predicate - From Path
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Creates a `FileTypePredicate` by probing the actual file type at the specified path. This function returns a `Result` and may fail if the path cannot be accessed.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::FileTypePredicate::from_path(Path::new("src")).unwrap();
```
--------------------------------
### Unified Error Handling for Predicate Creation
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/errors.md
Combines handling of potential errors during the creation of both file and content predicates using the `?` operator within a function returning `Result`. This pattern simplifies error propagation for predicate construction.
```rust
use std::path::Path;
use predicates::prelude::*;
fn validate_files() -> Result<(), Box> {
// Create predicates - these can fail
let file_check = predicate::path::eq_file("expected.txt")?;
let content_check = predicate::str::contains("TODO")
.from_utf8()
.from_file_path(Path::new("src/main.rs"))?;
// Evaluate predicates - these always succeed
let is_same = file_check.eval(Path::new("actual.txt"));
let has_todo = content_check.eval(Path::new("src/main.rs"));
println!("Files match: {}", is_same);
println!("Has TODO: {}", has_todo);
Ok(())
}
```
--------------------------------
### Composing Predicates with AND
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/00-START-HERE.txt
Demonstrates composing two predicates using the `.and()` method. This creates a new predicate that is true only if both original predicates are true.
```rust
.and(other)
```
--------------------------------
### Convert Binary File Predicate to Text
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Converts a `BinaryFilePredicate` into a `StrFilePredicate` to enable text-based comparisons of file content. This is useful for UTF-8 encoded files.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::eq_file("expected.txt").utf8();
```
--------------------------------
### Combining Iterator Predicates with Other Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/iterator-predicates.md
Demonstrates how to combine an iterator predicate with other predicates using logical OR (`or`). This allows for complex condition checking.
```rust
use predicates::prelude::*;
let valid_codes = vec![200, 201, 204, 301, 304];
let predicate = predicate::in_iter(valid_codes)
.sort()
.or(predicate::eq(404)); // Accept valid codes or 404
assert_eq!(true, predicate.eval(&200));
assert_eq!(true, predicate.eval(&404));
assert_eq!(false, predicate.eval(&500));
```
--------------------------------
### diff
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Creates a predicate that compares strings and shows a diff-style output on mismatch. Requires the 'diff' feature.
```APIDOC
## diff
### Description
Predicate that compares strings with diff-style output on mismatch.
### Function Signature
```rust
pub fn diff(expected: S) -> DifferencePredicate
where
S: Into,
```
### Parameters
#### Path Parameters
- **expected** (S) - Required - The expected string value
### Returns
- `DifferencePredicate`
### Example
```rust
use predicates::prelude::*
let predicate = predicate::str::diff("Hello\nWorld");
// On mismatch, displays a diff-style output
```
```
--------------------------------
### IsClosePredicate with Distance for Rounding Error Tolerance
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/float-predicates.md
Demonstrates using the `distance()` method for general rounding error handling in floating-point comparisons. This is a common use case for typical floating-point arithmetic.
```rust
use predicates::prelude::*;
// Typical floating-point arithmetic error tolerance
let predicate = predicate::float::is_close(0.45).distance(5);
```
--------------------------------
### DifferencePredicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/types.md
Compares strings and shows unified diff on mismatch. Requires the 'diff' feature.
```APIDOC
## DifferencePredicate
### Description
Compares strings and shows unified diff on mismatch. (requires `diff` feature)
### Constructed by
`predicate::str::diff(expected)`
```
--------------------------------
### Create Difference Predicate
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/string-predicates.md
Creates a predicate that compares strings and shows a diff-style output on mismatch. Requires the 'diff' feature.
```rust
use predicates::prelude::*;
let predicate = predicate::str::diff("Hello\nWorld");
// On mismatch, displays a diff-style output
```
--------------------------------
### Enable Specific Cargo Features for Predicates
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/configuration.md
Specify desired features like diff, regex, and float-cmp in your Cargo.toml to enable optional functionality.
```toml
[dependencies]
predicates = { version = "3.1", features = ["diff", "regex", "float-cmp"] }
```
--------------------------------
### Create Existence Predicate - Exists
Source: https://github.com/assert-rs/predicates-rs/blob/master/_autodocs/api-reference/path-predicates.md
Creates a predicate that checks if a given path exists. Ensure the `predicates::prelude::*` is imported.
```Rust
use std::path::Path;
use predicates::prelude::*;
let predicate = predicate::path::exists();
assert_eq!(true, predicate.eval(Path::new("Cargo.toml")));
```