minor cleanup (#8)

This commit is contained in:
Adam Leventhal 2021-10-09 08:38:01 -07:00 committed by GitHub
parent 273120683b
commit 3fe0029f5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 129 deletions

34
.github/workflows/rust.yml vendored Normal file
View File

@ -0,0 +1,34 @@
#
# Configuration for GitHub-based CI, based on the stock GitHub Rust config.
#
name: Build
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
check-style:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Report cargo version
run: cargo --version
- name: Report rustfmt version
run: cargo fmt -- --version
- name: Check style
run: cargo fmt -- --check
build-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-18.04, windows-2019, macos-10.15 ]
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --tests --verbose
- name: Run tests
run: cargo test --verbose

15
Cargo.lock generated
View File

@ -176,7 +176,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex", "regex",
"rustfmt-wrapper", "rustfmt-wrapper 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"schemars", "schemars",
"serde", "serde",
"serde_json", "serde_json",
@ -267,6 +267,17 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rustfmt-wrapper"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7733577fb5b13c8b256232e7ca84aa424f915efae6ec980082d60a03f99da3f8"
dependencies = [
"tempfile",
"thiserror",
"toolchain_find",
]
[[package]] [[package]]
name = "rustfmt-wrapper" name = "rustfmt-wrapper"
version = "0.1.0" version = "0.1.0"
@ -451,7 +462,7 @@ name = "typify"
version = "0.0.1" version = "0.0.1"
source = "git+https://github.com/oxidecomputer/typify#b662d84d82feb20f81ff2cd62c1a0af8ed77515a" source = "git+https://github.com/oxidecomputer/typify#b662d84d82feb20f81ff2cd62c1a0af8ed77515a"
dependencies = [ dependencies = [
"rustfmt-wrapper", "rustfmt-wrapper 0.1.0 (git+https://github.com/oxidecomputer/typify)",
"typify-impl", "typify-impl",
"typify-macro", "typify-macro",
] ]

View File

@ -14,7 +14,7 @@ openapiv3 = "1.0.0-beta.2"
proc-macro2 = "1.0.29" proc-macro2 = "1.0.29"
quote = "1.0.9" quote = "1.0.9"
regex = "1.5.4" regex = "1.5.4"
rustfmt-wrapper = { git = "https://github.com/oxidecomputer/typify" } rustfmt-wrapper = "0.1.0"
schemars = "0.8.5" schemars = "0.8.5"
serde = { version = "1", features = [ "derive" ] } serde = { version = "1", features = [ "derive" ] }
serde_json = "1.0.68" serde_json = "1.0.68"

View File

@ -171,7 +171,6 @@ where
} }
trait ParameterDataExt { trait ParameterDataExt {
fn render_type(&self) -> Result<String>;
fn schema(&self) -> Result<&openapiv3::ReferenceOr<openapiv3::Schema>>; fn schema(&self) -> Result<&openapiv3::ReferenceOr<openapiv3::Schema>>;
} }
@ -182,83 +181,6 @@ impl ParameterDataExt for openapiv3::ParameterData {
x => bail!("XXX param format {:#?}", x), x => bail!("XXX param format {:#?}", x),
} }
} }
fn render_type(&self) -> Result<String> {
use openapiv3::{SchemaKind, Type};
Ok(match &self.format {
openapiv3::ParameterSchemaOrContent::Schema(s) => {
let s = s.item().context("parameter data render type")?;
match &s.schema_kind {
SchemaKind::Type(Type::String(st)) => {
if !st.format.is_empty() {
bail!("XXX format");
}
if st.pattern.is_some() {
bail!("XXX pattern");
}
if !st.enumeration.is_empty() {
bail!("XXX enumeration");
}
if st.min_length.is_some() || st.max_length.is_some() {
bail!("XXX min/max length");
}
"&str".to_string()
}
SchemaKind::Type(Type::Integer(it)) => {
let mut uint;
let width;
use openapiv3::VariantOrUnknownOrEmpty::Unknown;
if let Unknown(f) = &it.format {
match f.as_str() {
"uint" | "uint32" => {
uint = true;
width = 32;
}
"uint64" => {
uint = true;
width = 32;
}
f => bail!("XXX unknown integer format {}", f),
}
} else {
bail!("XXX format {:?}", it.format);
}
if it.multiple_of.is_some() {
bail!("XXX multiple_of");
}
if it.exclusive_minimum || it.exclusive_maximum {
bail!("XXX exclusive");
}
if let Some(min) = it.minimum {
if min == 0 {
uint = true;
} else {
bail!("XXX invalid minimum: {}", min);
}
}
if it.maximum.is_some() {
bail!("XXX maximum");
}
if !it.enumeration.is_empty() {
bail!("XXX enumeration");
}
if uint {
format!("u{}", width)
} else {
format!("i{}", width)
}
}
x => bail!("unexpected type {:#?}", x),
}
}
x => bail!("XXX param format {:#?}", x),
})
}
} }
trait ExtractJsonMediaType { trait ExtractJsonMediaType {
@ -443,54 +365,6 @@ impl<T> ReferenceOrExt<T> for openapiv3::ReferenceOr<T> {
} }
} }
#[allow(dead_code)]
#[derive(Debug, PartialEq)]
enum TypeDetails {
Unknown,
Basic,
Array(TypeId),
Optional(TypeId),
/*
* Object property names are sorted lexicographically to ensure a stable
* order in the generated code.
*/
Object(BTreeMap<String, TypeId>),
NewType(TypeId),
Enumeration(Vec<String>),
/*
* A map with string keys and values of a specific type:
*/
Dictionary(TypeId),
}
#[derive(Debug)]
struct TypeEntry {
id: TypeId,
name: Option<String>,
details: TypeDetails,
}
#[derive(Debug, Eq, Clone)]
struct TypeId(u64);
impl PartialOrd for TypeId {
fn partial_cmp(&self, other: &TypeId) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for TypeId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl PartialEq for TypeId {
fn eq(&self, other: &TypeId) -> bool {
self.0 == other.0
}
}
enum ParamType { enum ParamType {
Path, Path,
Query, Query,