Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add support for parsing Rust-style string continuation escapes ([#585](https://github.com/ron-rs/ron/issues/585))

### Additions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Additions
### API Changes


- Add the `from_value` convenience function to deserialize a `T` from a `Value` ([#255](https://github.com/ron-rs/ron/issues/255))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please link to this PR instead, and also drive-by fix the above change log entry to also link to its PR?


## [0.12.2] - 2026-06-22

### Format Changes
Expand Down
13 changes: 13 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ where
Options::default().from_bytes(s)
}

/// A convenience function for deserializing a value of type `T`
/// from a [`Value`](crate::Value).
///
/// Unlike the other convenience functions this returns a plain [`Error`]
/// rather than a [`SpannedError`], since an in-memory [`Value`](crate::Value)
/// has no source text to point a span into.
pub fn from_value<T>(value: crate::Value) -> Result<T>
where
T: de::DeserializeOwned,
{
value.into_rust()
}

macro_rules! guard_recursion {
($self:expr => $expr:expr) => {{
if let Some(limit) = &mut $self.recursion_limit {
Expand Down
42 changes: 42 additions & 0 deletions tests/255_from_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use ron::value::{Number, Value};
use serde_derive::Deserialize;

#[derive(Debug, PartialEq, Deserialize)]
struct Config {
id: u32,
name: String,
}

#[test]
fn from_value_deserializes_struct() {
let value: Value = ron::from_str("(id: 7, name: \"ron\")").unwrap();

let config: Config = ron::de::from_value(value).unwrap();

assert_eq!(
config,
Config {
id: 7,
name: "ron".to_owned(),
}
);
}

#[test]
fn from_value_matches_into_rust() {
let value: Value = ron::from_str("(id: 1, name: \"a\")").unwrap();

let via_fn: Config = ron::de::from_value(value.clone()).unwrap();
let via_method: Config = value.into_rust().unwrap();

assert_eq!(via_fn, via_method);
}

#[test]
fn from_value_reports_error_on_type_mismatch() {
let value = Value::Number(Number::U8(1));

let result: Result<Config, _> = ron::de::from_value(value);

assert!(result.is_err());
}
Loading