digit_sequence/lib.rs
1//! This crate revolves around the [DigitSequence] struct,
2//! a sequence of 0-9 [u8] digits, with:
3//!
4//! * conversions from/to integers, numeric sequences and strings
5//!
6//! * different iteration strategies
7//!
8//! * a custom [CrateResult] and a custom [CrateError]
9//!
10//! * optional [serde] I/O
11//!
12//! # Features
13//!
14//! This crate supports the following _optional_ features:
15//!
16//! - `serde`: enables JSON conversions via [serde](https://crates.io/crates/serde)
17
18mod arrays;
19mod integers;
20mod iteration;
21mod result;
22mod slices;
23mod strings;
24mod vecs;
25
26#[cfg(test)]
27pub mod test_utils;
28
29pub use result::*;
30
31/// Immutable sequence of [u8] digits.
32///
33/// # Creation
34///
35/// A digit sequence is usually created via *conversions* -
36/// both fallible and infallible - although a
37/// [constructor](DigitSequence::new) is provided to instantiate
38/// an empty sequence that is also its [Default] value.
39///
40/// ```
41/// use digit_sequence::*;
42///
43/// # fn main() -> GenericResult<()> {
44/// assert_eq!(DigitSequence::new(), []);
45/// assert_eq!(DigitSequence::default(), []);
46///
47/// let sequence: DigitSequence = [3, 8, 7].try_into()?;
48/// assert_eq!(sequence, [3, 8, 7]);
49///
50/// assert_eq!(format!("{:?}", sequence), "DigitSequence([3, 8, 7])");
51/// assert_eq!(sequence.to_string(), "387");
52///
53/// # Ok(())
54/// # }
55/// ```
56///
57/// For details and more code samples, please refer to the
58/// implementations of the [From] and [TryFrom] interfaces.
59///
60/// # Equality
61///
62/// Equality is firstly supported with another [DigitSequence]:
63///
64/// ```
65/// use digit_sequence::*;
66///
67/// # fn main() -> GenericResult<()> {
68///
69/// let source = [1, 3, 5, 7, 9];
70///
71/// let left: DigitSequence = (&source).try_into()?;
72/// let right: DigitSequence = (&source).try_into()?;
73/// assert_eq!(left, right);
74///
75/// let other: DigitSequence = [9u8, 4u8].try_into()?;
76/// assert_ne!(left, other);
77///
78/// # Ok(())
79/// # }
80/// ```
81///
82/// Equality is also supported for operands of other iterable types - such as
83/// [[u8]], &[[u8]] or [Vec]: please, refer to the documentation for the
84/// implementations of this [PartialEq].
85///
86///
87/// # Order
88///
89/// [PartialOrd] is supported for [DigitSequence] - just as expected:
90///
91/// ```
92/// use digit_sequence::*;
93///
94/// # fn main() -> GenericResult<()> {
95/// let short_bigger: DigitSequence = [9].try_into()?;
96/// let short_smaller: DigitSequence = [4].try_into()?;
97///
98/// let longest: DigitSequence = [9, 7].try_into()?;
99///
100/// assert!(short_bigger > short_smaller);
101/// assert!(short_bigger < longest);
102///
103/// # Ok(())
104/// # }
105/// ```
106///
107/// # Serialization
108///
109/// **REQUIRES FEATURE**: `serde`.
110///
111/// When the `serde` feature is enabled for this crate, [DigitSequence] implements the [serde::Serialize] and [serde::Deserialize] traits.
112///
113/// ```
114/// #[cfg(feature = "my_feature")]
115/// {
116/// use digit_sequence::*;
117/// use serde_json::{to_string, from_str};
118///
119/// # fn main() -> GenericResult<()> {
120/// let original: DigitSequence = 9786u16.into();
121/// let expected_json = "[9,7,8,6]";
122///
123/// let json = to_string(&original)?;
124/// assert_eq!(json, expected_json);
125///
126/// let deserialized: DigitSequence = from_str(expected_json)?;
127/// assert_eq!(deserialized, original);
128///
129/// # Ok(())
130/// # }
131/// }
132/// ```
133#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
134#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
135pub struct DigitSequence(pub(crate) Vec<u8>);
136
137impl DigitSequence {
138 /// Creates an empty sequence.
139 ///
140 /// ```
141 /// use digit_sequence::DigitSequence;
142 ///
143 /// let sequence = DigitSequence::new();
144 ///
145 /// assert_eq!(sequence.iter().len(), 0);
146 /// ```
147 pub fn new() -> DigitSequence {
148 DigitSequence(vec![])
149 }
150
151 /// Tells whether the sequence is empty.
152 ///
153 /// ```
154 /// use digit_sequence::*;
155 ///
156 /// let empty = DigitSequence::new();
157 /// assert!(empty.is_empty());
158 ///
159 /// let non_empty: DigitSequence = 84u8.into();
160 /// assert!(!non_empty.is_empty());
161 /// ```
162 pub fn is_empty(&self) -> bool {
163 self.0.is_empty()
164 }
165}