use std::fmt;
use serde::{de, Deserialize, ser, Serialize};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LevelOfServiceCode {
NotSpecified,
PatientConsultation,
HomeDelivery,
Emergency,
Code04,
PatientConsultationRegardingGenericProductSelection,
AsNeeded,
Other,
InitialOfficeVisit,
FollowUpOfficeVisit,
Elective,
FullTreatmentPhaseOne,
FullTreatmentPhaseTwo,
Initial,
LimitedTreatment,
MedicareExpedited,
NewbornCare,
Routine,
Urgent,
}
impl LevelOfServiceCode {
pub fn code(&self) -> &str {
{
use LevelOfServiceCode::*;
match self {
NotSpecified => "00",
PatientConsultation => "01",
HomeDelivery => "02",
Emergency => "03",
Code04 => "04",
PatientConsultationRegardingGenericProductSelection => "05",
AsNeeded => "06",
Other => "09",
InitialOfficeVisit => "10",
FollowUpOfficeVisit => "11",
Elective => "E",
FullTreatmentPhaseOne => "F1",
FullTreatmentPhaseTwo => "F2",
Initial => "I",
LimitedTreatment => "L",
MedicareExpedited => "ME",
NewbornCare => "NBC",
Routine => "R",
Urgent => "U",
}
}
}
pub fn from_code(code: &[u8]) -> Option<LevelOfServiceCode> {
use LevelOfServiceCode::*;
match code {
b"00" => Some(NotSpecified),
b"01" => Some(PatientConsultation),
b"02" => Some(HomeDelivery),
b"03" => Some(Emergency),
b"04" => Some(Code04),
b"05" => Some(PatientConsultationRegardingGenericProductSelection),
b"06" => Some(AsNeeded),
b"09" => Some(Other),
b"10" => Some(InitialOfficeVisit),
b"11" => Some(FollowUpOfficeVisit),
b"E" => Some(Elective),
b"F1" => Some(FullTreatmentPhaseOne),
b"F2" => Some(FullTreatmentPhaseTwo),
b"I" => Some(Initial),
b"L" => Some(LimitedTreatment),
b"ME" => Some(MedicareExpedited),
b"NBC" => Some(NewbornCare),
b"R" => Some(Routine),
b"U" => Some(Urgent),
_ => None,
}
}
fn description(&self) -> &str {
use LevelOfServiceCode::*;
match self {
NotSpecified => "Not specified",
PatientConsultation => "Patient Consultation",
HomeDelivery => "Home delivery",
Emergency => "Emergency",
Code04 => "24 Hour",
PatientConsultationRegardingGenericProductSelection => {
"Patient Consultation Regarding Generic Product Selection"
}
AsNeeded => "As Needed",
Other => "Other",
InitialOfficeVisit => "Initial Office Visit",
FollowUpOfficeVisit => "Follow-up Office Visit",
Elective => "Elective",
FullTreatmentPhaseOne => "Full Treatment - Phase One",
FullTreatmentPhaseTwo => "Full Treatment - Phase Two",
Initial => "Initial",
LimitedTreatment => "Limited Treatment",
MedicareExpedited => "Medicare Expedited",
NewbornCare => "Newborn Care",
Routine => "Routine",
Urgent => "Urgent",
}
}
fn from_description(description: &str) -> Option<LevelOfServiceCode> {
{
use LevelOfServiceCode::*;
match description {
"Not specified" => Some(NotSpecified),
"Patient Consultation" => Some(PatientConsultation),
"Home delivery" => Some(HomeDelivery),
"Emergency" => Some(Emergency),
"24 Hour" => Some(Code04),
"Patient Consultation Regarding Generic Product Selection" => {
Some(PatientConsultationRegardingGenericProductSelection)
}
"As Needed" => Some(AsNeeded),
"Other" => Some(Other),
"Initial Office Visit" => Some(InitialOfficeVisit),
"Follow-up Office Visit" => Some(FollowUpOfficeVisit),
"Elective" => Some(Elective),
"Full Treatment - Phase One" => Some(FullTreatmentPhaseOne),
"Full Treatment - Phase Two" => Some(FullTreatmentPhaseTwo),
"Initial" => Some(Initial),
"Limited Treatment" => Some(LimitedTreatment),
"Medicare Expedited" => Some(MedicareExpedited),
"Newborn Care" => Some(NewbornCare),
"Routine" => Some(Routine),
"Urgent" => Some(Urgent),
_ => None,
}
}
}
}
impl Serialize for LevelOfServiceCode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
let value = if serializer.is_human_readable() {
self.description()
} else {
self.code()
};
serializer.serialize_str(value)
}
}
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = LevelOfServiceCode;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Level of Service Code")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
LevelOfServiceCode::from_description(v)
.ok_or_else(|| E::custom(format!("Invalid Level of Service Code: {}", v)))
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
LevelOfServiceCode::from_code(v)
.ok_or_else(|| E::custom(
format!(
"Invalid Level of Service Code: {}", std::str::from_utf8(v).unwrap()
),
))
}
}
impl<'de> Deserialize<'de> for LevelOfServiceCode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
if deserializer.is_human_readable() {
deserializer.deserialize_str(Visitor)
} else {
deserializer.deserialize_bytes(Visitor)
}
}
}