use std::fmt;
use std::str::FromStr;
use rust_decimal::Decimal;
use super::common::positive_integer_digits;
use super::id::{CadastralAreaCode, MunicipalityPartCode, OwnershipSheetId, ParcelId, UnitId};
use super::search::{
AuthorizedSubject, Building, BuildingRight, BuildingTypeCode, CadastralAreaReference,
Ownership, Parcel, Unit,
};
use crate::{Error, Result};
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct UnitBuildingNumber(String);
impl UnitBuildingNumber {
pub fn new(value: impl Into<String>) -> Result<Self> {
let value = value.into();
let Some(digits) = positive_integer_digits(&value) else {
return Err(Error::invalid_input(
"building_number",
"expected a positive decimal integer with at most 30 digits",
));
};
if digits.len() > 30 {
return Err(Error::invalid_input(
"building_number",
"expected a positive decimal integer with at most 30 digits",
));
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for UnitBuildingNumber {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for UnitBuildingNumber {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
Self::new(value)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for UnitBuildingNumber {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = <String as serde::Deserialize>::deserialize(deserializer)?;
Self::new(value).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParcelBoundingBoxRequest {
pub parcel_ids: Vec<ParcelId>,
}
impl ParcelBoundingBoxRequest {
pub fn new(parcel_ids: Vec<ParcelId>) -> Result<Self> {
let value = Self { parcel_ids };
value.validate()?;
Ok(value)
}
pub(crate) fn validate(&self) -> Result<()> {
if self.parcel_ids.is_empty() {
return Err(Error::invalid_input(
"parcel_ids",
"at least one parcel ID is required",
));
}
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UnitOverviewSelector {
Id(UnitId),
Number(UnitOverviewNaturalKey),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UnitOverviewNaturalKey {
pub municipality_part: MunicipalityPartCode,
pub building_type: BuildingTypeCode,
pub building_number: UnitBuildingNumber,
pub unit_number: u64,
}
impl UnitOverviewNaturalKey {
pub fn new(
municipality_part: MunicipalityPartCode,
building_type: BuildingTypeCode,
building_number: UnitBuildingNumber,
unit_number: u64,
) -> Result<Self> {
let value = Self {
municipality_part,
building_type,
building_number,
unit_number,
};
value.validate()?;
Ok(value)
}
pub(crate) fn validate(&self) -> Result<()> {
if self.unit_number > 9_999_999_999 {
return Err(Error::invalid_input(
"unit_number",
"unit number must be at most 9999999999",
));
}
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UnitOverviewRequest {
pub selector: UnitOverviewSelector,
}
impl UnitOverviewRequest {
pub(crate) fn validate(&self) -> Result<()> {
match &self.selector {
UnitOverviewSelector::Id(_) => Ok(()),
UnitOverviewSelector::Number(key) => key.validate(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OwnershipSheetPreviewSelector {
Id(OwnershipSheetId),
Number(OwnershipSheetNaturalKey),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OwnershipSheetNaturalKey {
pub cadastral_area: CadastralAreaCode,
pub sheet_number: u32,
}
impl OwnershipSheetNaturalKey {
pub fn new(cadastral_area: CadastralAreaCode, sheet_number: u32) -> Result<Self> {
let value = Self {
cadastral_area,
sheet_number,
};
value.validate()?;
Ok(value)
}
pub(crate) fn validate(&self) -> Result<()> {
if !(1..=999_999).contains(&self.sheet_number) {
return Err(Error::invalid_input(
"sheet_number",
"ownership-sheet number must be in 1..=999999",
));
}
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OwnershipSheetPreviewRequest {
pub selector: OwnershipSheetPreviewSelector,
}
impl OwnershipSheetPreviewRequest {
pub(crate) fn validate(&self) -> Result<()> {
match &self.selector {
OwnershipSheetPreviewSelector::Id(_) => Ok(()),
OwnershipSheetPreviewSelector::Number(key) => key.validate(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InformationRequest {
ParcelBoundingBoxes(ParcelBoundingBoxRequest),
UnitOverview(UnitOverviewRequest),
OwnershipSheetPreview(OwnershipSheetPreviewRequest),
}
impl InformationRequest {
pub(crate) fn validate(&self) -> Result<()> {
match self {
Self::ParcelBoundingBoxes(request) => request.validate(),
Self::UnitOverview(request) => request.validate(),
Self::OwnershipSheetPreview(request) => request.validate(),
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SpatialReference {
pub name: Option<String>,
pub dimension: Option<u32>,
pub axis_labels: Option<Vec<String>>,
pub unit_labels: Option<Vec<String>>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SJtskPoint {
pub y: Decimal,
pub x: Decimal,
pub reference: SpatialReference,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EnvelopeEncoding {
Corners,
Positions,
Coordinates {
decimal_separator: String,
coordinate_separator: String,
tuple_separator: String,
},
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BoundingEnvelope {
pub lower: SJtskPoint,
pub upper: SJtskPoint,
pub encoding: EnvelopeEncoding,
pub reference: SpatialReference,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GmlNull {
pub reason: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BoundingGeometry {
Envelope(Box<BoundingEnvelope>),
Null(GmlNull),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParcelBoundingBox {
pub parcel_ids: Vec<String>,
pub id_kind: String,
pub geometry: BoundingGeometry,
pub nil_reason: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParcelBoundingBoxResponse {
pub entries: Option<Vec<ParcelBoundingBox>>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OwnershipSheet {
pub id: OwnershipSheetId,
pub cadastral_area: CadastralAreaReference,
pub number: u32,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DetailedUnit {
pub unit: Unit,
pub type_name: Option<String>,
pub use_name: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UnitOverviewResponse {
pub ownership_sheet: Option<OwnershipSheet>,
pub unit: Option<DetailedUnit>,
pub building: Option<Building>,
pub subjects: Vec<AuthorizedSubject>,
pub ownerships: Vec<Ownership>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OwnershipSheetPreviewResponse {
pub ownership_sheet: Option<OwnershipSheet>,
pub subjects: Vec<AuthorizedSubject>,
pub ownerships: Vec<Ownership>,
pub parcels: Vec<Parcel>,
pub buildings: Vec<Building>,
pub units: Vec<DetailedUnit>,
pub building_rights: Vec<BuildingRight>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InformationResponse {
ParcelBoundingBoxes(ParcelBoundingBoxResponse),
UnitOverview(Box<UnitOverviewResponse>),
OwnershipSheetPreview(OwnershipSheetPreviewResponse),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn requests_validate_nonempty_and_natural_key_ranges() {
assert!(ParcelBoundingBoxRequest::new(Vec::new()).is_err());
assert!(OwnershipSheetNaturalKey::new(CadastralAreaCode::new("1").unwrap(), 0).is_err());
assert!(UnitBuildingNumber::new("123456789012345678901234567890").is_ok());
assert!(UnitBuildingNumber::new("1234567890123456789012345678901").is_err());
let raw = " \t+123456789012345678901234567890\r\n";
assert_eq!(UnitBuildingNumber::new(raw).unwrap().as_str(), raw);
assert!(UnitBuildingNumber::new(" +1234567890123456789012345678901 ").is_err());
assert!(UnitBuildingNumber::new("++1").is_err());
assert!(
ParcelBoundingBoxRequest {
parcel_ids: Vec::new()
}
.validate()
.is_err()
);
assert!(
UnitOverviewNaturalKey {
municipality_part: MunicipalityPartCode::new("1").unwrap(),
building_type: BuildingTypeCode::new("1").unwrap(),
building_number: UnitBuildingNumber::new("1").unwrap(),
unit_number: 10_000_000_000,
}
.validate()
.is_err()
);
assert!(
OwnershipSheetNaturalKey {
cadastral_area: CadastralAreaCode::new("1").unwrap(),
sheet_number: 0,
}
.validate()
.is_err()
);
}
#[cfg(feature = "serde")]
#[test]
fn building_number_deserialization_uses_the_validating_constructor() {
let number: UnitBuildingNumber = serde_json::from_str("\" +0001 \"").unwrap();
assert_eq!(number.as_str(), " +0001 ");
assert!(serde_json::from_str::<UnitBuildingNumber>("\"0\"").is_err());
}
}