Repository: tokio-rs/axum
File: axum/src/extract/path/de.rs (Rust)

Code before:
    {
        self.deserialize_str(v)
    }

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        if self.url_params.len() != 1 {
            return Err(PathDeserializationError::wrong_number_of_parameters()
                .got(self.url_params.len())
                .expected(1));
        }
        let key = &self.url_params[0].0;
        let value = &self.url_params[0].1;
        visitor
            .visit_borrowed_str(value)
            .map_err(|e: Self::Error| match e.kind {
                ErrorKind::Message(message) => {
                    PathDeserializationError::new(ErrorKind::DeserializeError {
                        key: key.to_string(),
                        value: value.as_str().to_owned(),
                        message,
                    })
                }
                kind => PathDeserializationError::new(kind),
            })
    }

    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_unit()
    }

    fn deserialize_unit_struct<V>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_unit()
    }

    fn deserialize_newtype_struct<V>(
        self,
        _name: &'static str,
        visitor: V,

Review comment (suggestion):
This is very nitty so take it or leave it, but if you really care just about one variant, you can use `if let` you'll have less indented code (maybe not in this case because the match is on the same line as the closure argument) and in the else branch you can directly return `e` instead of constructing the error again from a kind.

Code after:
    {
        self.deserialize_str(v)
    }

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        if self.url_params.len() != 1 {
            return Err(PathDeserializationError::wrong_number_of_parameters()
                .got(self.url_params.len())
                .expected(1));
        }
        let key = &self.url_params[0].0;
        let value = &self.url_params[0].1;
        visitor
            .visit_borrowed_str(value)
            .map_err(|e: PathDeserializationError| {
                if let ErrorKind::Message(message) = &e.kind {
                    PathDeserializationError::new(ErrorKind::DeserializeError {
                        key: key.to_string(),
                        value: value.as_str().to_owned(),
                        message: message.to_owned(),
                    })
                } else {
                    e
                }
            })
    }

    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_unit()
    }

    fn deserialize_unit_struct<V>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_unit()
    }

    fn deserialize_newtype_struct<V>(
        self,
        _name: &'static str,