summaryrefslogtreecommitdiff
path: root/src/values/rotation.rs
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2025-02-26 23:24:42 +0100
committerpml68 <contact@pml68.dev>2025-02-26 23:24:42 +0100
commit21941c6de6e0843147ccab7b4045943b7a878442 (patch)
tree535d4ae2a1182565d9db7efd19830f2e8f0ccaea /src/values/rotation.rs
parentfeat: switch to personal `iced` fork, work on version info (diff)
downloadiced-builder-21941c6de6e0843147ccab7b4045943b7a878442.tar.gz
feat: rework `Value` trait, create unit tests for parser implementations
Diffstat (limited to '')
-rw-r--r--src/values/rotation.rs85
1 files changed, 79 insertions, 6 deletions
diff --git a/src/values/rotation.rs b/src/values/rotation.rs
index 2b7a223..da291b2 100644
--- a/src/values/rotation.rs
+++ b/src/values/rotation.rs
@@ -3,14 +3,16 @@ use std::str::FromStr;
use iced::{Radians, Rotation};
-use super::ValueFromStr;
+use super::Value;
-#[derive(Debug, thiserror::Error)]
+#[derive(Debug, thiserror::Error, Clone, PartialEq)]
pub enum ParseRotationError {
#[error("float parsing error: {0}")]
ParseFloatError(ParseFloatError),
#[error("invalid prefix")]
InvalidPrefix,
+ #[error("cannot parse rotation from empty string")]
+ Empty,
}
impl From<ParseFloatError> for ParseRotationError {
@@ -19,13 +21,17 @@ impl From<ParseFloatError> for ParseRotationError {
}
}
-impl ValueFromStr for Rotation {
+impl Value for Rotation {
type Err = ParseRotationError;
- fn value_from_str(s: &str) -> Result<Self, Self::Err> {
- if s.starts_with(|c: char| !c.is_digit(10)) {
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ if s.is_empty() {
+ return Err(ParseRotationError::Empty);
+ }
+
+ if s.starts_with(|c: char| !c.is_ascii_digit()) {
let (prefix, value) = s.split_at(1);
- match prefix {
+ match prefix.to_lowercase().as_str() {
"s" => Ok(Rotation::Solid(Radians(f32::from_str(value)?))),
"f" => Ok(Rotation::Floating(Radians(f32::from_str(value)?))),
_ => Err(ParseRotationError::InvalidPrefix),
@@ -34,4 +40,71 @@ impl ValueFromStr for Rotation {
Ok(Rotation::Floating(Radians(f32::from_str(s)?)))
}
}
+
+ fn to_string(&self) -> String {
+ match self {
+ Self::Floating(value) => format!("f{}", value),
+ Self::Solid(value) => format!("s{}", value),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn can_parse_without_prefix() {
+ assert_eq!(
+ Rotation::from_str("10.5"),
+ Ok(Rotation::Floating(Radians(10.5)))
+ )
+ }
+
+ #[test]
+ fn can_parse_with_s_prefix() {
+ assert_eq!(
+ Rotation::from_str("s12.3"),
+ Ok(Rotation::Solid(Radians(12.3)))
+ )
+ }
+
+ #[test]
+ fn can_parse_with_f_prefix() {
+ assert_eq!(
+ Rotation::from_str("f16.9"),
+ Ok(Rotation::Floating(Radians(16.9)))
+ )
+ }
+
+ #[test]
+ fn can_parse_with_uppercase_prefix() {
+ assert_eq!(
+ Rotation::from_str("S9.4"),
+ Ok(Rotation::Solid(Radians(9.4)))
+ )
+ }
+
+ #[test]
+ fn cant_parse_invalid_prefix() {
+ assert_eq!(
+ Rotation::from_str("a6.0"),
+ Err(ParseRotationError::InvalidPrefix)
+ )
+ }
+
+ #[test]
+ fn cant_parse_invalid_float() {
+ assert_eq!(
+ Rotation::from_str("3.a"),
+ Err(ParseRotationError::ParseFloatError(
+ f32::from_str("3.a").expect_err("")
+ ))
+ )
+ }
+
+ #[test]
+ fn cant_parse_empty_string() {
+ assert_eq!(Rotation::from_str(""), Err(ParseRotationError::Empty))
+ }
}