2024-07-04 14:38:35 +03:00
|
|
|
#![allow(clippy::comparison_chain)]
|
|
|
|
|
2019-05-22 14:48:32 +03:00
|
|
|
#[derive(PartialEq, Debug)]
|
2015-09-19 04:27:25 +03:00
|
|
|
enum CreationError {
|
|
|
|
Negative,
|
|
|
|
Zero,
|
|
|
|
}
|
|
|
|
|
2024-06-26 16:54:18 +03:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
struct PositiveNonzeroInteger(u64);
|
|
|
|
|
2015-09-19 04:27:25 +03:00
|
|
|
impl PositiveNonzeroInteger {
|
2024-06-26 16:54:18 +03:00
|
|
|
fn new(value: i64) -> Result<Self, CreationError> {
|
|
|
|
// TODO: This function shouldn't always return an `Ok`.
|
|
|
|
Ok(Self(value as u64))
|
2015-09-19 04:27:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 23:46:21 +03:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2024-04-18 00:34:27 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_creation() {
|
|
|
|
assert_eq!(
|
2024-06-26 16:54:18 +03:00
|
|
|
PositiveNonzeroInteger::new(10),
|
|
|
|
Ok(PositiveNonzeroInteger(10)),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
PositiveNonzeroInteger::new(-10),
|
2024-04-18 00:34:27 +03:00
|
|
|
Err(CreationError::Negative),
|
|
|
|
);
|
2024-06-26 16:54:18 +03:00
|
|
|
assert_eq!(PositiveNonzeroInteger::new(0), Err(CreationError::Zero));
|
2024-04-18 00:34:27 +03:00
|
|
|
}
|
2015-09-19 04:27:25 +03:00
|
|
|
}
|