Skip to content

Derive a boolean from "True" or "False" #43

Answered by frozenlib
ahenshaw asked this question in Q&A
Discussion options

You must be logged in to vote

Create a FromStrFormat (IgnoreCase in the example below) that converts the string to lowercase before parsing, and apply it to the flag field so that #1 can also be parsed.

use parse_display::{Display, DisplayFormat, FromStr, FromStrFormat};
use std::str::FromStr;

#[derive(Display, FromStr)]
#[display("{name}|{flag}")]
struct Test {
    name: String,

    #[display(with = IgnoreCase)]
    flag: bool,
}

struct IgnoreCase;

impl<T: std::fmt::Display> DisplayFormat<T> for IgnoreCase {
    fn write(&self, f: &mut std::fmt::Formatter, value: &T) -> std::fmt::Result {
        write!(f, "{}", value)
    }
}
impl<T: std::str::FromStr> FromStrFormat<T> for IgnoreCase {
    type Err = T::Err;
    fn

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ahenshaw
Comment options

Answer selected by ahenshaw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants