proptest/
path.rs

1//! Strategies for generating [`PathBuf`] and related path types.
2//!
3//! [`PathParams`] in this module is used as the argument to the
4//! [`Arbitrary`](crate::arbitrary::Arbitrary) implementation for [`PathBuf`].
5
6use crate::{collection::SizeRange, string::StringParam};
7
8/// Parameters for the [`Arbitrary`] implementation for [`PathBuf`].
9///
10/// By default, this generates paths with 0 to 8 components uniformly at random, each of which is a
11/// default [`StringParam`].
12#[derive(Clone, Debug, PartialEq, Eq, Hash)]
13pub struct PathParams {
14    /// The number of components in the path.
15    components: SizeRange,
16    /// The regular expression to generate individual components.
17    component_regex: StringParam,
18}
19
20impl PathParams {
21    /// Gets the number of components in the path.
22    pub fn components(&self) -> SizeRange {
23        self.components.clone()
24    }
25
26    /// Sets the number of components in the path.
27    pub fn with_components(mut self, components: impl Into<SizeRange>) -> Self {
28        self.components = components.into();
29        self
30    }
31
32    /// Gets the regular expression to generate individual components.
33    pub fn component_regex(&self) -> StringParam {
34        self.component_regex
35    }
36
37    /// Sets the regular expression to generate individual components.
38    pub fn with_component_regex(
39        mut self,
40        component_regex: impl Into<StringParam>,
41    ) -> Self {
42        self.component_regex = component_regex.into();
43        self
44    }
45}
46
47impl Default for PathParams {
48    fn default() -> Self {
49        Self {
50            components: (0..8).into(),
51            // This is the default regex for `any::<String>()`.
52            component_regex: StringParam::default(),
53        }
54    }
55}