sea-orm-ffi 0.1.3

Compatibility layer for Sea-ORM when crossing a Rust-to-Rust FFI boundary
Documentation
//! Structs that support bridging [`time`] types.
//!
//! Note that these types need to be present _at all times_, regardless of compilation
//! flags. The C representation of the values must not change with feature flags!
//!
//! The [`From`] implementations should of course be cfg-ed out if the feature is not
//! enabled.

#[cfg(feature = "with-time")]
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};

#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct OrdinalDate {
	year: i32,
	ordinal: u16
}

#[cfg(feature = "with-time")]
impl From<Date> for OrdinalDate {
	fn from(value: Date) -> Self {
		let (year, ordinal) = value.to_ordinal_date();
		Self { year, ordinal }
	}
}

#[cfg(feature = "with-time")]
impl From<OrdinalDate> for Date {
	fn from(value: OrdinalDate) -> Self {
		Self::from_ordinal_date(value.year, value.ordinal).unwrap()
	}
}

#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct HmsNano {
	nano: u32,
	hour: u8,
	minute: u8,
	second: u8
}

#[cfg(feature = "with-time")]
impl From<Time> for HmsNano {
	fn from(value: Time) -> Self {
		Self {
			nano: value.nanosecond(),
			hour: value.hour(),
			minute: value.minute(),
			second: value.second()
		}
	}
}

#[cfg(feature = "with-time")]
impl From<HmsNano> for Time {
	fn from(value: HmsNano) -> Self {
		Self::from_hms_nano(value.hour, value.minute, value.second, value.nano).unwrap()
	}
}

#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct OrdinalDateHmsNano {
	year: i32,
	ordinal: u16,
	hour: u8,
	minute: u8,
	nano: u32,
	second: u8
}

#[cfg(feature = "with-time")]
impl From<PrimitiveDateTime> for OrdinalDateHmsNano {
	fn from(value: PrimitiveDateTime) -> Self {
		let (year, ordinal) = value.date().to_ordinal_date();
		let time = value.time();
		Self {
			year,
			ordinal,
			hour: time.hour(),
			minute: time.minute(),
			nano: time.nanosecond(),
			second: time.second()
		}
	}
}

#[cfg(feature = "with-time")]
impl From<OrdinalDateHmsNano> for PrimitiveDateTime {
	fn from(value: OrdinalDateHmsNano) -> Self {
		let date = Date::from_ordinal_date(value.year, value.ordinal).unwrap();
		let time =
			Time::from_hms_nano(value.hour, value.minute, value.second, value.nano)
				.unwrap();
		Self::new(date, time)
	}
}

#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct OrdinalDateHmsNanoOffset {
	year: i32,
	ordinal: u16,
	hour: u8,
	minute: u8,
	nano: u32,
	second: u8,
	offset_hour: i8,
	offset_minute: i8,
	offset_second: i8
}

#[cfg(feature = "with-time")]
impl From<OffsetDateTime> for OrdinalDateHmsNanoOffset {
	fn from(value: OffsetDateTime) -> Self {
		let (year, ordinal) = value.date().to_ordinal_date();
		let time = value.time();
		let (offset_hour, offset_minute, offset_second) = value.offset().as_hms();
		Self {
			year,
			ordinal,
			hour: time.hour(),
			minute: time.minute(),
			nano: time.nanosecond(),
			second: time.second(),
			offset_hour,
			offset_minute,
			offset_second
		}
	}
}

#[cfg(feature = "with-time")]
impl From<OrdinalDateHmsNanoOffset> for OffsetDateTime {
	fn from(value: OrdinalDateHmsNanoOffset) -> Self {
		let date = Date::from_ordinal_date(value.year, value.ordinal).unwrap();
		let time =
			Time::from_hms_nano(value.hour, value.minute, value.second, value.nano)
				.unwrap();
		let offset = UtcOffset::from_hms(
			value.offset_hour,
			value.offset_minute,
			value.offset_second
		)
		.unwrap();
		Self::new_in_offset(date, time, offset)
	}
}