Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bytes::Bytes for binary #345

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-345.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: break
break:
description: The Conjure `binary` type is now backed by `bytes::Bytes`.
links:
- https://github.com/palantir/conjure-rust/pull/345
20 changes: 10 additions & 10 deletions conjure-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl Context {
}
}
PrimitiveType::Safelong => quote!(conjure_object::SafeLong),
PrimitiveType::Binary => quote!(conjure_object::ByteBuf),
PrimitiveType::Binary => quote!(conjure_object::Bytes),
PrimitiveType::Any => quote!(conjure_object::Any),
PrimitiveType::Boolean => quote!(bool),
PrimitiveType::Uuid => quote!(conjure_object::Uuid),
Expand Down Expand Up @@ -414,7 +414,7 @@ impl Context {
PrimitiveType::Integer => quote!(i32),
PrimitiveType::Double => quote!(f64),
PrimitiveType::Safelong => quote!(conjure_object::SafeLong),
PrimitiveType::Binary => quote!(&[u8]),
PrimitiveType::Binary => quote!(&conjure_object::Bytes),
PrimitiveType::Any => quote!(&conjure_object::Any),
PrimitiveType::Boolean => quote!(bool),
PrimitiveType::Uuid => quote!(conjure_object::Uuid),
Expand Down Expand Up @@ -466,8 +466,10 @@ impl Context {
match def {
Type::Primitive(def) => match *def {
PrimitiveType::String => quote!(&*#value),
PrimitiveType::Binary => quote!(&**#value),
PrimitiveType::Any | PrimitiveType::Rid | PrimitiveType::Bearertoken => {
PrimitiveType::Any
| PrimitiveType::Rid
| PrimitiveType::Bearertoken
| PrimitiveType::Binary => {
quote!(&#value)
}
PrimitiveType::Datetime
Expand Down Expand Up @@ -524,10 +526,9 @@ impl Context {
}
PrimitiveType::Binary => {
let into = self.into_ident(this_type);
let vec = self.vec_ident(this_type);
SetterBounds::Generic {
argument_bound: quote!(#into<#vec<u8>>),
assign_rhs: quote!(conjure_object::ByteBuf::from(#value_ident)),
argument_bound: quote!(#into<conjure_object::Bytes>),
assign_rhs: quote!(#value_ident.into()),
}
}
PrimitiveType::Any => SetterBounds::Generic {
Expand Down Expand Up @@ -645,10 +646,9 @@ impl Context {
}
PrimitiveType::Binary => {
let into = self.into_ident(this_type);
let vec = self.vec_ident(this_type);
CollectionSetterBounds::Generic {
argument_bound: quote!(#into<#vec<u8>>),
assign_rhs: quote!(conjure_object::ByteBuf::from(#value_ident)),
argument_bound: quote!(#into<conjure_object::Bytes>),
assign_rhs: quote!(#value_ident.into()),
}
}
PrimitiveType::Any => CollectionSetterBounds::Generic {
Expand Down
14 changes: 7 additions & 7 deletions conjure-codegen/src/example_types/product/aliased_binary.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions conjure-codegen/src/example_types/product/any_example.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions conjure-codegen/src/example_types/product/binary_example.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion conjure-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! | `integer` | `i32` |
//! | `double` | `f64` |
//! | `safelong` | `conjure_object::SafeLong` |
//! | `binary` | `serde_bytes::ByteBuf` |
//! | `binary` | `bytes::Bytes` |
//! | `any` | `conjure_object::Any` |
//! | `boolean` | `bool` |
//! | `uuid` | `uuid::Uuid` |
Expand Down
2 changes: 1 addition & 1 deletion conjure-object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/palantir/conjure-rust"
readme = "../README.md"

[dependencies]
bytes = { version = "1.0", features = ["serde"] }
base64 = "0.22"
chrono = { version = "0.4.26", default-features = false, features = ["clock", "std", "serde"] }
educe = { version = "0.5", default-features = false, features = [
Expand All @@ -22,7 +23,6 @@ lazy_static = "1.0"
ordered-float = { version = "4", features = ["serde"] }
regex = { version = "1.3", default-features = false, features = ["std"] }
serde = "1.0"
serde_bytes = "0.11"
uuid = { version = "1.1", features = ["serde"] }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion conjure-object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
//! This crate consists of reexports and definitions of the Rust types that correspond to Conjure types. It is a
//! required dependency of crates which contain Conjure-generated code.

pub use bytes::{self, Bytes};
pub use chrono::{self, DateTime, Utc};
pub use serde;
pub use serde_bytes::{self, ByteBuf};
pub use uuid::{self, Uuid};

#[doc(inline)]
Expand Down
16 changes: 5 additions & 11 deletions conjure-object/src/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use base64::display::Base64Display;
use base64::engine::general_purpose::STANDARD;
use base64::{DecodeError, Engine};
use bytes::Bytes;
use chrono::format::{Fixed, Item, ParseError};
use chrono::{DateTime, Utc};
use serde_bytes::ByteBuf;
use std::error::Error;
use std::f64;
use std::fmt;
Expand Down Expand Up @@ -97,13 +97,7 @@ impl Plain for [u8] {
}
}

impl Plain for Vec<u8> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
Plain::fmt(&**self, fmt)
}
}

impl Plain for ByteBuf {
impl Plain for Bytes {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
Plain::fmt(&**self, fmt)
}
Expand Down Expand Up @@ -167,13 +161,13 @@ as_from_str!(SafeLong);
as_from_str!(String);
as_from_str!(Uuid);

impl FromPlain for ByteBuf {
impl FromPlain for Bytes {
type Err = ParseBinaryError;

#[inline]
fn from_plain(s: &str) -> Result<ByteBuf, ParseBinaryError> {
fn from_plain(s: &str) -> Result<Self, ParseBinaryError> {
let buf = STANDARD.decode(s).map_err(ParseBinaryError)?;
Ok(ByteBuf::from(buf))
Ok(Bytes::from(buf))
}
}

Expand Down
14 changes: 7 additions & 7 deletions example-api/src/product/aliased_binary.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions example-api/src/product/any_example.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading