Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed zip code error #2

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,50 @@ impl<'de> de::Visitor<'de> for TrainResponseWrapperVisitor {
}
}

/// Custom visitor used to deserialize the [`zip`] field in the [`Station`] struct.
///
/// The Amtrak API will sometimes serialize the zip code as an integer and
/// sometimes as a string. This visitor will handle both cases.
///
/// [`zip`]: Station::zip
struct ZipCodeVisitor;

impl<'de> de::Visitor<'de> for ZipCodeVisitor {
type Value = String;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string or an integer")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(v.to_string())
}

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(v.to_string())
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(v.to_string())
}
}

fn deserialize_zip_code<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_any(ZipCodeVisitor)
}

/// Represents an Amtrak train
#[derive(Debug, Deserialize, Clone)]
pub struct Train {
Expand Down Expand Up @@ -529,6 +573,7 @@ pub struct Station {
/// * `19104`
/// * `10001`
/// * `L7T 4A8` (Canadian zip code)
#[serde(deserialize_with = "deserialize_zip_code")]
pub zip: String,

/// A list of current [`train_id`] that have departed from or are enroute to
Expand Down
Loading