-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #506 from paperclip-rs/draft-poc
- Loading branch information
Showing
331 changed files
with
32,735 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Publish Bins | ||
on: | ||
release: | ||
types: [published] | ||
jobs: | ||
paperclip-bins: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- os: ubuntu-latest | ||
target: x86_64-unknown-linux-musl | ||
- os: ubuntu-latest | ||
target: aarch64-unknown-linux-musl | ||
- os: macos-14 | ||
target: x86_64-apple-darwin | ||
- os: macos-13 | ||
target: aarch64-apple-darwin | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
submodules: recursive | ||
- name: Install Rust | ||
uses: dtolnay/rust-toolchain@stable | ||
with: | ||
targets: ${{ matrix.target }} | ||
- name: Build | ||
uses: actions-rs/cargo@v1 | ||
with: | ||
use-cross: true | ||
command: build | ||
args: --bins --verbose --release --features "cli" -p paperclip -p paperclip-ng --target ${{ matrix.target }} | ||
- name: Archive | ||
shell: bash | ||
run: | | ||
tar -czf paperclip-${{ matrix.target }}.tar.gz LICENSE-APACHE LICENSE-MIT -C ./target/${{ matrix.target }}/release/ paperclip paperclip-ng | ||
- name: Publish | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
run: | | ||
gh release upload "${{ github.event.release.tag_name }}" --clobber *.tar.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "paperclip-ng" | ||
version = "0.1.0" | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
keywords = [ "openapi", "openapiv3", "cli", "codegen" ] | ||
description = "Experimental OpenAPI V3.0.3 Code Generator" | ||
homepage = "https://github.com/paperclip-rs/paperclip" | ||
repository = "https://github.com/paperclip-rs/paperclip" | ||
|
||
[[bin]] | ||
name = "paperclip-ng" | ||
path = "src/bin/cli/main.rs" | ||
|
||
[dependencies] | ||
ramhorns = { version = "1.0", default-features = false, features = ["indexes"] } | ||
ramhorns-derive = { version = "1.0" } | ||
openapiv3-paper = { version = "2.0" } | ||
heck = { version = "0.4" } | ||
itertools = { version = "0.10" } | ||
|
||
env_logger = "0.8" | ||
log = { version = "0.4", features = ["kv_unstable"] } | ||
structopt = { version = "0.3" } | ||
serde_json = "1.0" | ||
serde_yaml = "0.9" | ||
thiserror = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
macro_rules! impl_err_from { | ||
($err:ident :: $type:ty > $variant:ident) => { | ||
impl From<$type> for $err { | ||
fn from(s: $type) -> Self { | ||
$err::$variant(s) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// Global error which encapsulates all related errors. | ||
#[derive(Debug, thiserror::Error)] | ||
pub(crate) enum Error { | ||
/// The given directory cannot be used for generating code. | ||
#[error("Cannot generate code in the given directory")] | ||
InvalidCodegenDirectory, | ||
/// I/O errors. | ||
#[error("I/O error: {}", _0)] | ||
Io(std::io::Error), | ||
/// JSON coding errors. | ||
#[error("JSON error: {}", _0)] | ||
Json(serde_json::Error), | ||
/// YAML coding errors. | ||
#[error("YAML error: {}", _0)] | ||
Yaml(serde_yaml::Error), | ||
} | ||
|
||
impl_err_from!(Error::std::io::Error > Io); | ||
impl_err_from!(Error::serde_json::Error > Json); | ||
impl_err_from!(Error::serde_yaml::Error > Yaml); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use heck::ToSnakeCase; | ||
use std::{ | ||
fs::{self, File}, | ||
io::Read, | ||
path::PathBuf, | ||
}; | ||
use structopt::StructOpt; | ||
|
||
mod error; | ||
use error::Error; | ||
|
||
/// Deserialize the schema from the given reader. Currently, this only supports | ||
/// JSON and YAML formats. | ||
fn from_reader_v3<R>(mut reader: R) -> Result<openapiv3::OpenAPI, Error> | ||
where | ||
R: Read, | ||
{ | ||
let mut buf = [b' ']; | ||
while buf[0].is_ascii_whitespace() { | ||
reader.read_exact(&mut buf)?; | ||
} | ||
let reader = buf.as_ref().chain(reader); | ||
|
||
Ok(if buf[0] == b'{' { | ||
serde_json::from_reader::<_, openapiv3::OpenAPI>(reader)? | ||
} else { | ||
serde_yaml::from_reader::<_, openapiv3::OpenAPI>(reader)? | ||
}) | ||
} | ||
fn parse_spec_v3(s: &str) -> Result<openapiv3::OpenAPI, Error> { | ||
let fd = File::open(s)?; | ||
from_reader_v3(fd) | ||
} | ||
|
||
#[derive(Debug, StructOpt)] | ||
struct Opt { | ||
/// Path to OpenAPI spec in JSON/YAML format (also supports publicly accessible URLs). | ||
#[structopt(long)] | ||
spec: std::path::PathBuf, | ||
/// Output directory to write code (default: current working directory). | ||
#[structopt(short = "o", long = "out", parse(from_os_str))] | ||
output: Option<PathBuf>, | ||
/// Don't Render models. | ||
#[structopt(long)] | ||
no_models: bool, | ||
/// Don't Render operations. | ||
#[structopt(long)] | ||
no_ops: bool, | ||
/// Name of the crate. If this is not specified, then the name of the | ||
/// working directory is assumed to be crate name. | ||
#[structopt(long = "name")] | ||
pub name: Option<String>, | ||
/// The Version of the crate. | ||
#[structopt(long = "version", default_value = "0.1.0")] | ||
pub version: String, | ||
/// The Edition of the crate. | ||
#[structopt(long = "edition", default_value = "2018")] | ||
pub edition: String, | ||
/// Use custom templates (mustache) files, rather than the builtin ones. | ||
/// The root dir in this path must be the template name, example: default. | ||
#[structopt(short = "t", long = "templates", parse(from_os_str))] | ||
templates: Option<PathBuf>, | ||
} | ||
|
||
fn parse_args_and_run() -> Result<(), Error> { | ||
let opt: Opt = Opt::from_args(); | ||
|
||
if let Some(o) = &opt.output { | ||
fs::create_dir_all(o)?; | ||
} | ||
|
||
let spec = parse_spec_v3(opt.spec.to_string_lossy().as_ref())?; | ||
let name = opt.name.map(Ok::<String, Error>).unwrap_or_else(|| { | ||
Ok(fs::canonicalize(std::path::Path::new("."))? | ||
.file_name() | ||
.ok_or(Error::InvalidCodegenDirectory)? | ||
.to_string_lossy() | ||
.into_owned() | ||
.to_snake_case()) | ||
})?; | ||
let info = paperclip_ng::v3_03::PackageInfo { | ||
libname: name.to_snake_case(), | ||
name, | ||
version: opt.version, | ||
edition: opt.edition, | ||
}; | ||
|
||
paperclip_ng::v3_03::OpenApiV3::new(spec, opt.templates, opt.output, info)? | ||
.run(!opt.no_models, !opt.no_ops)?; | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
env_logger::init(); | ||
if let Err(e) = parse_args_and_run() { | ||
eprintln!("{}", e); | ||
std::process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod v3; | ||
|
||
pub mod v3_03 { | ||
pub use super::v3::{OpenApiV3, PackageInfo}; | ||
} |
Oops, something went wrong.