nil_payload/response/
city.rs1use derive_more::{Deref, DerefMut, Display, From, Into};
5use nil_core::city::{City, PublicCity};
6use nil_core::continent::coord::Coord;
7use nil_core::error::Error as CoreError;
8use nil_core::ranking::score::Score;
9use nil_core::world::World;
10use serde::{Deserialize, Serialize};
11
12#[cfg(feature = "axum")]
13use nil_payload_macros::IntoJsonResponse;
14
15#[cfg(feature = "typescript")]
16use ts_rs::TS;
17
18#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
19#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
20#[cfg_attr(feature = "typescript", derive(TS))]
21#[cfg_attr(feature = "typescript", ts(export))]
22pub struct GetCityResponse(pub City);
23
24#[derive(Clone, Copy, Debug, Deref, DerefMut, Display, From, Into, Deserialize, Serialize)]
25#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
26#[cfg_attr(feature = "typescript", derive(TS))]
27#[cfg_attr(feature = "typescript", ts(export))]
28pub struct GetCityScoreResponse(pub Score);
29
30#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
31#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
32#[cfg_attr(feature = "typescript", derive(TS))]
33#[cfg_attr(feature = "typescript", ts(export))]
34pub struct GetPublicCitiesResponse(pub Vec<GetPublicCityResponse>);
35
36#[derive(Clone, Debug, Deserialize, Serialize)]
37#[serde(rename_all = "camelCase")]
38#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
39#[cfg_attr(feature = "typescript", derive(TS))]
40#[cfg_attr(feature = "typescript", ts(export))]
41pub struct GetPublicCityResponse {
42 pub city: PublicCity,
43 pub score: Option<Score>,
44}
45
46#[bon::bon]
47impl GetPublicCityResponse {
48 #[builder]
49 pub fn new(
50 #[builder(start_fn)] world: &World,
51 #[builder(start_fn)] coord: Coord,
52 #[builder(default)] score: bool,
53 ) -> Result<Self, CoreError> {
54 let city = world.city(coord)?;
55 let score = score
56 .then(|| city.score(&world.stats().infrastructure()))
57 .transpose()?;
58
59 Ok(Self { city: PublicCity::from(city), score })
60 }
61}
62
63#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
64#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
65#[cfg_attr(feature = "typescript", derive(TS))]
66#[cfg_attr(feature = "typescript", ts(export))]
67pub struct SearchCityResponse(pub Vec<City>);
68
69#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
70#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
71#[cfg_attr(feature = "typescript", derive(TS))]
72#[cfg_attr(feature = "typescript", ts(export))]
73pub struct SearchPublicCityResponse(pub Vec<PublicCity>);