Skip to content

Commit 6b7e998

Browse files
authored
Merge pull request #30 from tomoikey/feat/doc_comment
add doc comments
2 parents 19f3cc2 + fb63fee commit 6b7e998

File tree

4 files changed

+86
-15
lines changed

4 files changed

+86
-15
lines changed

src/refined.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ impl<RULE, T> Refined<RULE>
5757
where
5858
RULE: Rule<Item = T>,
5959
{
60+
/// Creates a new `Refined` instance if the provided value satisfies the rule.
61+
///
62+
/// # Arguments
63+
///
64+
/// * `value` - The value to be refined.
65+
///
66+
/// # Returns
67+
///
68+
/// * `Result<Self, Error<T>>` - A `Refined` instance if the value satisfies the rule, otherwise an error.
69+
///
70+
/// # Example
71+
///
72+
/// ```rust
73+
/// use refined_type::rule::NonEmptyStringRule;
74+
/// use refined_type::Refined;
75+
///
76+
/// let non_empty_string = Refined::<NonEmptyStringRule>::new("Hello".to_string());
77+
/// assert!(non_empty_string.is_ok());
78+
///
79+
/// let empty_string = Refined::<NonEmptyStringRule>::new("".to_string());
80+
/// assert!(empty_string.is_err());
81+
/// ```
6082
pub fn new(value: T) -> Result<Self, Error<T>> {
6183
let value = RULE::validate(value).map_err(|e| {
6284
let message = e.to_string();
@@ -65,6 +87,28 @@ where
6587
Ok(Self { value })
6688
}
6789

90+
/// Creates a new `Refined` instance if the provided value satisfies the rule.
91+
///
92+
/// # Arguments
93+
///
94+
/// * `value` - The value to be refined.
95+
///
96+
/// # Panics
97+
///
98+
/// This function will panic if the value does not satisfy the rule.
99+
///
100+
/// # Example
101+
///
102+
/// ```rust
103+
/// use refined_type::rule::NonEmptyStringRule;
104+
/// use refined_type::Refined;
105+
///
106+
/// let non_empty_string = Refined::<NonEmptyStringRule>::unsafe_new("Hello".to_string());
107+
/// assert_eq!(non_empty_string.into_value(), "Hello");
108+
///
109+
/// // This will panic
110+
/// // let empty_string = Refined::<NonEmptyStringRule>::unsafe_new("".to_string());
111+
/// ```
68112
pub fn unsafe_new(value: T) -> Self
69113
where
70114
T: Debug,
@@ -118,10 +162,40 @@ where
118162
Refined::new(f(self.into_value()))
119163
}
120164

165+
/// Returns a reference to the value inside the `Refined` type.
166+
///
167+
/// # Returns
168+
///
169+
/// * `&RULE::Item` - A reference to the value inside the `Refined` type.
170+
///
171+
/// # Example
172+
///
173+
/// ```rust
174+
/// use refined_type::rule::NonEmptyStringRule;
175+
/// use refined_type::Refined;
176+
///
177+
/// let non_empty_string = Refined::<NonEmptyStringRule>::new("Hello".to_string()).unwrap();
178+
/// assert_eq!(non_empty_string.value(), "Hello");
179+
/// ```
121180
pub fn value(&self) -> &RULE::Item {
122181
&self.value
123182
}
124183

184+
/// Consumes the `Refined` instance and returns the inner value.
185+
///
186+
/// # Returns
187+
///
188+
/// * `RULE::Item` - The value inside the `Refined` type.
189+
///
190+
/// # Example
191+
///
192+
/// ```rust
193+
/// use refined_type::rule::NonEmptyStringRule;
194+
/// use refined_type::Refined;
195+
///
196+
/// let non_empty_string = Refined::<NonEmptyStringRule>::new("Hello".to_string()).unwrap();
197+
/// assert_eq!(non_empty_string.into_value(), "Hello");
198+
/// ```
125199
pub fn into_value(self) -> RULE::Item {
126200
self.value
127201
}

src/rule/collection/exists.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::{HashMap, HashSet, VecDeque};
22

33
use crate::rule::composer::Not;
4-
use crate::rule::{ForAllRule, Iterable, Rule};
4+
use crate::rule::{ForAllRule, Rule};
55
use crate::Refined;
66

77
/// A type that holds a value satisfying the `ExistsRule`
@@ -23,8 +23,7 @@ pub type ExistsHashMap<K, RULE> = Refined<ExistsHashMapRule<K, RULE>>;
2323
pub type ExistsString<RULE> = Refined<ExistsStringRule<RULE>>;
2424

2525
/// Rule where at least one data in the collection satisfies the condition
26-
pub type ExistsRule<RULE, ITERABLE> =
27-
Not<ForAllRule<Not<RULE>, ITERABLE, <ITERABLE as Iterable>::Item>>;
26+
pub type ExistsRule<RULE, ITERABLE> = Not<ForAllRule<Not<RULE>, ITERABLE>>;
2827

2928
/// Rule where at least one data in the `Vec` satisfies the condition
3029
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>>;

src/rule/collection/for_all.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::collections::{HashMap, HashSet, VecDeque};
22

3-
use crate::rule::{NoSkip, Rule, SkipRule};
3+
use crate::rule::{Iterable, NoSkip, Rule, SkipRule};
44
use crate::Refined;
55

66
/// A type that holds a value satisfying the `ForAllRule`
7-
pub type ForAll<RULE, ITERABLE, ITEM> = Refined<ForAllRule<RULE, ITERABLE, ITEM>>;
7+
pub type ForAll<RULE, ITERABLE> = Refined<ForAllRule<RULE, ITERABLE>>;
88

99
/// A type that holds a Vec value satisfying the `ForAllRule`
1010
pub type ForAllVec<RULE> = Refined<ForAllVecRule<RULE>>;
@@ -22,25 +22,23 @@ pub type ForAllHashMap<K, RULE> = Refined<ForAllHashMapRule<K, RULE>>;
2222
pub type ForAllString<RULE> = Refined<ForAllStringRule<RULE>>;
2323

2424
/// Rule where all the data in the collection satisfies the condition
25-
pub type ForAllRule<RULE, ITERABLE, ITEM> = SkipRule<RULE, ITERABLE, NoSkip<ITEM>>;
25+
pub type ForAllRule<RULE, ITERABLE> =
26+
SkipRule<RULE, ITERABLE, NoSkip<<ITERABLE as Iterable>::Item>>;
2627

2728
/// Rule where all the data in the `Vec` satisfies the condition
28-
pub type ForAllVecRule<RULE> = ForAllRule<RULE, Vec<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
29+
pub type ForAllVecRule<RULE> = ForAllRule<RULE, Vec<<RULE as Rule>::Item>>;
2930

3031
/// Rule where all the data in the `VecDeque` satisfies the condition
31-
pub type ForAllVecDequeRule<RULE> =
32-
ForAllRule<RULE, VecDeque<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
32+
pub type ForAllVecDequeRule<RULE> = ForAllRule<RULE, VecDeque<<RULE as Rule>::Item>>;
3333

3434
/// Rule where all the data in the `HashSet` satisfies the condition
35-
pub type ForAllHashSetRule<RULE> =
36-
ForAllRule<RULE, HashSet<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
35+
pub type ForAllHashSetRule<RULE> = ForAllRule<RULE, HashSet<<RULE as Rule>::Item>>;
3736

3837
/// Rule where all the data in the `HashMap` satisfies the condition
39-
pub type ForAllHashMapRule<K, RULE> =
40-
ForAllRule<RULE, HashMap<K, <RULE as Rule>::Item>, <RULE as Rule>::Item>;
38+
pub type ForAllHashMapRule<K, RULE> = ForAllRule<RULE, HashMap<K, <RULE as Rule>::Item>>;
4139

4240
/// Rule where all the data in the `String` satisfies the condition
43-
pub type ForAllStringRule<RULE> = ForAllRule<RULE, String, char>;
41+
pub type ForAllStringRule<RULE> = ForAllRule<RULE, String>;
4442

4543
#[cfg(test)]
4644
mod tests {

src/rule/collection/reverse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::Refined;
44
use std::marker::PhantomData;
55

66
/// A type that holds a value satisfying the `ReverseRule`
7-
pub type Reverse<'a, RULE> = Refined<ReverseRule<RULE>>;
7+
pub type Reverse<RULE> = Refined<ReverseRule<RULE>>;
88

99
/// Rule where the data in the collection satisfies the condition after reversing
1010
pub struct ReverseRule<RULE>

0 commit comments

Comments
 (0)