| 1 |
#![cfg_attr(not(feature = "std"), no_std)] |
| 2 |
#![allow(dead_code)] // some code is tested for type checking only |
| 3 |
|
| 4 |
#[cfg(not(feature = "std"))] |
| 5 |
extern crate alloc; |
| 6 |
|
| 7 |
#[cfg(not(feature = "std"))] |
| 8 |
use alloc::string::ToString; |
| 9 |
use core::{convert::Infallible, marker::PhantomData}; |
| 10 |
|
| 11 |
use derive_more::with_trait::FromStr; |
| 12 |
|
| 13 |
trait Some { |
| 14 |
type Assoc; |
| 15 |
} |
| 16 |
|
| 17 |
impl<T> Some for T { |
| 18 |
type Assoc = i32; |
| 19 |
} |
| 20 |
|
| 21 |
mod structs { |
| 22 |
use super::*; |
| 23 |
|
| 24 |
mod forward { |
| 25 |
use super::*; |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn unnamed() { |
| 29 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 30 |
struct Int(i32); |
| 31 |
|
| 32 |
assert_eq!("3".parse::<Int>().unwrap(), Int(3)); |
| 33 |
assert_eq!("0".parse::<Int>().unwrap(), Int(0)); |
| 34 |
assert_eq!("2147483647".parse::<Int>().unwrap(), Int(i32::MAX)); |
| 35 |
assert_eq!("-2147483648".parse::<Int>().unwrap(), Int(i32::MIN)); |
| 36 |
|
| 37 |
assert_eq!( |
| 38 |
"2147483648".parse::<Int>().unwrap_err().to_string(), |
| 39 |
"number too large to fit in target type", |
| 40 |
); |
| 41 |
assert_eq!( |
| 42 |
"-2147483649".parse::<Int>().unwrap_err().to_string(), |
| 43 |
"number too small to fit in target type", |
| 44 |
); |
| 45 |
assert_eq!( |
| 46 |
"wow".parse::<Int>().unwrap_err().to_string(), |
| 47 |
"invalid digit found in string", |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn named() { |
| 53 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 54 |
struct Point1D { |
| 55 |
x: i32, |
| 56 |
} |
| 57 |
|
| 58 |
assert_eq!("3".parse::<Point1D>().unwrap(), Point1D { x: 3 }); |
| 59 |
assert_eq!("0".parse::<Point1D>().unwrap(), Point1D { x: 0 }); |
| 60 |
assert_eq!( |
| 61 |
"2147483647".parse::<Point1D>().unwrap(), |
| 62 |
Point1D { x: i32::MAX }, |
| 63 |
); |
| 64 |
assert_eq!( |
| 65 |
"-2147483648".parse::<Point1D>().unwrap(), |
| 66 |
Point1D { x: i32::MIN }, |
| 67 |
); |
| 68 |
|
| 69 |
assert_eq!( |
| 70 |
"2147483648".parse::<Point1D>().unwrap_err().to_string(), |
| 71 |
"number too large to fit in target type", |
| 72 |
); |
| 73 |
assert_eq!( |
| 74 |
"-2147483649".parse::<Point1D>().unwrap_err().to_string(), |
| 75 |
"number too small to fit in target type", |
| 76 |
); |
| 77 |
assert_eq!( |
| 78 |
"wow".parse::<Point1D>().unwrap_err().to_string(), |
| 79 |
"invalid digit found in string", |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
mod generic { |
| 84 |
use super::*; |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn unnamed() { |
| 88 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 89 |
struct Int<I>(I); |
| 90 |
|
| 91 |
assert_eq!("3".parse::<Int<i32>>().unwrap(), Int(3)); |
| 92 |
assert_eq!("0".parse::<Int<i32>>().unwrap(), Int(0)); |
| 93 |
assert_eq!("2147483647".parse::<Int<i32>>().unwrap(), Int(i32::MAX)); |
| 94 |
assert_eq!("-2147483648".parse::<Int<i32>>().unwrap(), Int(i32::MIN)); |
| 95 |
|
| 96 |
assert_eq!( |
| 97 |
"2147483648".parse::<Int<i32>>().unwrap_err().to_string(), |
| 98 |
"number too large to fit in target type", |
| 99 |
); |
| 100 |
assert_eq!( |
| 101 |
"-2147483649".parse::<Int<i32>>().unwrap_err().to_string(), |
| 102 |
"number too small to fit in target type", |
| 103 |
); |
| 104 |
assert_eq!( |
| 105 |
"wow".parse::<Int<i32>>().unwrap_err().to_string(), |
| 106 |
"invalid digit found in string", |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn named() { |
| 112 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 113 |
struct Point1D<I> { |
| 114 |
x: I, |
| 115 |
} |
| 116 |
|
| 117 |
assert_eq!("3".parse::<Point1D<i32>>().unwrap(), Point1D { x: 3 }); |
| 118 |
assert_eq!("0".parse::<Point1D<i32>>().unwrap(), Point1D { x: 0 }); |
| 119 |
assert_eq!( |
| 120 |
"2147483647".parse::<Point1D<i32>>().unwrap(), |
| 121 |
Point1D { x: i32::MAX }, |
| 122 |
); |
| 123 |
assert_eq!( |
| 124 |
"-2147483648".parse::<Point1D<i32>>().unwrap(), |
| 125 |
Point1D { x: i32::MIN }, |
| 126 |
); |
| 127 |
|
| 128 |
assert_eq!( |
| 129 |
"2147483648" |
| 130 |
.parse::<Point1D<i32>>() |
| 131 |
.unwrap_err() |
| 132 |
.to_string(), |
| 133 |
"number too large to fit in target type", |
| 134 |
); |
| 135 |
assert_eq!( |
| 136 |
"-2147483649" |
| 137 |
.parse::<Point1D<i32>>() |
| 138 |
.unwrap_err() |
| 139 |
.to_string(), |
| 140 |
"number too small to fit in target type", |
| 141 |
); |
| 142 |
assert_eq!( |
| 143 |
"wow".parse::<Point1D<i32>>().unwrap_err().to_string(), |
| 144 |
"invalid digit found in string", |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn assoc() { |
| 150 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 151 |
struct Point1D<I: Some> { |
| 152 |
x: I::Assoc, |
| 153 |
} |
| 154 |
|
| 155 |
assert_eq!("3".parse::<Point1D<()>>().unwrap(), Point1D { x: 3 }); |
| 156 |
assert_eq!("0".parse::<Point1D<()>>().unwrap(), Point1D { x: 0 }); |
| 157 |
assert_eq!( |
| 158 |
"2147483647".parse::<Point1D<()>>().unwrap(), |
| 159 |
Point1D { x: i32::MAX }, |
| 160 |
); |
| 161 |
assert_eq!( |
| 162 |
"-2147483648".parse::<Point1D<()>>().unwrap(), |
| 163 |
Point1D { x: i32::MIN }, |
| 164 |
); |
| 165 |
|
| 166 |
assert_eq!( |
| 167 |
"2147483648".parse::<Point1D<()>>().unwrap_err().to_string(), |
| 168 |
"number too large to fit in target type", |
| 169 |
); |
| 170 |
assert_eq!( |
| 171 |
"-2147483649" |
| 172 |
.parse::<Point1D<()>>() |
| 173 |
.unwrap_err() |
| 174 |
.to_string(), |
| 175 |
"number too small to fit in target type", |
| 176 |
); |
| 177 |
assert_eq!( |
| 178 |
"wow".parse::<Point1D<()>>().unwrap_err().to_string(), |
| 179 |
"invalid digit found in string", |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn lifetime() { |
| 185 |
#[derive(Debug, Eq, PartialEq)] |
| 186 |
struct Empty<'a>(PhantomData<&'a i32>); |
| 187 |
|
| 188 |
impl FromStr for Empty<'_> { |
| 189 |
type Err = Infallible; |
| 190 |
|
| 191 |
fn from_str(_: &str) -> Result<Self, Self::Err> { |
| 192 |
Ok(Self(PhantomData)) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 197 |
struct Wrapper<'a>(Empty<'a>); |
| 198 |
|
| 199 |
assert_eq!( |
| 200 |
"3".parse::<Wrapper>().unwrap(), |
| 201 |
Wrapper(Empty(PhantomData)), |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn const_param() { |
| 207 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 208 |
struct Int<const N: usize>(i32); |
| 209 |
|
| 210 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 211 |
struct Point1D<const N: usize> { |
| 212 |
x: Int<N>, |
| 213 |
} |
| 214 |
|
| 215 |
assert_eq!("3".parse::<Int<1>>().unwrap(), Int(3)); |
| 216 |
assert_eq!("0".parse::<Int<1>>().unwrap(), Int(0)); |
| 217 |
assert_eq!("2147483647".parse::<Int<1>>().unwrap(), Int(i32::MAX)); |
| 218 |
assert_eq!("-2147483648".parse::<Int<1>>().unwrap(), Int(i32::MIN)); |
| 219 |
|
| 220 |
assert_eq!( |
| 221 |
"2147483648".parse::<Int<1>>().unwrap_err().to_string(), |
| 222 |
"number too large to fit in target type", |
| 223 |
); |
| 224 |
assert_eq!( |
| 225 |
"-2147483649".parse::<Int<1>>().unwrap_err().to_string(), |
| 226 |
"number too small to fit in target type", |
| 227 |
); |
| 228 |
assert_eq!( |
| 229 |
"wow".parse::<Int<1>>().unwrap_err().to_string(), |
| 230 |
"invalid digit found in string", |
| 231 |
); |
| 232 |
|
| 233 |
assert_eq!("3".parse::<Point1D<3>>().unwrap(), Point1D { x: Int(3) }); |
| 234 |
assert_eq!("0".parse::<Point1D<3>>().unwrap(), Point1D { x: Int(0) }); |
| 235 |
assert_eq!( |
| 236 |
"2147483647".parse::<Point1D<3>>().unwrap(), |
| 237 |
Point1D { x: Int(i32::MAX) }, |
| 238 |
); |
| 239 |
assert_eq!( |
| 240 |
"-2147483648".parse::<Point1D<3>>().unwrap(), |
| 241 |
Point1D { x: Int(i32::MIN) }, |
| 242 |
); |
| 243 |
|
| 244 |
assert_eq!( |
| 245 |
"2147483648".parse::<Point1D<3>>().unwrap_err().to_string(), |
| 246 |
"number too large to fit in target type", |
| 247 |
); |
| 248 |
assert_eq!( |
| 249 |
"-2147483649".parse::<Point1D<3>>().unwrap_err().to_string(), |
| 250 |
"number too small to fit in target type", |
| 251 |
); |
| 252 |
assert_eq!( |
| 253 |
"wow".parse::<Point1D<3>>().unwrap_err().to_string(), |
| 254 |
"invalid digit found in string", |
| 255 |
); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
mod custom_error { |
| 260 |
use core::num::ParseIntError; |
| 261 |
|
| 262 |
use super::*; |
| 263 |
|
| 264 |
#[derive(Debug, PartialEq)] |
| 265 |
struct CustomError(ParseIntError); |
| 266 |
|
| 267 |
impl CustomError { |
| 268 |
fn new(err: ParseIntError) -> Self { |
| 269 |
Self(err) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
impl From<ParseIntError> for CustomError { |
| 274 |
fn from(value: ParseIntError) -> Self { |
| 275 |
Self(value) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
fn custom_error_fn() -> fn(ParseIntError) -> CustomError { |
| 280 |
CustomError::new |
| 281 |
} |
| 282 |
|
| 283 |
mod unnamed { |
| 284 |
use super::*; |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn only_ty() { |
| 288 |
#[derive(Debug, FromStr)] |
| 289 |
#[from_str(error(CustomError))] |
| 290 |
struct MyInt(i32); |
| 291 |
|
| 292 |
assert_eq!( |
| 293 |
"foo".parse::<MyInt>().unwrap_err(), |
| 294 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn with_fn() { |
| 300 |
#[derive(Debug, FromStr)] |
| 301 |
#[from_str(error(CustomError, CustomError::new))] |
| 302 |
struct MyInt(i32); |
| 303 |
|
| 304 |
assert_eq!( |
| 305 |
"foo".parse::<MyInt>().unwrap_err(), |
| 306 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
#[expect(clippy::redundant_closure, reason = "intended for testing")] |
| 311 |
#[test] |
| 312 |
fn with_closure() { |
| 313 |
#[derive(Debug, FromStr)] |
| 314 |
#[from_str(error(CustomError, |e| CustomError::new(e)))] |
| 315 |
struct MyInt(i32); |
| 316 |
|
| 317 |
assert_eq!( |
| 318 |
"foo".parse::<MyInt>().unwrap_err(), |
| 319 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 320 |
); |
| 321 |
} |
| 322 |
|
| 323 |
#[test] |
| 324 |
fn with_fn_call() { |
| 325 |
#[derive(Debug, FromStr)] |
| 326 |
#[from_str(error(CustomError, custom_error_fn()))] |
| 327 |
struct MyInt(i32); |
| 328 |
|
| 329 |
assert_eq!( |
| 330 |
"foo".parse::<MyInt>().unwrap_err(), |
| 331 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 332 |
); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
mod named { |
| 337 |
use super::*; |
| 338 |
|
| 339 |
#[test] |
| 340 |
fn only_ty() { |
| 341 |
#[derive(Debug, FromStr)] |
| 342 |
#[from_str(error(CustomError))] |
| 343 |
struct MyInt { |
| 344 |
value: i32, |
| 345 |
} |
| 346 |
|
| 347 |
assert_eq!( |
| 348 |
"foo".parse::<MyInt>().unwrap_err(), |
| 349 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 350 |
); |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn with_fn() { |
| 355 |
#[derive(Debug, FromStr)] |
| 356 |
#[from_str(error(CustomError, CustomError::new))] |
| 357 |
struct MyInt { |
| 358 |
value: i32, |
| 359 |
} |
| 360 |
|
| 361 |
assert_eq!( |
| 362 |
"foo".parse::<MyInt>().unwrap_err(), |
| 363 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
#[expect(clippy::redundant_closure, reason = "intended for testing")] |
| 368 |
#[test] |
| 369 |
fn with_closure() { |
| 370 |
#[derive(Debug, FromStr)] |
| 371 |
#[from_str(error(CustomError, |e| CustomError::new(e)))] |
| 372 |
struct MyInt { |
| 373 |
value: i32, |
| 374 |
} |
| 375 |
|
| 376 |
assert_eq!( |
| 377 |
"foo".parse::<MyInt>().unwrap_err(), |
| 378 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 379 |
); |
| 380 |
} |
| 381 |
|
| 382 |
#[test] |
| 383 |
fn with_fn_call() { |
| 384 |
#[derive(Debug, FromStr)] |
| 385 |
#[from_str(error(CustomError, custom_error_fn()))] |
| 386 |
struct MyInt { |
| 387 |
value: i32, |
| 388 |
} |
| 389 |
|
| 390 |
assert_eq!( |
| 391 |
"foo".parse::<MyInt>().unwrap_err(), |
| 392 |
CustomError("foo".parse::<i32>().unwrap_err()), |
| 393 |
); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
mod flat { |
| 400 |
use super::*; |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn unit() { |
| 404 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 405 |
struct Foo; |
| 406 |
|
| 407 |
assert_eq!("Foo".parse::<Foo>().unwrap(), Foo); |
| 408 |
} |
| 409 |
|
| 410 |
#[test] |
| 411 |
fn empty_tuple() { |
| 412 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 413 |
struct Bar(); |
| 414 |
|
| 415 |
assert_eq!("Bar".parse::<Bar>().unwrap(), Bar()); |
| 416 |
} |
| 417 |
|
| 418 |
#[test] |
| 419 |
fn empty_struct() { |
| 420 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 421 |
struct Baz {} |
| 422 |
|
| 423 |
assert_eq!("Baz".parse::<Baz>().unwrap(), Baz {}); |
| 424 |
} |
| 425 |
|
| 426 |
#[test] |
| 427 |
fn case_insensitive() { |
| 428 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 429 |
struct Foo; |
| 430 |
|
| 431 |
assert_eq!("Foo".parse::<Foo>().unwrap(), Foo); |
| 432 |
assert_eq!("FOO".parse::<Foo>().unwrap(), Foo); |
| 433 |
assert_eq!("foo".parse::<Foo>().unwrap(), Foo); |
| 434 |
|
| 435 |
assert_eq!( |
| 436 |
"baz".parse::<Foo>().unwrap_err().to_string(), |
| 437 |
"Invalid `Foo` string representation", |
| 438 |
); |
| 439 |
assert_eq!( |
| 440 |
"other".parse::<Foo>().unwrap_err().to_string(), |
| 441 |
"Invalid `Foo` string representation", |
| 442 |
); |
| 443 |
} |
| 444 |
|
| 445 |
mod rename_all { |
| 446 |
use super::*; |
| 447 |
|
| 448 |
#[test] |
| 449 |
fn case_sensitive() { |
| 450 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 451 |
#[from_str(rename_all = "PascalCase")] |
| 452 |
struct Foo; |
| 453 |
|
| 454 |
assert_eq!("Foo".parse::<Foo>().unwrap(), Foo); |
| 455 |
|
| 456 |
assert_eq!( |
| 457 |
"foo".parse::<Foo>().unwrap_err().to_string(), |
| 458 |
"Invalid `Foo` string representation", |
| 459 |
); |
| 460 |
assert_eq!( |
| 461 |
"FOO".parse::<Foo>().unwrap_err().to_string(), |
| 462 |
"Invalid `Foo` string representation", |
| 463 |
); |
| 464 |
assert_eq!( |
| 465 |
"FoO".parse::<Foo>().unwrap_err().to_string(), |
| 466 |
"Invalid `Foo` string representation", |
| 467 |
); |
| 468 |
assert_eq!( |
| 469 |
"other".parse::<Foo>().unwrap_err().to_string(), |
| 470 |
"Invalid `Foo` string representation", |
| 471 |
); |
| 472 |
} |
| 473 |
|
| 474 |
mod casing { |
| 475 |
use super::*; |
| 476 |
|
| 477 |
macro_rules! casing_test { |
| 478 |
($name:ident, $casing:literal, $input:literal) => { |
| 479 |
mod $name { |
| 480 |
use super::*; |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn top_level() { |
| 484 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 485 |
#[from_str(rename_all = $casing)] |
| 486 |
struct FooBar; |
| 487 |
|
| 488 |
assert_eq!($input.parse::<FooBar>().unwrap(), FooBar); |
| 489 |
} |
| 490 |
} |
| 491 |
}; |
| 492 |
} |
| 493 |
|
| 494 |
casing_test!(lower_case, "lowercase", "foobar"); |
| 495 |
casing_test!(upper_case, "UPPERCASE", "FOOBAR"); |
| 496 |
casing_test!(pascal_case, "PascalCase", "FooBar"); |
| 497 |
casing_test!(camel_case, "camelCase", "fooBar"); |
| 498 |
casing_test!(snake_case, "snake_case", "foo_bar"); |
| 499 |
casing_test!(screaming_snake_case, "SCREAMING_SNAKE_CASE", "FOO_BAR"); |
| 500 |
casing_test!(kebab_case, "kebab-case", "foo-bar"); |
| 501 |
casing_test!(screaming_kebab_case, "SCREAMING-KEBAB-CASE", "FOO-BAR"); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
mod custom_error { |
| 506 |
use derive_more::FromStrError; |
| 507 |
|
| 508 |
use super::*; |
| 509 |
|
| 510 |
#[derive(Debug)] |
| 511 |
struct CustomError(FromStrError); |
| 512 |
|
| 513 |
impl CustomError { |
| 514 |
fn new(err: FromStrError) -> Self { |
| 515 |
Self(err) |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
impl From<FromStrError> for CustomError { |
| 520 |
fn from(value: FromStrError) -> Self { |
| 521 |
Self(value) |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
fn custom_error_fn() -> fn(FromStrError) -> CustomError { |
| 526 |
CustomError::new |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn only_ty() { |
| 531 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 532 |
#[from_str(error(CustomError))] |
| 533 |
struct Foo; |
| 534 |
|
| 535 |
assert_eq!( |
| 536 |
match "bar".parse::<Foo>().unwrap_err() { |
| 537 |
CustomError(e) => e.to_string(), |
| 538 |
}, |
| 539 |
"Invalid `Foo` string representation", |
| 540 |
); |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn with_fn() { |
| 545 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 546 |
#[from_str(error(CustomError, CustomError::new))] |
| 547 |
struct Foo; |
| 548 |
|
| 549 |
assert_eq!( |
| 550 |
match "bar".parse::<Foo>().unwrap_err() { |
| 551 |
CustomError(e) => e.to_string(), |
| 552 |
}, |
| 553 |
"Invalid `Foo` string representation", |
| 554 |
); |
| 555 |
} |
| 556 |
|
| 557 |
#[expect(clippy::redundant_closure, reason = "intended for testing")] |
| 558 |
#[test] |
| 559 |
fn with_closure() { |
| 560 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 561 |
#[from_str(error(CustomError, |e| CustomError::new(e)))] |
| 562 |
struct Foo; |
| 563 |
|
| 564 |
assert_eq!( |
| 565 |
match "bar".parse::<Foo>().unwrap_err() { |
| 566 |
CustomError(e) => e.to_string(), |
| 567 |
}, |
| 568 |
"Invalid `Foo` string representation", |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
#[test] |
| 573 |
fn with_fn_call() { |
| 574 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 575 |
#[from_str(error(CustomError, custom_error_fn()))] |
| 576 |
struct Foo; |
| 577 |
|
| 578 |
assert_eq!( |
| 579 |
match "bar".parse::<Foo>().unwrap_err() { |
| 580 |
CustomError(e) => e.to_string(), |
| 581 |
}, |
| 582 |
"Invalid `Foo` string representation", |
| 583 |
); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
mod enums { |
| 590 |
use super::*; |
| 591 |
|
| 592 |
mod flat { |
| 593 |
use super::*; |
| 594 |
|
| 595 |
/// Assertion that `FromStr` does not trigger an ambiguous associated item error for `Err`. |
| 596 |
#[derive(FromStr)] |
| 597 |
enum EnumWithErr { |
| 598 |
Err, |
| 599 |
} |
| 600 |
|
| 601 |
#[test] |
| 602 |
fn empty() { |
| 603 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 604 |
enum Enum {} |
| 605 |
|
| 606 |
assert_eq!( |
| 607 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 608 |
"Invalid `Enum` string representation", |
| 609 |
); |
| 610 |
} |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn unit() { |
| 614 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 615 |
enum Enum { |
| 616 |
Foo, |
| 617 |
} |
| 618 |
|
| 619 |
assert_eq!("Foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 620 |
assert_eq!("FOO".parse::<Enum>().unwrap(), Enum::Foo); |
| 621 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 622 |
|
| 623 |
assert_eq!( |
| 624 |
"baz".parse::<Enum>().unwrap_err().to_string(), |
| 625 |
"Invalid `Enum` string representation", |
| 626 |
); |
| 627 |
assert_eq!( |
| 628 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 629 |
"Invalid `Enum` string representation", |
| 630 |
); |
| 631 |
} |
| 632 |
|
| 633 |
#[test] |
| 634 |
fn empty_tuple() { |
| 635 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 636 |
enum Enum { |
| 637 |
Foo(), |
| 638 |
} |
| 639 |
|
| 640 |
assert_eq!("Foo".parse::<Enum>().unwrap(), Enum::Foo()); |
| 641 |
assert_eq!("FOO".parse::<Enum>().unwrap(), Enum::Foo()); |
| 642 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo()); |
| 643 |
|
| 644 |
assert_eq!( |
| 645 |
"baz".parse::<Enum>().unwrap_err().to_string(), |
| 646 |
"Invalid `Enum` string representation", |
| 647 |
); |
| 648 |
assert_eq!( |
| 649 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 650 |
"Invalid `Enum` string representation", |
| 651 |
); |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn empty_struct() { |
| 656 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 657 |
enum Enum { |
| 658 |
Foo {}, |
| 659 |
} |
| 660 |
|
| 661 |
assert_eq!("Foo".parse::<Enum>().unwrap(), Enum::Foo {}); |
| 662 |
assert_eq!("FOO".parse::<Enum>().unwrap(), Enum::Foo {}); |
| 663 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo {}); |
| 664 |
|
| 665 |
assert_eq!( |
| 666 |
"baz".parse::<Enum>().unwrap_err().to_string(), |
| 667 |
"Invalid `Enum` string representation", |
| 668 |
); |
| 669 |
assert_eq!( |
| 670 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 671 |
"Invalid `Enum` string representation", |
| 672 |
); |
| 673 |
} |
| 674 |
|
| 675 |
#[test] |
| 676 |
fn case_insensitive() { |
| 677 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 678 |
enum Enum { |
| 679 |
Foo, |
| 680 |
Bar, |
| 681 |
} |
| 682 |
|
| 683 |
assert_eq!("Foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 684 |
assert_eq!("FOO".parse::<Enum>().unwrap(), Enum::Foo); |
| 685 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 686 |
|
| 687 |
assert_eq!("Bar".parse::<Enum>().unwrap(), Enum::Bar); |
| 688 |
assert_eq!("baR".parse::<Enum>().unwrap(), Enum::Bar); |
| 689 |
assert_eq!("bar".parse::<Enum>().unwrap(), Enum::Bar); |
| 690 |
assert_eq!("BAR".parse::<Enum>().unwrap(), Enum::Bar); |
| 691 |
|
| 692 |
assert_eq!( |
| 693 |
"baz".parse::<Enum>().unwrap_err().to_string(), |
| 694 |
"Invalid `Enum` string representation", |
| 695 |
); |
| 696 |
assert_eq!( |
| 697 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 698 |
"Invalid `Enum` string representation", |
| 699 |
); |
| 700 |
} |
| 701 |
|
| 702 |
#[test] |
| 703 |
fn case_sensitive() { |
| 704 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 705 |
enum Enum { |
| 706 |
Baz, |
| 707 |
BaZ, |
| 708 |
Bar, |
| 709 |
} |
| 710 |
|
| 711 |
assert_eq!("Baz".parse::<Enum>().unwrap(), Enum::Baz); |
| 712 |
assert_eq!("BaZ".parse::<Enum>().unwrap(), Enum::BaZ); |
| 713 |
|
| 714 |
assert_eq!("Bar".parse::<Enum>().unwrap(), Enum::Bar); |
| 715 |
assert_eq!("baR".parse::<Enum>().unwrap(), Enum::Bar); |
| 716 |
assert_eq!("bar".parse::<Enum>().unwrap(), Enum::Bar); |
| 717 |
assert_eq!("BAR".parse::<Enum>().unwrap(), Enum::Bar); |
| 718 |
|
| 719 |
assert_eq!( |
| 720 |
"baz".parse::<Enum>().unwrap_err().to_string(), |
| 721 |
"Invalid `Enum` string representation", |
| 722 |
); |
| 723 |
assert_eq!( |
| 724 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 725 |
"Invalid `Enum` string representation", |
| 726 |
); |
| 727 |
} |
| 728 |
|
| 729 |
mod rename_all { |
| 730 |
use super::*; |
| 731 |
|
| 732 |
#[test] |
| 733 |
fn variant_case_sensitive() { |
| 734 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 735 |
enum Enum { |
| 736 |
Foo, |
| 737 |
#[from_str(rename_all = "UPPERCASE")] |
| 738 |
Bar, |
| 739 |
} |
| 740 |
|
| 741 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 742 |
assert_eq!("Foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 743 |
assert_eq!("FOO".parse::<Enum>().unwrap(), Enum::Foo); |
| 744 |
|
| 745 |
assert_eq!("BAR".parse::<Enum>().unwrap(), Enum::Bar); |
| 746 |
|
| 747 |
assert_eq!( |
| 748 |
"Bar".parse::<Enum>().unwrap_err().to_string(), |
| 749 |
"Invalid `Enum` string representation", |
| 750 |
); |
| 751 |
assert_eq!( |
| 752 |
"bar".parse::<Enum>().unwrap_err().to_string(), |
| 753 |
"Invalid `Enum` string representation", |
| 754 |
); |
| 755 |
assert_eq!( |
| 756 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 757 |
"Invalid `Enum` string representation", |
| 758 |
); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn similar_variant_both_case_sensitive() { |
| 763 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 764 |
enum Enum { |
| 765 |
Xyz, |
| 766 |
#[from_str(rename_all = "kebab-case")] |
| 767 |
XyZ, |
| 768 |
} |
| 769 |
|
| 770 |
assert_eq!("Xyz".parse::<Enum>().unwrap(), Enum::Xyz); |
| 771 |
assert_eq!("xy-z".parse::<Enum>().unwrap(), Enum::XyZ); |
| 772 |
|
| 773 |
assert_eq!( |
| 774 |
"xyz".parse::<Enum>().unwrap_err().to_string(), |
| 775 |
"Invalid `Enum` string representation", |
| 776 |
); |
| 777 |
assert_eq!( |
| 778 |
"XyZ".parse::<Enum>().unwrap_err().to_string(), |
| 779 |
"Invalid `Enum` string representation", |
| 780 |
); |
| 781 |
assert_eq!( |
| 782 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 783 |
"Invalid `Enum` string representation", |
| 784 |
); |
| 785 |
} |
| 786 |
|
| 787 |
#[test] |
| 788 |
fn similar_string_both_case_sensitive() { |
| 789 |
#[allow(non_camel_case_types)] |
| 790 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 791 |
enum Enum { |
| 792 |
Abc, |
| 793 |
#[from_str(rename_all = "lowercase")] |
| 794 |
AB_C, |
| 795 |
} |
| 796 |
|
| 797 |
assert_eq!("Abc".parse::<Enum>().unwrap(), Enum::Abc); |
| 798 |
assert_eq!("abc".parse::<Enum>().unwrap(), Enum::AB_C); |
| 799 |
|
| 800 |
assert_eq!( |
| 801 |
"ABC".parse::<Enum>().unwrap_err().to_string(), |
| 802 |
"Invalid `Enum` string representation", |
| 803 |
); |
| 804 |
assert_eq!( |
| 805 |
"AB_C".parse::<Enum>().unwrap_err().to_string(), |
| 806 |
"Invalid `Enum` string representation", |
| 807 |
); |
| 808 |
assert_eq!( |
| 809 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 810 |
"Invalid `Enum` string representation", |
| 811 |
); |
| 812 |
} |
| 813 |
|
| 814 |
#[test] |
| 815 |
fn variant_overrides_top_level() { |
| 816 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 817 |
#[from_str(rename_all = "snake_case")] |
| 818 |
enum Enum { |
| 819 |
Foo, |
| 820 |
#[from_str(rename_all = "UPPERCASE")] |
| 821 |
Bar, |
| 822 |
Baz, |
| 823 |
BaZ, |
| 824 |
} |
| 825 |
|
| 826 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 827 |
assert_eq!("BAR".parse::<Enum>().unwrap(), Enum::Bar); |
| 828 |
assert_eq!("baz".parse::<Enum>().unwrap(), Enum::Baz); |
| 829 |
assert_eq!("ba_z".parse::<Enum>().unwrap(), Enum::BaZ); |
| 830 |
|
| 831 |
assert_eq!( |
| 832 |
"Foo".parse::<Enum>().unwrap_err().to_string(), |
| 833 |
"Invalid `Enum` string representation", |
| 834 |
); |
| 835 |
assert_eq!( |
| 836 |
"Bar".parse::<Enum>().unwrap_err().to_string(), |
| 837 |
"Invalid `Enum` string representation", |
| 838 |
); |
| 839 |
assert_eq!( |
| 840 |
"bar".parse::<Enum>().unwrap_err().to_string(), |
| 841 |
"Invalid `Enum` string representation", |
| 842 |
); |
| 843 |
assert_eq!( |
| 844 |
"Baz".parse::<Enum>().unwrap_err().to_string(), |
| 845 |
"Invalid `Enum` string representation", |
| 846 |
); |
| 847 |
assert_eq!( |
| 848 |
"BaZ".parse::<Enum>().unwrap_err().to_string(), |
| 849 |
"Invalid `Enum` string representation", |
| 850 |
); |
| 851 |
assert_eq!( |
| 852 |
"other".parse::<Enum>().unwrap_err().to_string(), |
| 853 |
"Invalid `Enum` string representation", |
| 854 |
); |
| 855 |
} |
| 856 |
|
| 857 |
mod casing { |
| 858 |
use super::*; |
| 859 |
|
| 860 |
macro_rules! casing_test { |
| 861 |
($name:ident, $casing:literal, $VariantOne:literal, $Two:literal) => { |
| 862 |
mod $name { |
| 863 |
use super::*; |
| 864 |
|
| 865 |
#[test] |
| 866 |
fn top_level() { |
| 867 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 868 |
#[from_str(rename_all = $casing)] |
| 869 |
enum Enum { |
| 870 |
VariantOne, |
| 871 |
Two, |
| 872 |
} |
| 873 |
|
| 874 |
assert_eq!( |
| 875 |
$VariantOne.parse::<Enum>().unwrap(), |
| 876 |
Enum::VariantOne, |
| 877 |
); |
| 878 |
assert_eq!($Two.parse::<Enum>().unwrap(), Enum::Two); |
| 879 |
} |
| 880 |
|
| 881 |
#[test] |
| 882 |
fn variant_level() { |
| 883 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 884 |
#[from_str(rename_all = "lowercase")] // ignored |
| 885 |
enum Enum { |
| 886 |
#[from_str(rename_all = $casing)] |
| 887 |
VariantOne, |
| 888 |
#[from_str(rename_all = $casing)] |
| 889 |
Two, |
| 890 |
} |
| 891 |
|
| 892 |
assert_eq!( |
| 893 |
$VariantOne.parse::<Enum>().unwrap(), |
| 894 |
Enum::VariantOne, |
| 895 |
); |
| 896 |
assert_eq!($Two.parse::<Enum>().unwrap(), Enum::Two); |
| 897 |
} |
| 898 |
|
| 899 |
#[test] |
| 900 |
fn only_variant_level() { |
| 901 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 902 |
enum Enum { |
| 903 |
#[from_str(rename_all = $casing)] |
| 904 |
VariantOne, |
| 905 |
#[from_str(rename_all = $casing)] |
| 906 |
Two, |
| 907 |
} |
| 908 |
|
| 909 |
assert_eq!( |
| 910 |
$VariantOne.parse::<Enum>().unwrap(), |
| 911 |
Enum::VariantOne, |
| 912 |
); |
| 913 |
assert_eq!($Two.parse::<Enum>().unwrap(), Enum::Two); |
| 914 |
} |
| 915 |
} |
| 916 |
}; |
| 917 |
} |
| 918 |
|
| 919 |
casing_test!(lower_case, "lowercase", "variantone", "two"); |
| 920 |
casing_test!(upper_case, "UPPERCASE", "VARIANTONE", "TWO"); |
| 921 |
casing_test!(pascal_case, "PascalCase", "VariantOne", "Two"); |
| 922 |
casing_test!(camel_case, "camelCase", "variantOne", "two"); |
| 923 |
casing_test!(snake_case, "snake_case", "variant_one", "two"); |
| 924 |
casing_test!( |
| 925 |
screaming_snake_case, |
| 926 |
"SCREAMING_SNAKE_CASE", |
| 927 |
"VARIANT_ONE", |
| 928 |
"TWO" |
| 929 |
); |
| 930 |
casing_test!(kebab_case, "kebab-case", "variant-one", "two"); |
| 931 |
casing_test!( |
| 932 |
screaming_kebab_case, |
| 933 |
"SCREAMING-KEBAB-CASE", |
| 934 |
"VARIANT-ONE", |
| 935 |
"TWO" |
| 936 |
); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
mod custom_error { |
| 941 |
use derive_more::FromStrError; |
| 942 |
|
| 943 |
use super::*; |
| 944 |
|
| 945 |
#[derive(Debug)] |
| 946 |
struct CustomError(FromStrError); |
| 947 |
|
| 948 |
impl CustomError { |
| 949 |
fn new(err: FromStrError) -> Self { |
| 950 |
Self(err) |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
impl From<FromStrError> for CustomError { |
| 955 |
fn from(value: FromStrError) -> Self { |
| 956 |
Self(value) |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
fn custom_error_fn() -> fn(FromStrError) -> CustomError { |
| 961 |
CustomError::new |
| 962 |
} |
| 963 |
|
| 964 |
macro_rules! assertions { |
| 965 |
() => { |
| 966 |
assert_eq!("foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 967 |
assert_eq!("Foo".parse::<Enum>().unwrap(), Enum::Foo); |
| 968 |
assert_eq!("FOO".parse::<Enum>().unwrap(), Enum::Foo); |
| 969 |
|
| 970 |
assert_eq!("Bar".parse::<Enum>().unwrap(), Enum::Bar); |
| 971 |
assert_eq!("bar".parse::<Enum>().unwrap(), Enum::Bar); |
| 972 |
|
| 973 |
assert_eq!("Baz".parse::<Enum>().unwrap(), Enum::Baz); |
| 974 |
assert_eq!("BaZ".parse::<Enum>().unwrap(), Enum::BaZ); |
| 975 |
|
| 976 |
assert_eq!( |
| 977 |
match "baz".parse::<Enum>().unwrap_err() { |
| 978 |
CustomError(e) => e.to_string(), |
| 979 |
}, |
| 980 |
"Invalid `Enum` string representation", |
| 981 |
); |
| 982 |
assert_eq!( |
| 983 |
match "other".parse::<Enum>().unwrap_err() { |
| 984 |
CustomError(e) => e.to_string(), |
| 985 |
}, |
| 986 |
"Invalid `Enum` string representation", |
| 987 |
); |
| 988 |
}; |
| 989 |
} |
| 990 |
|
| 991 |
#[test] |
| 992 |
fn only_ty() { |
| 993 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 994 |
#[from_str(error(CustomError))] |
| 995 |
enum Enum { |
| 996 |
Foo, |
| 997 |
Bar, |
| 998 |
Baz, |
| 999 |
BaZ, |
| 1000 |
} |
| 1001 |
|
| 1002 |
assertions!(); |
| 1003 |
} |
| 1004 |
|
| 1005 |
#[test] |
| 1006 |
fn with_fn() { |
| 1007 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 1008 |
#[from_str(error(CustomError, CustomError::new))] |
| 1009 |
enum Enum { |
| 1010 |
Foo, |
| 1011 |
Bar, |
| 1012 |
Baz, |
| 1013 |
BaZ, |
| 1014 |
} |
| 1015 |
|
| 1016 |
assertions!(); |
| 1017 |
} |
| 1018 |
|
| 1019 |
#[expect(clippy::redundant_closure, reason = "intended for testing")] |
| 1020 |
#[test] |
| 1021 |
fn with_closure() { |
| 1022 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 1023 |
#[from_str(error(CustomError, |e| CustomError::new(e)))] |
| 1024 |
enum Enum { |
| 1025 |
Foo, |
| 1026 |
Bar, |
| 1027 |
Baz, |
| 1028 |
BaZ, |
| 1029 |
} |
| 1030 |
|
| 1031 |
assertions!(); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn with_fn_call() { |
| 1036 |
#[derive(Debug, Eq, FromStr, PartialEq)] |
| 1037 |
#[from_str(error(CustomError, custom_error_fn()))] |
| 1038 |
enum Enum { |
| 1039 |
Foo, |
| 1040 |
Bar, |
| 1041 |
Baz, |
| 1042 |
BaZ, |
| 1043 |
} |
| 1044 |
|
| 1045 |
assertions!(); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |