use chrono_tz::Tz;
use crate::util::{Locale, Utf16String};
use super::temporal_formatting_utils::TemporalFormattingError;
use super::{TemporalArrayUtils, TemporalValue};
pub struct TemporalSetUtils {
array_utils: TemporalArrayUtils,
}
impl TemporalSetUtils {
pub fn new(locale: Locale, default_zone_id: Tz) -> Result<Self, TemporalFormattingError> {
Ok(Self {
array_utils: TemporalArrayUtils::new(locale, default_zone_id)?,
})
}
pub fn set_format(
&self,
target: &[Option<TemporalValue>],
pattern: Option<&str>,
locale: Option<&Locale>,
) -> Result<Vec<Option<Utf16String>>, TemporalFormattingError> {
Ok(dedupe(
self.array_utils.array_format(target, pattern, locale)?,
))
}
pub fn set_day(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_day(target)?))
}
pub fn set_month(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_month(target)?))
}
pub fn set_year(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_year(target)?))
}
pub fn set_day_of_week(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_day_of_week(target)?))
}
pub fn set_hour(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_hour(target)?))
}
pub fn set_minute(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_minute(target)?))
}
pub fn set_second(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_second(target)?))
}
pub fn set_nanosecond(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<i32>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_nanosecond(target)?))
}
pub fn set_month_name(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<Utf16String>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_month_name(target)?))
}
pub fn set_month_name_short(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<Utf16String>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_month_name_short(target)?))
}
pub fn set_day_of_week_name(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<Utf16String>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_day_of_week_name(target)?))
}
pub fn set_day_of_week_name_short(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<Utf16String>>, TemporalFormattingError> {
Ok(dedupe(
self.array_utils.array_day_of_week_name_short(target)?,
))
}
pub fn set_format_iso(
&self,
target: &[Option<TemporalValue>],
) -> Result<Vec<Option<Utf16String>>, TemporalFormattingError> {
Ok(dedupe(self.array_utils.array_format_iso(target)?))
}
}
fn dedupe<T: PartialEq>(values: Vec<Option<T>>) -> Vec<Option<T>> {
let mut output = Vec::with_capacity(values.len());
for value in values {
if !output.contains(&value) {
output.push(value);
}
}
output
}