Skip to main content

nil_payload/response/
city.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use 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#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
16#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
17#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
18#[cfg_attr(feature = "typescript", ts(export))]
19pub struct GetCitiesResponse(pub Vec<GetCityResponse>);
20
21#[derive(Clone, Debug, Deserialize, Serialize)]
22#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
23#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
24#[cfg_attr(feature = "typescript", ts(export))]
25pub struct GetCityResponse {
26  pub city: City,
27  pub score: Option<Score>,
28}
29
30#[bon::bon]
31impl GetCityResponse {
32  #[builder]
33  pub fn new(
34    #[builder(start_fn)] world: &World,
35    #[builder(start_fn)] coord: Coord,
36    #[builder(default)] score: bool,
37  ) -> Result<Self, CoreError> {
38    let city = world.city(coord)?;
39    let score = score
40      .then(|| city.score(&world.stats().infrastructure()))
41      .transpose()?;
42
43    Ok(Self { city: city.clone(), score })
44  }
45}
46
47#[derive(Clone, Copy, Debug, Deref, DerefMut, Display, From, Into, Deserialize, Serialize)]
48#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
49#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
50#[cfg_attr(feature = "typescript", ts(export))]
51pub struct GetCityScoreResponse(pub Score);
52
53#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
54#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
55#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
56#[cfg_attr(feature = "typescript", ts(export))]
57pub struct GetPublicCitiesResponse(pub Vec<GetPublicCityResponse>);
58
59#[derive(Clone, Debug, Deserialize, Serialize)]
60#[serde(rename_all = "camelCase")]
61#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
62#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
63#[cfg_attr(feature = "typescript", ts(export))]
64pub struct GetPublicCityResponse {
65  pub city: PublicCity,
66  pub score: Option<Score>,
67}
68
69#[bon::bon]
70impl GetPublicCityResponse {
71  #[builder]
72  pub fn new(
73    #[builder(start_fn)] world: &World,
74    #[builder(start_fn)] coord: Coord,
75    #[builder(default)] score: bool,
76  ) -> Result<Self, CoreError> {
77    GetCityResponse::builder(world, coord)
78      .score(score)
79      .build()
80      .map(Into::into)
81  }
82}
83
84impl From<GetCityResponse> for GetPublicCityResponse {
85  fn from(value: GetCityResponse) -> Self {
86    Self {
87      city: PublicCity::from(&value.city),
88      score: value.score,
89    }
90  }
91}
92
93#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
94#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
95#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
96#[cfg_attr(feature = "typescript", ts(export))]
97pub struct SearchCityResponse(pub Vec<City>);
98
99#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
100#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
101#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
102#[cfg_attr(feature = "typescript", ts(export))]
103pub struct SearchPublicCityResponse(pub Vec<PublicCity>);