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

Add no_std support #1

Merged
merged 9 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
8 changes: 5 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ jobs:
with:
components: clippy
- name: Clippy
uses: actions-rs/clippy-check@v1
uses: giraffate/clippy-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features --all-targets
clippy_flags: ''
reporter: 'github-pr-check'

publish-dry-run:
name: Publish (dry run)
Expand Down Expand Up @@ -68,6 +68,8 @@ jobs:
override: true
- name: Install cargo-nextest
uses: taiki-e/install-action@nextest
- name: Install Protoc
uses: arduino/setup-protoc@v2
- name: Build tests
uses: actions-rs/cargo@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion pbjson-build/src/escape.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Contains code to escape strings to avoid collisions with reserved Rust keywords
//! Contains code to escape strings to avoid collisions with reserved Rust keywords

pub fn escape_ident(mut ident: String) -> String {
// Copied from prost-build::ident
Expand Down
4 changes: 2 additions & 2 deletions pbjson-build/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn write_serialize_start<W: Write>(indent: usize, rust_type: &str, writer: &mut
writer,
r#"{indent}impl serde::Serialize for {rust_type} {{
{indent} #[allow(deprecated)]
{indent} fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
{indent} fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
{indent} where
{indent} S: serde::Serializer,
{indent} {{"#,
Expand All @@ -62,7 +62,7 @@ fn write_deserialize_start<W: Write>(indent: usize, rust_type: &str, writer: &mu
writer,
r#"{indent}impl<'de> serde::Deserialize<'de> for {rust_type} {{
{indent} #[allow(deprecated)]
{indent} fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
{indent} fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
{indent} where
{indent} D: serde::Deserializer<'de>,
{indent} {{"#,
Expand Down
12 changes: 6 additions & 6 deletions pbjson-build/src/generator/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ fn write_visitor<W: Write>(
{indent}impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {{
{indent} type Value = {rust_type};

{indent} fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
{indent} fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {{
{indent} write!(formatter, "expected one of: {{:?}}", &FIELDS)
{indent} }}

{indent} fn visit_i64<E>(self, v: i64) -> std::result::Result<Self::Value, E>
{indent} fn visit_i64<E>(self, v: i64) -> core::result::Result<Self::Value, E>
{indent} where
{indent} E: serde::de::Error,
{indent} {{
{indent} use std::convert::TryFrom;
{indent} use core::convert::TryFrom;
{indent} i32::try_from(v)
{indent} .ok()
{indent} .and_then({rust_type}::from_i32)
Expand All @@ -113,11 +113,11 @@ fn write_visitor<W: Write>(
{indent} }})
{indent} }}

{indent} fn visit_u64<E>(self, v: u64) -> std::result::Result<Self::Value, E>
{indent} fn visit_u64<E>(self, v: u64) -> core::result::Result<Self::Value, E>
{indent} where
{indent} E: serde::de::Error,
{indent} {{
{indent} use std::convert::TryFrom;
{indent} use core::convert::TryFrom;
{indent} i32::try_from(v)
{indent} .ok()
{indent} .and_then({rust_type}::from_i32)
Expand All @@ -126,7 +126,7 @@ fn write_visitor<W: Write>(
{indent} }})
{indent} }}

{indent} fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
{indent} fn visit_str<E>(self, value: &str) -> core::result::Result<Self::Value, E>
{indent} where
{indent} E: serde::de::Error,
{indent} {{"#,
Expand Down
40 changes: 20 additions & 20 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn write_decode_variant<W: Write>(
writeln!(writer, "{}::from_i32({})", resolver.rust_type(path), value)?;
write!(
writer,
"{}.ok_or_else(|| serde::ser::Error::custom(format!(\"Invalid variant {{}}\", {})))",
"{}.ok_or_else(|| serde::ser::Error::custom(::alloc::format!(\"Invalid variant {{}}\", {})))",
Indent(indent),
value
)
Expand Down Expand Up @@ -261,7 +261,7 @@ fn write_serialize_variable<W: Write>(
writeln!(writer)?;
write!(
writer,
"{}}}).collect::<Result<Vec<_>, _>>()",
"{}}}).collect::<Result<::alloc::vec::Vec<_>, _>>()",
Indent(indent + 1)
)
}
Expand All @@ -287,7 +287,7 @@ fn write_serialize_variable<W: Write>(
{
writeln!(
writer,
"{}let v: std::collections::HashMap<_, _> = {}.iter()",
"{}let v: alloc::collections::BTreeMap<_, _> = {}.iter()",
Indent(indent),
variable.raw
)?;
Expand All @@ -296,7 +296,7 @@ fn write_serialize_variable<W: Write>(
FieldType::Scalar(ScalarType::I64) | FieldType::Scalar(ScalarType::U64) => {
writeln!(
writer,
"{}.map(|(k, v)| (k, v.to_string())).collect();",
"{}.map(|(k, v)| (k, ::alloc::string::ToString::to_string(v))).collect();",
Indent(indent + 1)
)?;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ fn write_serialize_scalar_variable<W: Write>(
writer: &mut W,
) -> Result<()> {
let conversion = match scalar {
ScalarType::I64 | ScalarType::U64 => "ToString::to_string",
ScalarType::I64 | ScalarType::U64 => "::alloc::string::ToString::to_string",
ScalarType::Bytes => "pbjson::private::base64::encode",
_ => {
return writeln!(
Expand All @@ -366,7 +366,7 @@ fn write_serialize_scalar_variable<W: Write>(
FieldModifier::Repeated => {
writeln!(
writer,
"{}struct_ser.serialize_field(\"{}\", &{}.iter().map({}).collect::<Vec<_>>())?;",
"{}struct_ser.serialize_field(\"{}\", &{}.iter().map({}).collect::<::alloc::vec::Vec<_>>())?;",
Indent(indent),
field_name,
variable.raw,
Expand Down Expand Up @@ -513,11 +513,11 @@ fn write_deserialize_message<W: Write>(
r#"{indent}impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {{
{indent} type Value = {rust_type};

{indent} fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
{indent} fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {{
{indent} formatter.write_str("struct {name}")
{indent} }}

{indent} fn visit_map<V>(self, mut map: V) -> std::result::Result<{rust_type}, V::Error>
{indent} fn visit_map<V>(self, mut map: V) -> core::result::Result<{rust_type}, V::Error>
{indent} where
{indent} V: serde::de::MapAccess<'de>,
{indent} {{"#,
Expand Down Expand Up @@ -689,7 +689,7 @@ fn write_deserialize_field_name<W: Write>(
writeln!(
writer,
r#"{indent}impl<'de> serde::Deserialize<'de> for GeneratedField {{
{indent} fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>
{indent} fn deserialize<D>(deserializer: D) -> core::result::Result<GeneratedField, D::Error>
{indent} where
{indent} D: serde::Deserializer<'de>,
{indent} {{
Expand All @@ -698,12 +698,12 @@ fn write_deserialize_field_name<W: Write>(
{indent} impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {{
{indent} type Value = GeneratedField;

{indent} fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
{indent} fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {{
{indent} write!(formatter, "expected one of: {{:?}}", &FIELDS)
{indent} }}

{indent} #[allow(unused_variables)]
{indent} fn visit_str<E>(self, value: &str) -> std::result::Result<GeneratedField, E>
{indent} fn visit_str<E>(self, value: &str) -> core::result::Result<GeneratedField, E>
{indent} where
{indent} E: serde::de::Error,
{indent} {{"#,
Expand Down Expand Up @@ -837,7 +837,7 @@ fn write_deserialize_field<W: Write>(
Some(deserializer) => {
write!(
writer,
"map.next_value::<::std::option::Option<{}>>()?.map(|x| {}::{}(x.0))",
"map.next_value::<::core::option::Option<{}>>()?.map(|x| {}::{}(x.0))",
deserializer,
resolver.rust_type(&one_of.path),
field.rust_type_name()
Expand All @@ -846,7 +846,7 @@ fn write_deserialize_field<W: Write>(
None => {
write!(
writer,
"map.next_value::<::std::option::Option<_>>()?.map({}::{})",
"map.next_value::<::core::option::Option<_>>()?.map({}::{})",
resolver.rust_type(&one_of.path),
field.rust_type_name()
)?;
Expand All @@ -855,15 +855,15 @@ fn write_deserialize_field<W: Write>(
FieldType::Enum(path) => {
write!(
writer,
"map.next_value::<::std::option::Option<{}>>()?.map(|x| {}::{}(x as i32))",
"map.next_value::<::core::option::Option<{}>>()?.map(|x| {}::{}(x as i32))",
resolver.rust_type(path),
resolver.rust_type(&one_of.path),
field.rust_type_name()
)?;
}
FieldType::Message(_) => writeln!(
writer,
"map.next_value::<::std::option::Option<_>>()?.map({}::{})",
"map.next_value::<::core::option::Option<_>>()?.map({}::{})",
resolver.rust_type(&one_of.path),
field.rust_type_name()
)?,
Expand All @@ -878,14 +878,14 @@ fn write_deserialize_field<W: Write>(
FieldModifier::Optional => {
write!(
writer,
"map.next_value::<::std::option::Option<{}>>()?.map(|x| x as i32)",
"map.next_value::<::core::option::Option<{}>>()?.map(|x| x as i32)",
resolver.rust_type(path)
)?;
}
FieldModifier::Repeated => {
write!(
writer,
"Some(map.next_value::<Vec<{}>>()?.into_iter().map(|x| x as i32).collect())",
"Some(map.next_value::<::alloc::vec::Vec<{}>>()?.into_iter().map(|x| x as i32).collect())",
resolver.rust_type(path)
)?;
}
Expand All @@ -903,7 +903,7 @@ fn write_deserialize_field<W: Write>(
match btree_map {
true => write!(
writer,
"{}map.next_value::<std::collections::BTreeMap<",
"{}map.next_value::<alloc::collections::BTreeMap<",
Indent(indent + 2),
)?,
false => write!(
Expand Down Expand Up @@ -1012,15 +1012,15 @@ fn write_encode_scalar_field<W: Write>(
FieldModifier::Optional => {
writeln!(
writer,
"{}map.next_value::<::std::option::Option<{}>>()?.map(|x| x.0)",
"{}map.next_value::<::core::option::Option<{}>>()?.map(|x| x.0)",
Indent(indent + 1),
deserializer
)?;
}
FieldModifier::Repeated => {
writeln!(
writer,
"{}Some(map.next_value::<Vec<{}>>()?",
"{}Some(map.next_value::<::alloc::vec::Vec<{}>>()?",
Indent(indent + 1),
deserializer
)?;
Expand Down
2 changes: 1 addition & 1 deletion pbjson-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() -> Result<()> {
.compile_well_known_types()
.extern_path(".google.protobuf", "::pbjson_types")
.extern_path(".test.external", "crate")
.bytes(&[".test"])
.bytes([".test"])
.protoc_arg("--experimental_allow_proto3_optional");

if cfg!(feature = "btree") {
Expand Down
17 changes: 14 additions & 3 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use serde::{Deserialize, Serialize};

extern crate alloc;

/// A test of an externally defined message
#[derive(Clone, PartialEq, ::prost::Message, Serialize, Deserialize)]
#[derive(Clone, Eq, PartialEq, ::prost::Message, Serialize, Deserialize)]
pub struct ExternMessage {}

/// A test of an externally defined enumeration
Expand Down Expand Up @@ -211,6 +213,8 @@ mod tests {
#[test]
#[cfg(not(any(feature = "emit-fields", feature = "use-integers-for-enums")))]
fn test_kitchen_sink() {
use chrono::Timelike;

let mut decoded: KitchenSink = serde_json::from_str("{}").unwrap();

verify(&decoded, "{}");
Expand Down Expand Up @@ -547,13 +551,20 @@ mod tests {
decoded.optional_string = None;
verify_decode(&decoded, "{}");

let date = chrono::Utc.ymd(2072, 3, 1).and_hms_milli(5, 2, 5, 30);
let date = chrono::Utc
.with_ymd_and_hms(2072, 3, 1, 5, 2, 5)
.unwrap()
.with_nanosecond(30)
.unwrap();
decoded.timestamp = Some(Timestamp {
seconds: date.timestamp(),
nanos: date.timestamp_subsec_nanos() as i32,
});

verify(&decoded, r#"{"timestamp":"2072-03-01T05:02:05.030+00:00"}"#);
verify(
&decoded,
r#"{"timestamp":"2072-03-01T05:02:05.000000030+00:00"}"#,
);

decoded.timestamp = None;
verify_decode(&decoded, "{}");
Expand Down
8 changes: 6 additions & 2 deletions pbjson-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ exclude = ["protos/*"]
[lib]
name = "pbjson_types"

[features]
default = ["std"]
std = ["prost/std", "serde/std"]

[dependencies] # In alphabetical order
bytes = "1.0"
chrono = { version = "0.4", default-features = false, features = ["alloc"] }
informalsystems-pbjson = { path = "../pbjson", version = "0.5" }
prost = "0.11"
serde = { version = "1.0", features = ["derive"] }
prost = { version = "0.11", default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false }

[dev-dependencies]
serde_json = "1.0"
Expand Down
10 changes: 8 additions & 2 deletions pbjson-types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ fn main() -> Result<()> {
config
.file_descriptor_set_path(&descriptor_path)
.compile_well_known_types()
.disable_comments(&["."])
.bytes(&[".google"])
.disable_comments(["."])
.bytes([".google"])
.skip_protoc_run();

let std = std::env::var("CARGO_FEATURE_STD").map_or(false, |_| true);

if !std {
config.btree_map([".google"]);
}

let empty: &[&str] = &[];
config.compile_protos(empty, empty)?;

Expand Down
15 changes: 9 additions & 6 deletions pbjson-types/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use crate::Duration;
use serde::de::Visitor;
use serde::Serialize;

impl TryFrom<Duration> for std::time::Duration {
type Error = std::num::TryFromIntError;
use alloc::format;
use alloc::string::ToString;

impl TryFrom<Duration> for core::time::Duration {
type Error = core::num::TryFromIntError;

fn try_from(value: Duration) -> Result<Self, Self::Error> {
Ok(Self::new(
Expand All @@ -13,8 +16,8 @@ impl TryFrom<Duration> for std::time::Duration {
}
}

impl From<std::time::Duration> for Duration {
fn from(value: std::time::Duration) -> Self {
impl From<core::time::Duration> for Duration {
fn from(value: core::time::Duration) -> Self {
Self {
seconds: value.as_secs() as _,
nanos: value.subsec_nanos() as _,
Expand Down Expand Up @@ -43,7 +46,7 @@ impl Serialize for Duration {

if self.nanos != 0 {
s.push('.');
let f = match split_nanos(self.nanos.abs() as u32) {
let f = match split_nanos(self.nanos.unsigned_abs()) {
(millis, 0, 0) => format!("{:03}", millis),
(millis, micros, 0) => format!("{:03}{:03}", millis, micros),
(millis, micros, nanos) => format!("{:03}{:03}{:03}", millis, micros, nanos),
Expand All @@ -61,7 +64,7 @@ struct DurationVisitor;
impl<'de> Visitor<'de> for DurationVisitor {
type Value = Duration;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str("a duration string")
}

Expand Down
Loading