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

Document nested block comments and add more intra docs links #440

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ fn main() {
let x: MyStruct = ron::from_str("(boolean: true, float: 1.23)").unwrap();

println!("RON: {}", ron::to_string(&x).unwrap());

println!("Pretty RON: {}", ron::ser::to_string_pretty(
&x, ron::ser::PrettyConfig::default()).unwrap(),
);
}
```

Expand Down
3 changes: 2 additions & 1 deletion docs/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ RON = [extensions], ws, value, ws;
```ebnf
ws = { ws_single | comment };
ws_single = "\n" | "\t" | "\r" | " ";
comment = ["//", { no_newline }, "\n"] | ["/*", { ? any character ? }, "*/"];
comment = ["//", { no_newline }, "\n"] | ["/*", nested_block_comment, "*/"];
nested_block_comment = { ? any characters except "/*" ? }, [ "/*", nested_block_comment, "*/", nested_block_comment ];
juntyr marked this conversation as resolved.
Show resolved Hide resolved
```

## Commas
Expand Down
15 changes: 9 additions & 6 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod value;
/// The RON deserializer.
///
/// If you just want to simply deserialize a value,
/// you can use the `from_str` convenience function.
/// you can use the [`from_str`] convenience function.
pub struct Deserializer<'de> {
bytes: Bytes<'de>,
newtype_variant: bool,
Expand Down Expand Up @@ -128,9 +128,9 @@ impl<'de> Deserializer<'de> {
}
}

/// Called from `deserialize_any` when a struct was detected. Decides if
/// there is a unit, tuple or usual struct and deserializes it
/// accordingly.
/// Called from [`deserialize_any`][serde::Deserializer::deserialize_any]
/// when a struct was detected. Decides if there is a unit, tuple or usual
/// struct and deserializes it accordingly.
///
/// This method assumes there is no identifier left.
fn handle_any_struct<V>(&mut self, visitor: V) -> Result<V::Value>
Expand All @@ -155,8 +155,11 @@ impl<'de> Deserializer<'de> {
}
}

/// Called from `deserialize_struct`, `struct_variant`, and `handle_any_struct`.
/// Handles deserialising the enclosing parentheses and everything in between.
/// Called from
/// [`deserialize_struct`][serde::Deserializer::deserialize_struct],
/// [`struct_variant`][serde::de::VariantAccess::struct_variant], and
/// [`handle_any_struct`][Self::handle_any_struct]. Handles
/// deserialising the enclosing parentheses and everything in between.
///
/// This method assumes there is no struct name identifier left.
fn handle_struct_after_name<V>(
Expand Down
9 changes: 7 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ impl Options {
Ok(value)
}

/// Serializes `value` into `writer`
/// Serializes `value` into `writer`.
///
/// This function does not generate any newlines or nice formatting;
/// if you want that, you can use
/// [`to_writer_pretty`][Self::to_writer_pretty] instead.
pub fn to_writer<W, T>(&self, writer: W, value: &T) -> Result<()>
where
W: io::Write,
Expand All @@ -185,7 +189,8 @@ impl Options {
/// Serializes `value` and returns it as string.
///
/// This function does not generate any newlines or nice formatting;
/// if you want that, you can use `to_string_pretty` instead.
/// if you want that, you can use
/// [`to_string_pretty`][Self::to_string_pretty] instead.
pub fn to_string<T>(&self, value: &T) -> Result<String>
where
T: ?Sized + ser::Serialize,
Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum AnyNum {

#[derive(Clone, Copy, Debug)]
pub struct Bytes<'a> {
/// Bits set according to the `Extensions` enum.
/// Bits set according to the [`Extensions`] enum.
pub exts: Extensions,
bytes: &'a [u8],
cursor: Position,
Expand Down
23 changes: 14 additions & 9 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ mod raw;
mod tests;
mod value;

/// Serializes `value` into `writer`
/// Serializes `value` into `writer`.
///
/// This function does not generate any newlines or nice formatting;
/// if you want that, you can use [`to_writer_pretty`] instead.
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<()>
where
W: io::Write,
Expand All @@ -39,7 +42,7 @@ where
/// Serializes `value` and returns it as string.
///
/// This function does not generate any newlines or nice formatting;
/// if you want that, you can use `to_string_pretty` instead.
/// if you want that, you can use [`to_string_pretty`] instead.
pub fn to_string<T>(value: &T) -> Result<String>
where
T: ?Sized + Serialize,
Expand Down Expand Up @@ -102,7 +105,7 @@ pub struct PrettyConfig {
}

impl PrettyConfig {
/// Creates a default `PrettyConfig`.
/// Creates a default [`PrettyConfig`].
pub fn new() -> Self {
Default::default()
}
Expand Down Expand Up @@ -256,8 +259,8 @@ impl Default for PrettyConfig {

/// The RON serializer.
///
/// You can just use `to_string` for deserializing a value.
/// If you want it pretty-printed, take a look at the `pretty` module.
/// You can just use [`to_string`] for deserializing a value.
/// If you want it pretty-printed, take a look at [`to_string_pretty`].
pub struct Serializer<W: io::Write> {
output: W,
pretty: Option<(PrettyConfig, Pretty)>,
Expand All @@ -268,16 +271,18 @@ pub struct Serializer<W: io::Write> {
}

impl<W: io::Write> Serializer<W> {
/// Creates a new `Serializer`.
/// Creates a new [`Serializer`].
///
/// Most of the time you can just use `to_string` or `to_string_pretty`.
/// Most of the time you can just use [`to_string`] or
/// [`to_string_pretty`].
pub fn new(writer: W, config: Option<PrettyConfig>) -> Result<Self> {
Self::with_options(writer, config, Options::default())
}

/// Creates a new `Serializer`.
/// Creates a new [`Serializer`].
///
/// Most of the time you can just use `to_string` or `to_string_pretty`.
/// Most of the time you can just use [`to_string`] or
/// [`to_string_pretty`].
pub fn with_options(
mut writer: W,
config: Option<PrettyConfig>,
Expand Down
59 changes: 31 additions & 28 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) mod raw;

pub use raw::RawValue;

/// A `Value` to `Value` map.
/// A [`Value`] to [`Value`] map.
///
/// This structure either uses a [BTreeMap](std::collections::BTreeMap) or the
/// [IndexMap](indexmap::IndexMap) internally.
Expand All @@ -29,7 +29,7 @@ pub use raw::RawValue;
pub struct Map(MapInner);

impl Map {
/// Creates a new, empty `Map`.
/// Creates a new, empty [`Map`].
pub fn new() -> Map {
Default::default()
}
Expand Down Expand Up @@ -144,20 +144,20 @@ type MapInner = std::collections::BTreeMap<Value, Value>;
#[cfg(feature = "indexmap")]
type MapInner = indexmap::IndexMap<Value, Value>;

/// A wrapper for a number, which can be either `f64` or `i64`.
/// A wrapper for a number, which can be either [`f64`] or [`i64`].
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Hash, Ord)]
pub enum Number {
Integer(i64),
Float(Float),
}

/// A wrapper for `f64`, which guarantees that the inner value
/// is finite and thus implements `Eq`, `Hash` and `Ord`.
/// A wrapper for [`f64`], which guarantees that the inner value
/// is finite and thus implements [`Eq`], [`Hash`] and [`Ord`].
#[derive(Copy, Clone, Debug)]
pub struct Float(f64);

impl Float {
/// Construct a new `Float`.
/// Construct a new [`Float`].
pub fn new(v: f64) -> Self {
Float(v)
}
Expand All @@ -174,8 +174,8 @@ impl Number {
v.into()
}

/// Returns the `f64` representation of the number regardless of whether the number is stored
/// as a float or integer.
/// Returns the [`f64`] representation of the [`Number`] regardless of
/// whether the number is stored as a float or integer.
///
/// # Example
///
Expand All @@ -190,7 +190,7 @@ impl Number {
self.map_to(|i| i as f64, |f| f)
}

/// If the `Number` is a float, return it. Otherwise return `None`.
/// If the [`Number`] is a float, return it. Otherwise return [`None`].
///
/// # Example
///
Expand All @@ -205,7 +205,7 @@ impl Number {
self.map_to(|_| None, Some)
}

/// If the `Number` is an integer, return it. Otherwise return `None`.
/// If the [`Number`] is an integer, return it. Otherwise return [`None`].
///
/// # Example
///
Expand Down Expand Up @@ -261,8 +261,9 @@ impl From<i32> for Number {
}
}

// The following number conversion checks if the integer fits losslessly into an i64, before
// constructing a Number::Integer variant. If not, the conversion defaults to float.
/// The following [`Number`] conversion checks if the integer fits losslessly
/// into an [`i64`], before constructing a [`Number::Integer`] variant.
/// If not, the conversion defaults to [`Number::Float`].

impl From<u64> for Number {
fn from(i: u64) -> Number {
Expand All @@ -275,19 +276,19 @@ impl From<u64> for Number {
}

/// Partial equality comparison
/// In order to be able to use `Number` as a mapping key, NaN floating values
/// wrapped in `Float` are equals to each other. It is not the case for
/// underlying `f64` values itself.
/// In order to be able to use [`Number`] as a mapping key, NaN floating values
/// wrapped in [`Float`] are equal to each other. It is not the case for
/// underlying [`f64`] values itself.
impl PartialEq for Float {
fn eq(&self, other: &Self) -> bool {
self.0.is_nan() && other.0.is_nan() || self.0 == other.0
}
}

/// Equality comparison
/// In order to be able to use `Float` as a mapping key, NaN floating values
/// wrapped in `Float` are equals to each other. It is not the case for
/// underlying `f64` values itself.
/// In order to be able to use [`Float`] as a mapping key, NaN floating values
/// wrapped in [`Float`] are equal to each other. It is not the case for
/// underlying [`f64`] values itself.
impl Eq for Float {}

impl Hash for Float {
Expand All @@ -297,9 +298,11 @@ impl Hash for Float {
}

/// Partial ordering comparison
/// In order to be able to use `Number` as a mapping key, NaN floating values
/// wrapped in `Number` are equals to each other and are less then any other
/// floating value. It is not the case for the underlying `f64` values themselves.
/// In order to be able to use [`Number`] as a mapping key, NaN floating values
/// wrapped in [`Number`] are equal to each other and are less then any other
/// floating value. It is not the case for the underlying [`f64`] values
/// themselves.
///
/// ```
/// use ron::value::Number;
/// assert!(Number::new(std::f64::NAN) < Number::new(std::f64::NEG_INFINITY));
Expand All @@ -317,10 +320,10 @@ impl PartialOrd for Float {
}

