Skip to main content

cityjson_types/
lib.rs

1#![doc = include_str!("../README.md")]
2pub(crate) mod backend;
3mod cityjson;
4pub mod error;
5pub mod query;
6pub mod raw;
7pub mod relational;
8pub mod resources;
9pub mod symbols;
10pub mod v2_0;
11
12pub mod prelude {
13    pub use super::{CityJSON, CityJSONVersion, CityModelType};
14    pub use crate::error::{Error, Result};
15    pub use crate::resources::{
16        handles::{
17            CityObjectHandle, GeometryHandle, GeometryTemplateHandle, MaterialHandle,
18            SemanticHandle, TextureHandle,
19        },
20        mapping::{materials::MaterialMap, semantics::SemanticMap, textures::TextureMap},
21        storage::{BorrowedStringStorage, OwnedStringStorage, StringStorage},
22    };
23}
24
25use crate::error::{Error, Result};
26use crate::resources::storage::StringStorage;
27use crate::v2_0::VertexRef;
28use std::fmt;
29
30/// Whether a [`CityModel`](v2_0::citymodel::CityModel) is a full `CityJSON` document or a
31/// single-feature `CityJSONFeature` (used for streaming and tiling).
32#[repr(C)]
33#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum CityModelType {
36    #[default]
37    CityJSON,
38    CityJSONFeature,
39}
40
41impl fmt::Display for CityModelType {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match &self {
44            CityModelType::CityJSON => {
45                write!(f, "CityJSON")
46            }
47            CityModelType::CityJSONFeature => {
48                write!(f, "CityJSONFeature")
49            }
50        }
51    }
52}
53
54impl CityModelType {
55    fn _from_str(value: &str) -> error::Result<CityModelType> {
56        match value {
57            "CityJSON" => Ok(CityModelType::CityJSON),
58            "CityJSONFeature" => Ok(CityModelType::CityJSONFeature),
59            _ => Err(Error::UnsupportedVersion(
60                value.to_string(),
61                "CityJSON, CityJSONFeature".to_string(),
62            )),
63        }
64    }
65}
66
67impl TryFrom<&str> for CityModelType {
68    type Error = Error;
69
70    fn try_from(value: &str) -> Result<Self> {
71        CityModelType::_from_str(value)
72    }
73}
74
75impl TryFrom<String> for CityModelType {
76    type Error = Error;
77
78    fn try_from(value: String) -> Result<Self> {
79        CityModelType::_from_str(value.as_ref())
80    }
81}
82
83/// Supported `CityJSON` spec versions. Currently only v2.0.
84#[repr(C)]
85#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
86#[non_exhaustive]
87pub enum CityJSONVersion {
88    #[default]
89    V2_0,
90}
91
92impl CityJSONVersion {
93    fn _from_str(value: &str) -> error::Result<CityJSONVersion> {
94        match value {
95            "2.0" | "2.0.0" | "2.0.1" => Ok(CityJSONVersion::V2_0),
96            _ => Err(Error::UnsupportedVersion(
97                value.to_string(),
98                "2.0, 2.0.0, 2.0.1".to_string(),
99            )),
100        }
101    }
102}
103
104impl fmt::Display for CityJSONVersion {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        match &self {
107            CityJSONVersion::V2_0 => {
108                write!(f, "2.0")
109            }
110        }
111    }
112}
113
114impl TryFrom<&str> for CityJSONVersion {
115    type Error = Error;
116
117    fn try_from(value: &str) -> Result<Self> {
118        CityJSONVersion::_from_str(value)
119    }
120}
121
122impl TryFrom<String> for CityJSONVersion {
123    type Error = Error;
124
125    fn try_from(value: String) -> Result<Self> {
126        CityJSONVersion::_from_str(value.as_ref())
127    }
128}
129
130#[derive(Debug)]
131#[non_exhaustive]
132pub enum CityJSON<VR: VertexRef, SS: StringStorage> {
133    V2_0(v2_0::CityModel<VR, SS>),
134}
135
136fn format_option<T: std::fmt::Display>(option: Option<&T>) -> String {
137    option.map_or_else(|| "None".to_string(), std::string::ToString::to_string)
138}