File tree 3 files changed +64
-0
lines changed 3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ //! Core types for [`forrustts`](https://docs.rs/forrustts)
2
+
3
+ #![ warn( missing_docs) ]
4
+ #![ warn( rustdoc:: broken_intra_doc_links) ]
5
+ #![ cfg_attr( doc_cfg, feature( doc_cfg) ) ]
6
+
1
7
use thiserror:: Error ;
2
8
3
9
mod position;
@@ -9,8 +15,21 @@ mod time;
9
15
pub use position:: Position ;
10
16
pub use time:: Time ;
11
17
18
+ /// Error type
12
19
#[ derive( Error , Debug ) ]
13
20
pub enum Error {
21
+ /// Invalid [`Position`]
22
+ ///
23
+ /// # Example
24
+ ///
25
+ /// ```
26
+ /// if let Err(e) = forrustts_core::Position::try_from(-1) {
27
+ /// assert!(matches!(e, forrustts_core::Error::PositionError(-1)));
28
+ /// }
29
+ /// # else {
30
+ /// # panic!("should be an Err...");
31
+ /// # }
32
+ /// ```
14
33
#[ error( "{0:?}" ) ]
15
34
PositionError ( i64 ) ,
16
35
}
Original file line number Diff line number Diff line change 4
4
pub struct Position ( i64 ) ;
5
5
6
6
impl Position {
7
+ /// Create a new Position
8
+ ///
9
+ /// # Returns
10
+ ///
11
+ /// * `Some` if `position` is non-negative
12
+ /// * `None` otherwise
13
+ ///
14
+ /// # Examples
15
+ ///
16
+ /// ```
17
+ /// let p = forrustts_core::Position::new(10).unwrap();
18
+ /// assert_eq!(p, 10); // can be compared to i64
19
+ /// # assert_eq!(10, p);
20
+ /// # assert!(p > 0);
21
+ /// # assert!(p != 0);
22
+ /// # assert!(p >= 10);
23
+ /// # assert!(p <= 10);
24
+ /// # assert!(0 < p);
25
+ /// let p2 = forrustts_core::Position::new(11).unwrap();
26
+ /// assert!(p < p2);
27
+ /// assert!(p != p2);
28
+ /// ```
7
29
pub fn new ( position : i64 ) -> Option < Self > {
8
30
if position >= 0 {
9
31
Some ( Self ( position) )
@@ -12,6 +34,21 @@ impl Position {
12
34
}
13
35
}
14
36
37
+ /// Create a new position with a non-negative integer
38
+ ///
39
+ /// # Examples
40
+ ///
41
+ /// ```
42
+ /// let p = forrustts_core::Position::new_valid(10);
43
+ /// ```
44
+ ///
45
+ /// # Panics
46
+ ///
47
+ /// Will panic if `position` < 0.
48
+ ///
49
+ /// ```should_panic
50
+ /// let p = forrustts_core::Position::new(-1).unwrap();
51
+ /// ```
15
52
pub fn new_valid ( position : i64 ) -> Self {
16
53
Self :: new ( position) . unwrap ( )
17
54
}
Original file line number Diff line number Diff line change
1
+ //! Core library prelude
2
+ //!
3
+ //! # Examples
4
+ //!
5
+ //! ```
6
+ //! use forrustts_core::prelude::*;
7
+ //! ```
8
+
1
9
pub use crate :: Position ;
2
10
pub use crate :: Time ;
You can’t perform that action at this time.
0 commit comments