Skip to content

Commit

Permalink
v0.2 housekeeping: non_exhaustive all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed Oct 10, 2024
1 parent 48aa068 commit 25381dc
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nanoserde"
version = "0.2.0-beta.1"
version = "0.2.0-beta.2"
authors = ["makepad <[email protected]>", "Fedor <[email protected]>"]
license = "MIT OR Apache-2.0"
description = """
Expand Down Expand Up @@ -30,4 +30,4 @@ toml = []
std = []

[dependencies]
nanoserde-derive = { path = "derive", version = "=0.2.0-beta.1", optional = true }
nanoserde-derive = { path = "derive", version = "=0.2.0-beta.2", optional = true }
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nanoserde-derive"
version = "0.2.0-beta.1"
version = "0.2.0-beta.2"
authors = ["Makepad <[email protected]>", "Fedor <[email protected]>"]
edition = "2018"
description = "Fork of makepad-tinyserde derive without any external dependencies"
Expand Down
2 changes: 2 additions & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate alloc;
extern crate proc_macro;

#[cfg(any(feature = "json", feature = "ron", feature = "binary"))]
#[macro_use]
mod shared;

Expand All @@ -21,6 +22,7 @@ mod serde_json;
#[cfg(feature = "json")]
use crate::serde_json::*;

#[cfg(any(feature = "json", feature = "ron", feature = "binary"))]
mod parse;

#[cfg(feature = "binary")]
Expand Down
2 changes: 2 additions & 0 deletions derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub struct Struct {
pub named: bool,
pub fields: Vec<Field>,
pub attributes: Vec<Attribute>,
#[cfg_attr(feature = "ron", allow(unused))]
pub generics: Vec<Generic>,
}

Expand All @@ -143,6 +144,7 @@ pub struct Enum {
pub name: String,
pub variants: Vec<Field>,
pub attributes: Vec<Attribute>,
#[cfg_attr(feature = "ron", allow(unused))]
pub generics: Vec<Generic>,
}

Expand Down
2 changes: 1 addition & 1 deletion derive/src/serde_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream {
let id: u16 = DeBin::de_bin(o,d)?;
Ok(match id {{
{}
_ => return ::core::result::Result::Err(nanoserde::DeBinErr{{o:*o, l:0, s:d.len()}})
_ => return ::core::result::Result::Err(nanoserde::DeBinErr::new(*o, 0, d.len()))
}})
}}
}}", generic_w_bounds,enum_.name,generic_no_bounds, r)
Expand Down
10 changes: 7 additions & 3 deletions derive/src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ pub fn derive_ser_json_struct(struct_: &Struct) -> TokenStream {
let proxy_attr = crate::shared::attrs_proxy(&field.attributes);
let struct_null_on_none = shared::attrs_serialize_none_as_null(&struct_.attributes);
let field_null_on_none = shared::attrs_serialize_none_as_null(&field.attributes);
let null_on_none = (field_null_on_none || struct_null_on_none) && proxy_attr.is_none();
let field_header = &format!("if first_field_was_serialized {{
let null_on_none =
(field_null_on_none || struct_null_on_none) && proxy_attr.is_none();
let field_header = &format!(
"if first_field_was_serialized {{
s.conl();
}};
first_field_was_serialized = true;
s.field(d+1, \"{}\");", json_fieldname);
s.field(d+1, \"{}\");",
json_fieldname
);
l!(
s,
"{}
Expand Down
11 changes: 6 additions & 5 deletions derive/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
#![cfg(any(feature = "json", feature = "ron", feature = "binary"))]

#[cfg(any(feature = "json", feature = "binary"))]
use alloc::{format, string::ToString, vec::Vec};

use alloc::string::String;

#[cfg(any(feature = "binary", feature = "json"))]
use crate::parse::{Enum, Struct};
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//! For `#[nserde(..)]` supported attributes for each format check [Features support matrix](https://github.com/not-fl3/nanoserde#features-support-matrix)
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

extern crate alloc;

Expand Down
7 changes: 7 additions & 0 deletions src/serde_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,19 @@ pub trait DeBin: Sized {

/// The error message when failing to deserialize from raw bytes.
#[derive(Clone)]
#[non_exhaustive]
pub struct DeBinErr {
pub o: usize,
pub l: usize,
pub s: usize,
}

impl DeBinErr {
pub fn new(o: usize, l: usize, s: usize) -> Self {
Self { o, l, s }
}
}

impl core::fmt::Debug for DeBinErr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
Expand Down
12 changes: 10 additions & 2 deletions src/serde_json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::str::Chars;

// remove this after 1.81 is live
// remove this after 1.81 is msrv
#[cfg(not(feature = "std"))]
use core::error::Error;
#[cfg(feature = "std")]
Expand All @@ -13,11 +13,16 @@ use alloc::string::{String, ToString};
use alloc::vec::Vec;

/// The internal state of a JSON serialization.
#[non_exhaustive]
pub struct SerJsonState {
pub out: String,
}

impl SerJsonState {
pub fn new(out: String) -> Self {
Self { out }
}

pub fn indent(&mut self, _d: usize) {
//for _ in 0..d {
// self.out.push_str(" ");
Expand Down Expand Up @@ -67,7 +72,7 @@ pub trait SerJson {
///
/// ```rust
/// # use nanoserde::*;
/// let mut s = SerJsonState { out: String::new() };
/// let mut s = SerJsonState::new(String::new());
/// 42u32.ser_json(0, &mut s);
/// assert_eq!(s.out, "42");
/// ```
Expand Down Expand Up @@ -103,6 +108,7 @@ pub trait DeJson: Sized {

/// A JSON parsed token.
#[derive(PartialEq, Debug)]
#[non_exhaustive]
pub enum DeJsonTok {
Str,
Char(char),
Expand Down Expand Up @@ -130,6 +136,7 @@ impl Default for DeJsonTok {

/// The internal state of a JSON deserialization.
#[derive(Default)]
#[non_exhaustive]
pub struct DeJsonState {
pub cur: char,
pub tok: DeJsonTok,
Expand All @@ -142,6 +149,7 @@ pub struct DeJsonState {

/// The error message when failing to deserialize a JSON string.
#[derive(Clone)]
#[non_exhaustive]
pub struct DeJsonErr {
pub msg: String,
pub line: usize,
Expand Down
2 changes: 2 additions & 0 deletions src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl Default for DeRonTok {

/// The internal state of a RON deserialization.
#[derive(Default)]
#[non_exhaustive]
pub struct DeRonState {
pub cur: char,
pub tok: DeRonTok,
Expand All @@ -139,6 +140,7 @@ pub struct DeRonState {

/// The error message when failing to deserialize a RON string.
#[derive(Clone)]
#[non_exhaustive]
pub struct DeRonErr {
pub msg: String,
pub line: usize,
Expand Down
3 changes: 3 additions & 0 deletions src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ macro_rules! ident_term_chars {
/// assert_eq!(parsed["Section.value"], Toml::Num(1.));
/// ```
#[derive(Default)]
#[non_exhaustive]
pub struct TomlParser {
cur: char,
line: usize,
Expand All @@ -63,6 +64,7 @@ pub struct TomlParser {

/// A TOML parsed token.
#[derive(PartialEq, Debug)]
#[non_exhaustive]
pub enum TomlTok {
Ident(String),
Str(String),
Expand Down Expand Up @@ -197,6 +199,7 @@ impl Toml {

/// The error message when failing to parse a TOML string.
#[derive(Clone)]
#[non_exhaustive]
pub struct TomlErr {
pub msg: String,
pub line: usize,
Expand Down

0 comments on commit 25381dc

Please sign in to comment.