rstmt_nrt/transform/
config.rs

1/*
2    Appellation: config <module>
3    Contrib: @FL03
4*/
5
6/// The [`PathFinderConfig`] object provides a standard interface for configuring various
7/// implemented transformers.
8#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
9#[cfg_attr(
10    feature = "serde",
11    derive(serde::Serialize, serde::Deserialize),
12    serde(default, rename_all = "snake_case")
13)]
14pub struct PathFinderConfig {
15    /// Maximum search depth for pathfinding
16    pub(crate) depth: usize,
17    /// Maximum number of paths to find
18    pub(crate) paths: usize,
19}
20
21impl PathFinderConfig {
22    /// the default maximum search depth for pathfinding
23    pub const DEFAULT_MAX_DEPTH: usize = 5;
24    /// the default maximum number of paths to find
25    pub const DEFAULT_MAX_PATHS: usize = 5;
26
27    /// returns a new instance of the [`PathFinderConfig`] with the given values
28    pub fn new(depth: usize, paths: usize) -> Self {
29        Self {
30            depth, // Default search depth
31            paths, // default number of paths to find
32        }
33    }
34    /// returns a new instance of the [`PathFinderConfig`] with the given depth and default
35    /// [`paths`](Self::DEFAULT_MAX_PATHS)
36    pub fn from_depth(depth: usize) -> Self {
37        Self {
38            depth,                          // Default search depth
39            paths: Self::DEFAULT_MAX_PATHS, // default number of paths to find
40        }
41    }
42    /// returns a new instance of the [`PathFinderConfig`] with the given paths and default
43    /// [`depth`](Self::DEFAULT_MAX_DEPTH)
44    pub fn from_paths(paths: usize) -> Self {
45        Self {
46            depth: Self::DEFAULT_MAX_DEPTH, // Default search depth
47            paths,                          // default number of paths to find
48        }
49    }
50    /// returns the maximum depth for pathfinding
51    pub const fn max_depth(&self) -> usize {
52        self.depth
53    }
54    /// returns a mutable reference to the maximum depth for pathfinding
55    pub const fn max_depth_mut(&mut self) -> &mut usize {
56        &mut self.depth
57    }
58    /// returns the maximum number of paths to find
59    pub const fn max_paths(&self) -> usize {
60        self.paths
61    }
62    /// returns a mutable reference to the maximum number of paths to find
63    pub const fn max_paths_mut(&mut self) -> &mut usize {
64        &mut self.paths
65    }
66    /// set the maximum depth for pathfinding
67    pub fn set_max_depth(&mut self, depth: usize) -> &mut Self {
68        self.depth = depth;
69        self
70    }
71    /// set the maximum number of paths to find
72    pub fn set_max_paths(&mut self, paths: usize) -> &mut Self {
73        self.paths = paths;
74        self
75    }
76    /// consumes the current instance to create another with the given maximum depth
77    pub fn with_max_depth(self, depth: usize) -> Self {
78        Self { depth, ..self }
79    }
80    /// consumes the current instance to create another with the given maximum number of paths
81    pub fn with_max_paths(self, paths: usize) -> Self {
82        Self { paths, ..self }
83    }
84}
85
86impl Default for PathFinderConfig {
87    fn default() -> Self {
88        Self {
89            depth: Self::DEFAULT_MAX_DEPTH,
90            paths: Self::DEFAULT_MAX_PATHS,
91        }
92    }
93}
94
95impl core::fmt::Display for PathFinderConfig {
96    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
97        write!(f, "{{ depth: {}, paths: {} }}", self.depth, self.paths)
98    }
99}