dfx_core/util/
mod.rs

1use std::{borrow::Cow, time::Duration};
2
3use schemars::{
4    JsonSchema,
5    r#gen::SchemaGenerator,
6    schema::{InstanceType, Metadata, Schema, SchemaObject, StringValidation},
7};
8
9pub fn network_to_pathcompat(network_name: &str) -> String {
10    network_name.replace(|c: char| !c.is_ascii_alphanumeric(), "_")
11}
12
13pub fn expiry_duration() -> Duration {
14    // 5 minutes is max ingress timeout
15    // 4 minutes accounts for possible replica drift
16    Duration::from_secs(60 * 4)
17}
18
19pub struct ByteSchema;
20
21impl JsonSchema for ByteSchema {
22    fn schema_name() -> String {
23        "Byte".to_string()
24    }
25    fn schema_id() -> Cow<'static, str> {
26        Cow::Borrowed("byte_unit::Byte")
27    }
28    fn json_schema(_: &mut SchemaGenerator) -> Schema {
29        Schema::Object(SchemaObject {
30            instance_type: Some(vec![InstanceType::Integer, InstanceType::String].into()),
31            number: None,
32            string: Some(Box::new(StringValidation {
33                pattern: Some("^[0-9]+( *([KkMmGgTtPpEeZzYy]i?)?[Bb])?$".to_string()),
34                ..Default::default()
35            })),
36            metadata: Some(Box::new(Metadata {
37                title: Some("Byte Count".to_string()),
38                description: Some("A quantity of bytes. Representable either as an integer, or as an SI unit string".to_string()),
39                examples: vec![72.into(), "2KB".into(), "4 MiB".into()],
40                ..Default::default()
41            })),
42            ..Default::default()
43        })
44    }
45}