Skip to main content

nil_server_types/
lib.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4#![cfg_attr(docsrs, feature(doc_cfg))]
5#![doc(html_favicon_url = "https://nil.dev.br/favicon.png")]
6#![feature(str_as_str)]
7
8use derive_more::From;
9use jiff::Zoned;
10use nil_core::continent::ContinentSize;
11use nil_core::player::PlayerId;
12use nil_core::round::RoundId;
13use nil_core::world::config::{WorldConfig, WorldId};
14use serde::{Deserialize, Serialize};
15use std::borrow::Cow;
16use std::ops::Deref;
17use strum::EnumIs;
18
19#[derive(Clone, Copy, Debug, EnumIs, Deserialize, Serialize)]
20#[serde(tag = "kind", rename_all = "kebab-case")]
21pub enum ServerKind {
22  Local { id: WorldId },
23  Remote,
24}
25
26#[derive(Clone, Debug, Deserialize, Serialize)]
27#[serde(rename_all = "camelCase")]
28pub struct RemoteWorld {
29  pub config: WorldConfig,
30  pub description: Option<String>,
31  pub created_by: PlayerId,
32  pub created_at: Zoned,
33  pub updated_at: Zoned,
34  pub has_password: bool,
35  pub current_round: RoundId,
36  pub active_players: usize,
37  pub total_players: usize,
38  pub continent_size: ContinentSize,
39}
40
41#[derive(Clone, Debug, From, Deserialize, Serialize)]
42#[from(String, &str, Box<str>, Cow<'_, str>)]
43pub struct Token(Box<str>);
44
45impl Token {
46  pub fn new(token: impl AsRef<str>) -> Self {
47    Self(Box::from(token.as_ref()))
48  }
49}
50
51impl AsRef<str> for Token {
52  fn as_ref(&self) -> &str {
53    self.0.as_str()
54  }
55}
56
57impl AsRef<[u8]> for Token {
58  fn as_ref(&self) -> &[u8] {
59    self.0.as_bytes()
60  }
61}
62
63impl Deref for Token {
64  type Target = str;
65
66  fn deref(&self) -> &Self::Target {
67    self.0.as_str()
68  }
69}