opentalk_types_common/roomserver/
token.rs

1// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5use derive_more::{AsRef, Display, From, FromStr, Into};
6use uuid::Uuid;
7
8use crate::utils::ExampleData;
9
10/// The token that is required to start a roomserver signaling session
11#[derive(
12    AsRef, Display, From, FromStr, Into, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
13)]
14#[cfg_attr(
15    feature = "redis",
16    derive(redis_args::ToRedisArgs, redis_args::FromRedisValue)
17)]
18#[cfg_attr(feature = "redis", to_redis_args(fmt), from_redis_value(FromStr))]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema), schema(example = json!(Token::example_data())))]
21pub struct Token(Uuid);
22
23impl Token {
24    /// Create a ZERO token id, e.g. for testing purposes
25    pub const fn nil() -> Self {
26        Self(Uuid::nil())
27    }
28
29    /// Create a token from a number, e.g. for testing purposes
30    pub const fn from_u128(id: u128) -> Self {
31        Self(Uuid::from_u128(id))
32    }
33
34    /// Generate a new random token
35    #[cfg(feature = "rand")]
36    pub fn generate() -> Self {
37        Self(Uuid::new_v4())
38    }
39}
40
41impl ExampleData for Token {
42    fn example_data() -> Self {
43        Self::from_u128(0xabadcafe)
44    }
45}