File: remote_derive.rs

package info (click to toggle)
rust-schemars 0.8.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: makefile: 2
file content (45 lines) | stat: -rw-r--r-- 986 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
mod util;

use other_crate::Duration;
use schemars::JsonSchema;
use serde::Serialize;
use util::*;

mod other_crate {
    #[derive(Default)]
    pub struct Duration {
        pub secs: i64,
        pub nanos: i32,
    }
}

#[derive(JsonSchema, Serialize)]
#[serde(remote = "Duration")]
struct DurationDef {
    secs: i64,
    nanos: i32,
}

fn custom_serialize<S>(value: &Duration, ser: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    ser.collect_str(&format_args!("{}.{:09}s", value.secs, value.nanos))
}

#[derive(JsonSchema, Serialize)]
struct Process {
    command_line: String,
    #[serde(with = "DurationDef")]
    wall_time: Duration,
    #[serde(default, with = "DurationDef")]
    user_cpu_time: Duration,
    #[serde(default, serialize_with = "custom_serialize")]
    #[schemars(with = "DurationDef")]
    system_cpu_time: Duration,
}

#[test]
fn remote_derive_json_schema() -> TestResult {
    test_default_generated_schema::<Process>("remote_derive")
}