seaplane_cli/
ops.rs

1//! This module provides types that wrap the API endpoint models and add additional fields/context
2//! that is only relevant for the CLI or purposes of consuming the API.
3
4pub mod encoded_string;
5pub mod flight;
6pub mod formation;
7pub mod locks;
8pub mod metadata;
9pub mod restrict;
10
11use std::fmt;
12
13use rand::Rng;
14use serde::{Deserialize, Serialize};
15
16pub use self::encoded_string::EncodedString;
17use crate::cli::validator::{validate_flight_name, validate_formation_name};
18
19pub fn generate_flight_name() -> String {
20    // TODO: Maybe set an upper bound on the number of iterations and don't expect
21    names::Generator::default()
22        .find(|name| validate_flight_name(name).is_ok())
23        .expect("Failed to generate a random name")
24}
25
26pub fn generate_formation_name() -> String {
27    // TODO: Maybe set an upper bound on the number of iterations and don't expect
28    names::Generator::default()
29        .find(|name| validate_formation_name(name).is_ok())
30        .expect("Failed to generate a random name")
31}
32
33#[derive(Deserialize, Serialize, Copy, Clone, Hash, PartialEq, Eq)]
34#[serde(transparent)]
35pub struct Id {
36    #[serde(
37        serialize_with = "hex::serde::serialize",
38        deserialize_with = "hex::serde::deserialize"
39    )]
40    pub inner: [u8; 32],
41}
42
43impl Default for Id {
44    fn default() -> Self { Self { inner: rand::thread_rng().gen() } }
45}
46
47impl Id {
48    pub fn new() -> Self { Self::default() }
49}
50
51impl fmt::Display for Id {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        write!(f, "{}", hex::encode(self.inner))
54    }
55}
56
57impl fmt::Debug for Id {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Id [ {self} ]") }
59}