Skip to content

Commit

Permalink
Require docs for public members
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Dec 18, 2023
1 parent 3685587 commit f2e3142
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions progenitor-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2022 Oxide Computer Company

//! Support for generated clients.
#![deny(missing_docs)]

mod progenitor_client;

pub use crate::progenitor_client::*;
Expand Down
1 change: 1 addition & 0 deletions progenitor-client/src/progenitor_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ const PATH_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
.add(b'%');

#[doc(hidden)]
/// Percent encode input string.
pub fn encode_path(pc: &str) -> String {
percent_encoding::utf8_percent_encode(pc, PATH_SET).to_string()
}
Expand Down
35 changes: 33 additions & 2 deletions progenitor-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2023 Oxide Computer Company

//! Core implementation for the progenitor OpenAPI client generator.
#![deny(missing_docs)]

use std::collections::{HashMap, HashSet};

use openapiv3::OpenAPI;
Expand All @@ -21,6 +25,7 @@ mod template;
mod to_schema;
mod util;

#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum Error {
#[error("unexpected value type {0}: {1}")]
Expand All @@ -37,15 +42,18 @@ pub enum Error {
InternalError(String),
}

#[allow(missing_docs)]
pub type Result<T> = std::result::Result<T, Error>;

/// OpenAPI generator.
pub struct Generator {
type_space: TypeSpace,
settings: GenerationSettings,
uses_futures: bool,
uses_websockets: bool,
}

/// Settings for [Generator].
#[derive(Default, Clone)]
pub struct GenerationSettings {
interface: InterfaceStyle,
Expand All @@ -60,9 +68,12 @@ pub struct GenerationSettings {
convert: Vec<(schemars::schema::SchemaObject, String, Vec<TypeImpl>)>,
}

/// Style of generated client.
#[derive(Clone, Deserialize, PartialEq, Eq)]
pub enum InterfaceStyle {
/// Use positional style.
Positional,
/// Use builder style.
Builder,
}

Expand All @@ -72,9 +83,12 @@ impl Default for InterfaceStyle {
}
}

/// Style for using the OpenAPI tags when generating names in the client.
#[derive(Clone, Deserialize)]
pub enum TagStyle {
/// Merge tags to create names in the generated client.
Merged,
/// Use each tag name to create separate names in the generated client.
Separate,
}

Expand All @@ -85,40 +99,49 @@ impl Default for TagStyle {
}

impl GenerationSettings {
/// Create new generator settings with default values.
pub fn new() -> Self {
Self::default()
}

/// Set the [InterfaceStyle].
pub fn with_interface(&mut self, interface: InterfaceStyle) -> &mut Self {
self.interface = interface;
self
}

/// Set the [TagStyle].
pub fn with_tag(&mut self, tag: TagStyle) -> &mut Self {
self.tag = tag;
self
}

/// Client inner type available to pre and post hooks.
pub fn with_inner_type(&mut self, inner_type: TokenStream) -> &mut Self {
self.inner_type = Some(inner_type);
self
}

/// Hook invoked before issuing the HTTP request.
pub fn with_pre_hook(&mut self, pre_hook: TokenStream) -> &mut Self {
self.pre_hook = Some(pre_hook);
self
}

/// Hook invoked prior to receiving the HTTP response.
pub fn with_post_hook(&mut self, post_hook: TokenStream) -> &mut Self {
self.post_hook = Some(post_hook);
self
}

/// Additional derive macros applied to generated types.
pub fn with_derive(&mut self, derive: impl ToString) -> &mut Self {
self.extra_derives.push(derive.to_string());
self
}

/// Modify a type with the given name.
/// See [typify::TypeSpaceSettings::with_patch].
pub fn with_patch<S: AsRef<str>>(
&mut self,
type_name: S,
Expand All @@ -129,6 +152,8 @@ impl GenerationSettings {
self
}

/// Replace a referenced type with a named type.
/// See [typify::TypeSpaceSettings::with_replacement].
pub fn with_replacement<
TS: ToString,
RS: ToString,
Expand All @@ -146,6 +171,8 @@ impl GenerationSettings {
self
}

/// Replace a given schema with a named type.
/// See [typify::TypeSpaceSettings::with_conversion].
pub fn with_conversion<S: ToString, I: Iterator<Item = TypeImpl>>(
&mut self,
schema: schemars::schema::SchemaObject,
Expand All @@ -172,6 +199,7 @@ impl Default for Generator {
}

impl Generator {
/// Create a new generator with default values.
pub fn new(settings: &GenerationSettings) -> Self {
let mut type_settings = TypeSpaceSettings::default();
type_settings
Expand Down Expand Up @@ -210,6 +238,7 @@ impl Generator {
}
}

/// Emit a [TokenStream] containing the generated client code.
pub fn generate_tokens(&mut self, spec: &OpenAPI) -> Result<TokenStream> {
validate_openapi(spec)?;

Expand Down Expand Up @@ -509,22 +538,24 @@ impl Generator {
Ok(out)
}

// TODO deprecate?
/// Get the [TypeSpace] for schemas present in the OpenAPI specification.
pub fn get_type_space(&self) -> &TypeSpace {
&self.type_space
}

/// Whether the generated client needs to use additional crates to support futures.
pub fn uses_futures(&self) -> bool {
self.uses_futures
}

/// Whether the generated client needs to use additional crates to support websockets.
pub fn uses_websockets(&self) -> bool {
self.uses_websockets
}
}

/// Add newlines after end-braces at <= two levels of indentation.
pub fn space_out_items(content: String) -> Result<String> {
// Add newlines after end-braces at <= two levels of indentation.
Ok(if cfg!(not(windows)) {
let regex = regex::Regex::new(r#"(\n\s*})(\n\s{0,8}[^} ])"#).unwrap();
regex.replace_all(&content, "$1\n$2").to_string()
Expand Down
4 changes: 4 additions & 0 deletions progenitor-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2022 Oxide Computer Company

//! Macros for the progenitor OpenAPI client generator.
#![deny(missing_docs)]

use std::{
collections::HashMap,
fmt::Display,
Expand Down
2 changes: 2 additions & 0 deletions progenitor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//! For details see the [repo
//! README](https://github.com/oxidecomputer/progenitor/blob/main/README.md)
#![deny(missing_docs)]

pub use progenitor_client;
pub use progenitor_impl::Error;
pub use progenitor_impl::GenerationSettings;
Expand Down

0 comments on commit f2e3142

Please sign in to comment.