/// Ordering comparison
/// In order to be able to use `Float` as a mapping key, NaN floating values
/// wrapped in `Float` are equals to each other and are less then any other
/// floating value. It is not the case for underlying `f64` values itself. See
/// the `PartialEq` implementation.
/// In order to be able to use [`Float`] as a mapping key, NaN floating values
/// wrapped in [`Float`] are equal to each other and are less then any other
/// floating value. It is not the case for underlying [`f64`] values itself.
/// See the [`PartialEq`] implementation.
impl Ord for Float {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).expect("Bug: Contract violation")
Expand All @@ -340,7 +343,7 @@ pub enum Value {
}

impl Value {
/// Tries to deserialize this `Value` into `T`.
/// Tries to deserialize this [`Value`] into `T`.
pub fn into_rust<T>(self) -> Result<T>
where
T: DeserializeOwned,
Expand All @@ -349,8 +352,8 @@ impl Value {
}
}

/// Deserializer implementation for RON `Value`.
/// This does not support enums (because `Value` doesn't store them).
/// Deserializer implementation for RON [`Value`].
/// This does not support enums (because [`Value`] does not store them).
impl<'de> Deserializer<'de> for Value {
type Error = Error;

Expand Down
6 changes: 4 additions & 2 deletions src/value/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ impl RawValue {
&self.ron
}

/// Helper function to validate a RON string and turn it into a `RawValue`.
/// Helper function to validate a RON string and turn it into a
/// [`RawValue`].
pub fn from_ron(ron: &str) -> SpannedResult<&Self> {
Options::default()
.from_str::<&Self>(ron)
.map(|_| Self::from_borrowed_str(ron))
}

/// Helper function to validate a RON string and turn it into a `RawValue`.
/// Helper function to validate a RON string and turn it into a
/// [`RawValue`].
pub fn from_boxed_ron(ron: Box<str>) -> SpannedResult<Box<Self>> {
match Options::default().from_str::<&Self>(&ron) {
Ok(_) => Ok(Self::from_boxed_str(ron)),
Expand Down