Typify update for builder structs (#171)

This commit is contained in:
Adam Leventhal 2022-08-18 11:58:55 -07:00 committed by GitHub
parent 95ff213295
commit e4221a1350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 19854 additions and 54 deletions

6
Cargo.lock generated
View File

@ -1751,7 +1751,7 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "typify"
version = "0.0.10-dev"
source = "git+https://github.com/oxidecomputer/typify#f5da2758877a94eb5c8c4bd6091d07a468e22d3b"
source = "git+https://github.com/oxidecomputer/typify#ab2d3e18f624ce4a55278c0846ebb5f936134023"
dependencies = [
"typify-impl",
"typify-macro",
@ -1760,7 +1760,7 @@ dependencies = [
[[package]]
name = "typify-impl"
version = "0.0.10-dev"
source = "git+https://github.com/oxidecomputer/typify#f5da2758877a94eb5c8c4bd6091d07a468e22d3b"
source = "git+https://github.com/oxidecomputer/typify#ab2d3e18f624ce4a55278c0846ebb5f936134023"
dependencies = [
"heck",
"log",
@ -1778,7 +1778,7 @@ dependencies = [
[[package]]
name = "typify-macro"
version = "0.0.10-dev"
source = "git+https://github.com/oxidecomputer/typify#f5da2758877a94eb5c8c4bd6091d07a468e22d3b"
source = "git+https://github.com/oxidecomputer/typify#ab2d3e18f624ce4a55278c0846ebb5f936134023"
dependencies = [
"proc-macro2",
"quote",

View File

@ -11,6 +11,9 @@ members = [
#[patch."https://github.com/oxidecomputer/dropshot"]
#dropshot = { path = "../dropshot/dropshot" }
#[patch."https://github.com/oxidecomputer/typify"]
#typify = { path = "../typify/typify" }
#[patch.crates-io]
#typify = { path = "../typify/typify" }
#rustfmt-wrapper = { path = "../rustfmt-wrapper" }
#rustfmt-wrapper = { path = "../rustfmt-wrapper" }

View File

@ -5,7 +5,7 @@ use proc_macro2::TokenStream;
use quote::quote;
use serde::Deserialize;
use thiserror::Error;
use typify::TypeSpace;
use typify::{TypeSpace, TypeSpaceSettings};
use crate::to_schema::ToSchema;
@ -30,7 +30,6 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Default)]
pub struct Generator {
type_space: TypeSpace,
settings: GenerationSettings,
@ -44,10 +43,10 @@ pub struct GenerationSettings {
inner_type: Option<TokenStream>,
pre_hook: Option<TokenStream>,
post_hook: Option<TokenStream>,
extra_derives: Vec<TokenStream>,
extra_derives: Vec<String>,
}
#[derive(Clone, Deserialize)]
#[derive(Clone, Deserialize, PartialEq, Eq)]
pub enum InterfaceStyle {
Positional,
Builder,
@ -101,17 +100,35 @@ impl GenerationSettings {
self
}
// TODO maybe change to a typify::Settings or something
pub fn with_derive(&mut self, derive: TokenStream) -> &mut Self {
self.extra_derives.push(derive);
pub fn with_derive(&mut self, derive: impl ToString) -> &mut Self {
self.extra_derives.push(derive.to_string());
self
}
}
impl Default for Generator {
fn default() -> Self {
Self {
type_space: TypeSpace::new(
TypeSpaceSettings::default().with_type_mod("types"),
),
settings: Default::default(),
uses_futures: Default::default(),
}
}
}
impl Generator {
pub fn new(settings: &GenerationSettings) -> Self {
let mut type_settings = TypeSpaceSettings::default();
type_settings
.with_type_mod("types")
.with_struct_builder(settings.interface == InterfaceStyle::Builder);
settings.extra_derives.iter().for_each(|derive| {
let _ = type_settings.with_derive(derive.clone());
});
Self {
type_space: TypeSpace::default(),
type_space: TypeSpace::new(&type_settings),
settings: settings.clone(),
uses_futures: false,
}
@ -129,12 +146,7 @@ impl Generator {
})
.collect::<Vec<(String, _)>>();
self.type_space.set_type_mod("types");
self.type_space.add_ref_types(schemas)?;
self.settings
.extra_derives
.iter()
.for_each(|derive| self.type_space.add_derive(derive.clone()));
let raw_methods = spec
.paths
@ -176,14 +188,7 @@ impl Generator {
}
}?;
let mut types = self
.type_space
.iter_types()
.map(|t| (t.name(), t.definition()))
.collect::<Vec<_>>();
types.sort_by(|(a_name, _), (b_name, _)| a_name.cmp(b_name));
let types = types.into_iter().map(|(_, def)| def);
let shared = self.type_space.common_code();
let types = self.type_space.to_stream();
let inner_property = self.settings.inner_type.as_ref().map(|inner| {
quote! {
@ -208,15 +213,14 @@ impl Generator {
#[allow(unused_imports)]
use progenitor_client::{encode_path, RequestBuilderExt};
pub mod types {
use serde::{Deserialize, Serialize};
// This may be used by some impl Deserialize, but not all.
#[allow(unused_imports)]
use std::convert::TryFrom;
#shared
#(#types)*
#types
}
#[derive(Clone, Debug)]
@ -430,6 +434,7 @@ impl Generator {
deps.iter().map(ToString::to_string).collect()
}
// TODO deprecate?
pub fn get_type_space(&self) -> &TypeSpace {
&self.type_space
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -11,11 +11,23 @@ pub mod types {
pub key: String,
}
impl EnrolBody {
pub fn builder() -> builder::EnrolBody {
builder::EnrolBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GlobalJobsResult {
pub summary: Vec<ReportSummary>,
}
impl GlobalJobsResult {
pub fn builder() -> builder::GlobalJobsResult {
builder::GlobalJobsResult::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OutputRecord {
pub msg: String,
@ -23,12 +35,24 @@ pub mod types {
pub time: chrono::DateTime<chrono::offset::Utc>,
}
impl OutputRecord {
pub fn builder() -> builder::OutputRecord {
builder::OutputRecord::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PingResult {
pub host: String,
pub ok: bool,
}
impl PingResult {
pub fn builder() -> builder::PingResult {
builder::PingResult::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReportFinishBody {
pub duration_millis: i32,
@ -37,6 +61,12 @@ pub mod types {
pub id: ReportId,
}
impl ReportFinishBody {
pub fn builder() -> builder::ReportFinishBody {
builder::ReportFinishBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReportId {
pub host: String,
@ -46,17 +76,35 @@ pub mod types {
pub uuid: String,
}
impl ReportId {
pub fn builder() -> builder::ReportId {
builder::ReportId::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReportOutputBody {
pub id: ReportId,
pub record: OutputRecord,
}
impl ReportOutputBody {
pub fn builder() -> builder::ReportOutputBody {
builder::ReportOutputBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReportResult {
pub existed_already: bool,
}
impl ReportResult {
pub fn builder() -> builder::ReportResult {
builder::ReportResult::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReportStartBody {
pub id: ReportId,
@ -64,6 +112,12 @@ pub mod types {
pub start_time: chrono::DateTime<chrono::offset::Utc>,
}
impl ReportStartBody {
pub fn builder() -> builder::ReportStartBody {
builder::ReportStartBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReportSummary {
pub age_seconds: i32,
@ -73,6 +127,604 @@ pub mod types {
pub status: i32,
pub when: chrono::DateTime<chrono::offset::Utc>,
}
impl ReportSummary {
pub fn builder() -> builder::ReportSummary {
builder::ReportSummary::default()
}
}
mod builder {
pub struct EnrolBody {
host: Result<String, String>,
key: Result<String, String>,
}
impl Default for EnrolBody {
fn default() -> Self {
Self {
host: Err("no value supplied for host".to_string()),
key: Err("no value supplied for key".to_string()),
}
}
}
impl EnrolBody {
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn key<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.key = value
.try_into()
.map_err(|e| format!("error converting supplied value for key: {}", e));
self
}
}
impl std::convert::TryFrom<EnrolBody> for super::EnrolBody {
type Error = String;
fn try_from(value: EnrolBody) -> Result<Self, Self::Error> {
Ok(Self {
host: value.host?,
key: value.key?,
})
}
}
pub struct GlobalJobsResult {
summary: Result<Vec<super::ReportSummary>, String>,
}
impl Default for GlobalJobsResult {
fn default() -> Self {
Self {
summary: Err("no value supplied for summary".to_string()),
}
}
}
impl GlobalJobsResult {
pub fn summary<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<Vec<super::ReportSummary>>,
T::Error: std::fmt::Display,
{
self.summary = value
.try_into()
.map_err(|e| format!("error converting supplied value for summary: {}", e));
self
}
}
impl std::convert::TryFrom<GlobalJobsResult> for super::GlobalJobsResult {
type Error = String;
fn try_from(value: GlobalJobsResult) -> Result<Self, Self::Error> {
Ok(Self {
summary: value.summary?,
})
}
}
pub struct OutputRecord {
msg: Result<String, String>,
stream: Result<String, String>,
time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
}
impl Default for OutputRecord {
fn default() -> Self {
Self {
msg: Err("no value supplied for msg".to_string()),
stream: Err("no value supplied for stream".to_string()),
time: Err("no value supplied for time".to_string()),
}
}
}
impl OutputRecord {
pub fn msg<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.msg = value
.try_into()
.map_err(|e| format!("error converting supplied value for msg: {}", e));
self
}
pub fn stream<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.stream = value
.try_into()
.map_err(|e| format!("error converting supplied value for stream: {}", e));
self
}
pub fn time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.time = value
.try_into()
.map_err(|e| format!("error converting supplied value for time: {}", e));
self
}
}
impl std::convert::TryFrom<OutputRecord> for super::OutputRecord {
type Error = String;
fn try_from(value: OutputRecord) -> Result<Self, Self::Error> {
Ok(Self {
msg: value.msg?,
stream: value.stream?,
time: value.time?,
})
}
}
pub struct PingResult {
host: Result<String, String>,
ok: Result<bool, String>,
}
impl Default for PingResult {
fn default() -> Self {
Self {
host: Err("no value supplied for host".to_string()),
ok: Err("no value supplied for ok".to_string()),
}
}
}
impl PingResult {
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn ok<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<bool>,
T::Error: std::fmt::Display,
{
self.ok = value
.try_into()
.map_err(|e| format!("error converting supplied value for ok: {}", e));
self
}
}
impl std::convert::TryFrom<PingResult> for super::PingResult {
type Error = String;
fn try_from(value: PingResult) -> Result<Self, Self::Error> {
Ok(Self {
host: value.host?,
ok: value.ok?,
})
}
}
pub struct ReportFinishBody {
duration_millis: Result<i32, String>,
end_time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
exit_status: Result<i32, String>,
id: Result<super::ReportId, String>,
}
impl Default for ReportFinishBody {
fn default() -> Self {
Self {
duration_millis: Err("no value supplied for duration_millis".to_string()),
end_time: Err("no value supplied for end_time".to_string()),
exit_status: Err("no value supplied for exit_status".to_string()),
id: Err("no value supplied for id".to_string()),
}
}
}
impl ReportFinishBody {
pub fn duration_millis<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.duration_millis = value.try_into().map_err(|e| {
format!("error converting supplied value for duration_millis: {}", e)
});
self
}
pub fn end_time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.end_time = value
.try_into()
.map_err(|e| format!("error converting supplied value for end_time: {}", e));
self
}
pub fn exit_status<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.exit_status = value
.try_into()
.map_err(|e| format!("error converting supplied value for exit_status: {}", e));
self
}
pub fn id<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::ReportId>,
T::Error: std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
}
impl std::convert::TryFrom<ReportFinishBody> for super::ReportFinishBody {
type Error = String;
fn try_from(value: ReportFinishBody) -> Result<Self, Self::Error> {
Ok(Self {
duration_millis: value.duration_millis?,
end_time: value.end_time?,
exit_status: value.exit_status?,
id: value.id?,
})
}
}
pub struct ReportId {
host: Result<String, String>,
job: Result<String, String>,
pid: Result<u64, String>,
time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
uuid: Result<String, String>,
}
impl Default for ReportId {
fn default() -> Self {
Self {
host: Err("no value supplied for host".to_string()),
job: Err("no value supplied for job".to_string()),
pid: Err("no value supplied for pid".to_string()),
time: Err("no value supplied for time".to_string()),
uuid: Err("no value supplied for uuid".to_string()),
}
}
}
impl ReportId {
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn job<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.job = value
.try_into()
.map_err(|e| format!("error converting supplied value for job: {}", e));
self
}
pub fn pid<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<u64>,
T::Error: std::fmt::Display,
{
self.pid = value
.try_into()
.map_err(|e| format!("error converting supplied value for pid: {}", e));
self
}
pub fn time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.time = value
.try_into()
.map_err(|e| format!("error converting supplied value for time: {}", e));
self
}
pub fn uuid<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.uuid = value
.try_into()
.map_err(|e| format!("error converting supplied value for uuid: {}", e));
self
}
}
impl std::convert::TryFrom<ReportId> for super::ReportId {
type Error = String;
fn try_from(value: ReportId) -> Result<Self, Self::Error> {
Ok(Self {
host: value.host?,
job: value.job?,
pid: value.pid?,
time: value.time?,
uuid: value.uuid?,
})
}
}
pub struct ReportOutputBody {
id: Result<super::ReportId, String>,
record: Result<super::OutputRecord, String>,
}
impl Default for ReportOutputBody {
fn default() -> Self {
Self {
id: Err("no value supplied for id".to_string()),
record: Err("no value supplied for record".to_string()),
}
}
}
impl ReportOutputBody {
pub fn id<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::ReportId>,
T::Error: std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
pub fn record<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::OutputRecord>,
T::Error: std::fmt::Display,
{
self.record = value
.try_into()
.map_err(|e| format!("error converting supplied value for record: {}", e));
self
}
}
impl std::convert::TryFrom<ReportOutputBody> for super::ReportOutputBody {
type Error = String;
fn try_from(value: ReportOutputBody) -> Result<Self, Self::Error> {
Ok(Self {
id: value.id?,
record: value.record?,
})
}
}
pub struct ReportResult {
existed_already: Result<bool, String>,
}
impl Default for ReportResult {
fn default() -> Self {
Self {
existed_already: Err("no value supplied for existed_already".to_string()),
}
}
}
impl ReportResult {
pub fn existed_already<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<bool>,
T::Error: std::fmt::Display,
{
self.existed_already = value.try_into().map_err(|e| {
format!("error converting supplied value for existed_already: {}", e)
});
self
}
}
impl std::convert::TryFrom<ReportResult> for super::ReportResult {
type Error = String;
fn try_from(value: ReportResult) -> Result<Self, Self::Error> {
Ok(Self {
existed_already: value.existed_already?,
})
}
}
pub struct ReportStartBody {
id: Result<super::ReportId, String>,
script: Result<String, String>,
start_time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
}
impl Default for ReportStartBody {
fn default() -> Self {
Self {
id: Err("no value supplied for id".to_string()),
script: Err("no value supplied for script".to_string()),
start_time: Err("no value supplied for start_time".to_string()),
}
}
}
impl ReportStartBody {
pub fn id<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::ReportId>,
T::Error: std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
pub fn script<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.script = value
.try_into()
.map_err(|e| format!("error converting supplied value for script: {}", e));
self
}
pub fn start_time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.start_time = value
.try_into()
.map_err(|e| format!("error converting supplied value for start_time: {}", e));
self
}
}
impl std::convert::TryFrom<ReportStartBody> for super::ReportStartBody {
type Error = String;
fn try_from(value: ReportStartBody) -> Result<Self, Self::Error> {
Ok(Self {
id: value.id?,
script: value.script?,
start_time: value.start_time?,
})
}
}
pub struct ReportSummary {
age_seconds: Result<i32, String>,
duration_seconds: Result<i32, String>,
host: Result<String, String>,
job: Result<String, String>,
status: Result<i32, String>,
when: Result<chrono::DateTime<chrono::offset::Utc>, String>,
}
impl Default for ReportSummary {
fn default() -> Self {
Self {
age_seconds: Err("no value supplied for age_seconds".to_string()),
duration_seconds: Err("no value supplied for duration_seconds".to_string()),
host: Err("no value supplied for host".to_string()),
job: Err("no value supplied for job".to_string()),
status: Err("no value supplied for status".to_string()),
when: Err("no value supplied for when".to_string()),
}
}
}
impl ReportSummary {
pub fn age_seconds<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.age_seconds = value
.try_into()
.map_err(|e| format!("error converting supplied value for age_seconds: {}", e));
self
}
pub fn duration_seconds<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.duration_seconds = value.try_into().map_err(|e| {
format!(
"error converting supplied value for duration_seconds: {}",
e
)
});
self
}
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn job<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.job = value
.try_into()
.map_err(|e| format!("error converting supplied value for job: {}", e));
self
}
pub fn status<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.status = value
.try_into()
.map_err(|e| format!("error converting supplied value for status: {}", e));
self
}
pub fn when<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.when = value
.try_into()
.map_err(|e| format!("error converting supplied value for when: {}", e));
self
}
}
impl std::convert::TryFrom<ReportSummary> for super::ReportSummary {
type Error = String;
fn try_from(value: ReportSummary) -> Result<Self, Self::Error> {
Ok(Self {
age_seconds: value.age_seconds?,
duration_seconds: value.duration_seconds?,
host: value.host?,
job: value.job?,
status: value.status?,
when: value.when?,
})
}
}
}
}
#[derive(Clone, Debug)]

View File

@ -11,11 +11,23 @@ pub mod types {
pub key: String,
}
impl EnrolBody {
pub fn builder() -> builder::EnrolBody {
builder::EnrolBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct GlobalJobsResult {
pub summary: Vec<ReportSummary>,
}
impl GlobalJobsResult {
pub fn builder() -> builder::GlobalJobsResult {
builder::GlobalJobsResult::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct OutputRecord {
pub msg: String,
@ -23,12 +35,24 @@ pub mod types {
pub time: chrono::DateTime<chrono::offset::Utc>,
}
impl OutputRecord {
pub fn builder() -> builder::OutputRecord {
builder::OutputRecord::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct PingResult {
pub host: String,
pub ok: bool,
}
impl PingResult {
pub fn builder() -> builder::PingResult {
builder::PingResult::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct ReportFinishBody {
pub duration_millis: i32,
@ -37,6 +61,12 @@ pub mod types {
pub id: ReportId,
}
impl ReportFinishBody {
pub fn builder() -> builder::ReportFinishBody {
builder::ReportFinishBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct ReportId {
pub host: String,
@ -46,17 +76,35 @@ pub mod types {
pub uuid: String,
}
impl ReportId {
pub fn builder() -> builder::ReportId {
builder::ReportId::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct ReportOutputBody {
pub id: ReportId,
pub record: OutputRecord,
}
impl ReportOutputBody {
pub fn builder() -> builder::ReportOutputBody {
builder::ReportOutputBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct ReportResult {
pub existed_already: bool,
}
impl ReportResult {
pub fn builder() -> builder::ReportResult {
builder::ReportResult::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct ReportStartBody {
pub id: ReportId,
@ -64,6 +112,12 @@ pub mod types {
pub start_time: chrono::DateTime<chrono::offset::Utc>,
}
impl ReportStartBody {
pub fn builder() -> builder::ReportStartBody {
builder::ReportStartBody::default()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct ReportSummary {
pub age_seconds: i32,
@ -73,6 +127,604 @@ pub mod types {
pub status: i32,
pub when: chrono::DateTime<chrono::offset::Utc>,
}
impl ReportSummary {
pub fn builder() -> builder::ReportSummary {
builder::ReportSummary::default()
}
}
mod builder {
pub struct EnrolBody {
host: Result<String, String>,
key: Result<String, String>,
}
impl Default for EnrolBody {
fn default() -> Self {
Self {
host: Err("no value supplied for host".to_string()),
key: Err("no value supplied for key".to_string()),
}
}
}
impl EnrolBody {
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn key<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.key = value
.try_into()
.map_err(|e| format!("error converting supplied value for key: {}", e));
self
}
}
impl std::convert::TryFrom<EnrolBody> for super::EnrolBody {
type Error = String;
fn try_from(value: EnrolBody) -> Result<Self, Self::Error> {
Ok(Self {
host: value.host?,
key: value.key?,
})
}
}
pub struct GlobalJobsResult {
summary: Result<Vec<super::ReportSummary>, String>,
}
impl Default for GlobalJobsResult {
fn default() -> Self {
Self {
summary: Err("no value supplied for summary".to_string()),
}
}
}
impl GlobalJobsResult {
pub fn summary<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<Vec<super::ReportSummary>>,
T::Error: std::fmt::Display,
{
self.summary = value
.try_into()
.map_err(|e| format!("error converting supplied value for summary: {}", e));
self
}
}
impl std::convert::TryFrom<GlobalJobsResult> for super::GlobalJobsResult {
type Error = String;
fn try_from(value: GlobalJobsResult) -> Result<Self, Self::Error> {
Ok(Self {
summary: value.summary?,
})
}
}
pub struct OutputRecord {
msg: Result<String, String>,
stream: Result<String, String>,
time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
}
impl Default for OutputRecord {
fn default() -> Self {
Self {
msg: Err("no value supplied for msg".to_string()),
stream: Err("no value supplied for stream".to_string()),
time: Err("no value supplied for time".to_string()),
}
}
}
impl OutputRecord {
pub fn msg<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.msg = value
.try_into()
.map_err(|e| format!("error converting supplied value for msg: {}", e));
self
}
pub fn stream<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.stream = value
.try_into()
.map_err(|e| format!("error converting supplied value for stream: {}", e));
self
}
pub fn time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.time = value
.try_into()
.map_err(|e| format!("error converting supplied value for time: {}", e));
self
}
}
impl std::convert::TryFrom<OutputRecord> for super::OutputRecord {
type Error = String;
fn try_from(value: OutputRecord) -> Result<Self, Self::Error> {
Ok(Self {
msg: value.msg?,
stream: value.stream?,
time: value.time?,
})
}
}
pub struct PingResult {
host: Result<String, String>,
ok: Result<bool, String>,
}
impl Default for PingResult {
fn default() -> Self {
Self {
host: Err("no value supplied for host".to_string()),
ok: Err("no value supplied for ok".to_string()),
}
}
}
impl PingResult {
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn ok<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<bool>,
T::Error: std::fmt::Display,
{
self.ok = value
.try_into()
.map_err(|e| format!("error converting supplied value for ok: {}", e));
self
}
}
impl std::convert::TryFrom<PingResult> for super::PingResult {
type Error = String;
fn try_from(value: PingResult) -> Result<Self, Self::Error> {
Ok(Self {
host: value.host?,
ok: value.ok?,
})
}
}
pub struct ReportFinishBody {
duration_millis: Result<i32, String>,
end_time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
exit_status: Result<i32, String>,
id: Result<super::ReportId, String>,
}
impl Default for ReportFinishBody {
fn default() -> Self {
Self {
duration_millis: Err("no value supplied for duration_millis".to_string()),
end_time: Err("no value supplied for end_time".to_string()),
exit_status: Err("no value supplied for exit_status".to_string()),
id: Err("no value supplied for id".to_string()),
}
}
}
impl ReportFinishBody {
pub fn duration_millis<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.duration_millis = value.try_into().map_err(|e| {
format!("error converting supplied value for duration_millis: {}", e)
});
self
}
pub fn end_time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.end_time = value
.try_into()
.map_err(|e| format!("error converting supplied value for end_time: {}", e));
self
}
pub fn exit_status<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.exit_status = value
.try_into()
.map_err(|e| format!("error converting supplied value for exit_status: {}", e));
self
}
pub fn id<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::ReportId>,
T::Error: std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
}
impl std::convert::TryFrom<ReportFinishBody> for super::ReportFinishBody {
type Error = String;
fn try_from(value: ReportFinishBody) -> Result<Self, Self::Error> {
Ok(Self {
duration_millis: value.duration_millis?,
end_time: value.end_time?,
exit_status: value.exit_status?,
id: value.id?,
})
}
}
pub struct ReportId {
host: Result<String, String>,
job: Result<String, String>,
pid: Result<u64, String>,
time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
uuid: Result<String, String>,
}
impl Default for ReportId {
fn default() -> Self {
Self {
host: Err("no value supplied for host".to_string()),
job: Err("no value supplied for job".to_string()),
pid: Err("no value supplied for pid".to_string()),
time: Err("no value supplied for time".to_string()),
uuid: Err("no value supplied for uuid".to_string()),
}
}
}
impl ReportId {
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn job<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.job = value
.try_into()
.map_err(|e| format!("error converting supplied value for job: {}", e));
self
}
pub fn pid<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<u64>,
T::Error: std::fmt::Display,
{
self.pid = value
.try_into()
.map_err(|e| format!("error converting supplied value for pid: {}", e));
self
}
pub fn time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.time = value
.try_into()
.map_err(|e| format!("error converting supplied value for time: {}", e));
self
}
pub fn uuid<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.uuid = value
.try_into()
.map_err(|e| format!("error converting supplied value for uuid: {}", e));
self
}
}
impl std::convert::TryFrom<ReportId> for super::ReportId {
type Error = String;
fn try_from(value: ReportId) -> Result<Self, Self::Error> {
Ok(Self {
host: value.host?,
job: value.job?,
pid: value.pid?,
time: value.time?,
uuid: value.uuid?,
})
}
}
pub struct ReportOutputBody {
id: Result<super::ReportId, String>,
record: Result<super::OutputRecord, String>,
}
impl Default for ReportOutputBody {
fn default() -> Self {
Self {
id: Err("no value supplied for id".to_string()),
record: Err("no value supplied for record".to_string()),
}
}
}
impl ReportOutputBody {
pub fn id<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::ReportId>,
T::Error: std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
pub fn record<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::OutputRecord>,
T::Error: std::fmt::Display,
{
self.record = value
.try_into()
.map_err(|e| format!("error converting supplied value for record: {}", e));
self
}
}
impl std::convert::TryFrom<ReportOutputBody> for super::ReportOutputBody {
type Error = String;
fn try_from(value: ReportOutputBody) -> Result<Self, Self::Error> {
Ok(Self {
id: value.id?,
record: value.record?,
})
}
}
pub struct ReportResult {
existed_already: Result<bool, String>,
}
impl Default for ReportResult {
fn default() -> Self {
Self {
existed_already: Err("no value supplied for existed_already".to_string()),
}
}
}
impl ReportResult {
pub fn existed_already<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<bool>,
T::Error: std::fmt::Display,
{
self.existed_already = value.try_into().map_err(|e| {
format!("error converting supplied value for existed_already: {}", e)
});
self
}
}
impl std::convert::TryFrom<ReportResult> for super::ReportResult {
type Error = String;
fn try_from(value: ReportResult) -> Result<Self, Self::Error> {
Ok(Self {
existed_already: value.existed_already?,
})
}
}
pub struct ReportStartBody {
id: Result<super::ReportId, String>,
script: Result<String, String>,
start_time: Result<chrono::DateTime<chrono::offset::Utc>, String>,
}
impl Default for ReportStartBody {
fn default() -> Self {
Self {
id: Err("no value supplied for id".to_string()),
script: Err("no value supplied for script".to_string()),
start_time: Err("no value supplied for start_time".to_string()),
}
}
}
impl ReportStartBody {
pub fn id<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<super::ReportId>,
T::Error: std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
pub fn script<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.script = value
.try_into()
.map_err(|e| format!("error converting supplied value for script: {}", e));
self
}
pub fn start_time<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.start_time = value
.try_into()
.map_err(|e| format!("error converting supplied value for start_time: {}", e));
self
}
}
impl std::convert::TryFrom<ReportStartBody> for super::ReportStartBody {
type Error = String;
fn try_from(value: ReportStartBody) -> Result<Self, Self::Error> {
Ok(Self {
id: value.id?,
script: value.script?,
start_time: value.start_time?,
})
}
}
pub struct ReportSummary {
age_seconds: Result<i32, String>,
duration_seconds: Result<i32, String>,
host: Result<String, String>,
job: Result<String, String>,
status: Result<i32, String>,
when: Result<chrono::DateTime<chrono::offset::Utc>, String>,
}
impl Default for ReportSummary {
fn default() -> Self {
Self {
age_seconds: Err("no value supplied for age_seconds".to_string()),
duration_seconds: Err("no value supplied for duration_seconds".to_string()),
host: Err("no value supplied for host".to_string()),
job: Err("no value supplied for job".to_string()),
status: Err("no value supplied for status".to_string()),
when: Err("no value supplied for when".to_string()),
}
}
}
impl ReportSummary {
pub fn age_seconds<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.age_seconds = value
.try_into()
.map_err(|e| format!("error converting supplied value for age_seconds: {}", e));
self
}
pub fn duration_seconds<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.duration_seconds = value.try_into().map_err(|e| {
format!(
"error converting supplied value for duration_seconds: {}",
e
)
});
self
}
pub fn host<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.host = value
.try_into()
.map_err(|e| format!("error converting supplied value for host: {}", e));
self
}
pub fn job<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<String>,
T::Error: std::fmt::Display,
{
self.job = value
.try_into()
.map_err(|e| format!("error converting supplied value for job: {}", e));
self
}
pub fn status<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<i32>,
T::Error: std::fmt::Display,
{
self.status = value
.try_into()
.map_err(|e| format!("error converting supplied value for status: {}", e));
self
}
pub fn when<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<chrono::DateTime<chrono::offset::Utc>>,
T::Error: std::fmt::Display,
{
self.when = value
.try_into()
.map_err(|e| format!("error converting supplied value for when: {}", e));
self
}
}
impl std::convert::TryFrom<ReportSummary> for super::ReportSummary {
type Error = String;
fn try_from(value: ReportSummary) -> Result<Self, Self::Error> {
Ok(Self {
age_seconds: value.age_seconds?,
duration_seconds: value.duration_seconds?,
host: value.host?,
job: value.job?,
status: value.status?,
when: value.when?,
})
}
}
}
}
#[derive(Clone, Debug)]

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -934,7 +934,7 @@ pub mod types {
pub name: Name,
pub ncpus: InstanceCpuCount,
///The network interfaces to be created for this instance.
#[serde(default = "instance_create_network_interfaces")]
#[serde(default = "defaults::instance_create_network_interfaces")]
pub network_interfaces: InstanceNetworkInterfaceAttachment,
///User data for instance initialization systems (such as cloud-init).
/// Must be a Base64-encoded string, as specified in RFC 4648 § 4 (+ and
@ -943,10 +943,6 @@ pub mod types {
pub user_data: String,
}
fn instance_create_network_interfaces() -> InstanceNetworkInterfaceAttachment {
InstanceNetworkInterfaceAttachment::Default
}
///Describe the instance's disks at creation time
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
@ -1121,7 +1117,7 @@ pub mod types {
Err("")
.or_else(|_: Self::Error| Ok(Self::V4(Ipv4Net::try_from(value)?)))
.or_else(|_: Self::Error| Ok(Self::V6(Ipv6Net::try_from(value)?)))
.or_else(|_: Self::Error| Err("string conversion failed for all variants"))
.map_err(|_: Self::Error| "string conversion failed for all variants")
}
}
@ -2883,6 +2879,13 @@ pub mod types {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<Name>,
}
mod defaults {
pub(super) fn instance_create_network_interfaces(
) -> super::InstanceNetworkInterfaceAttachment {
super::InstanceNetworkInterfaceAttachment::Default
}
}
}
#[derive(Clone, Debug)]

View File

@ -5,16 +5,6 @@ pub mod types {
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use std::convert::TryFrom;
mod defaults {
pub(super) fn default_u64<T, const V: u64>() -> T
where
T: std::convert::TryFrom<u64>,
<T as std::convert::TryFrom<u64>>::Error: std::fmt::Debug,
{
T::try_from(V).unwrap()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BodyWithDefaults {
#[serde(rename = "forty-two", default = "defaults::default_u64::<u32, 42>")]
@ -32,6 +22,16 @@ pub mod types {
pub message: String,
pub request_id: String,
}
mod defaults {
pub(super) fn default_u64<T, const V: u64>() -> T
where
T: std::convert::TryFrom<u64>,
<T as std::convert::TryFrom<u64>>::Error: std::fmt::Debug,
{
T::try_from(V).unwrap()
}
}
}
#[derive(Clone, Debug)]

View File

@ -1,4 +1,4 @@
// Copyright 2021 Oxide Computer Company
// Copyright 2022 Oxide Computer Company
use std::{fs::File, path::PathBuf};
@ -25,7 +25,7 @@ fn verify_apis(openapi_file: &str) {
GenerationSettings::default()
.with_interface(InterfaceStyle::Builder)
.with_tag(TagStyle::Merged)
.with_derive(quote::quote! {JsonSchema}),
.with_derive("JsonSchema"),
);
let output = generator.generate_text_normalize_comments(&spec).unwrap();
expectorate::assert_contents(

View File

@ -70,5 +70,12 @@ mod builder_tagged {
.instance_name("instance")
.stream();
let _ = stream.collect::<Vec<_>>();
// client
// .instance_create()
// .organization_name("org")
// .project_name("project")
// .body(types::InstanceCreate::builder())
// .send();
}
}