diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/ron/examples | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ron/examples')
-rw-r--r-- | third_party/rust/ron/examples/decode.rs | 68 | ||||
-rw-r--r-- | third_party/rust/ron/examples/decode_file.rs | 37 | ||||
-rw-r--r-- | third_party/rust/ron/examples/encode.rs | 50 | ||||
-rw-r--r-- | third_party/rust/ron/examples/example.ron | 22 | ||||
-rw-r--r-- | third_party/rust/ron/examples/transcode.rs | 31 |
5 files changed, 208 insertions, 0 deletions
diff --git a/third_party/rust/ron/examples/decode.rs b/third_party/rust/ron/examples/decode.rs new file mode 100644 index 0000000000..5aa2042fa7 --- /dev/null +++ b/third_party/rust/ron/examples/decode.rs @@ -0,0 +1,68 @@ +#![allow(dead_code)] + +use std::collections::HashMap; + +use ron::de::from_str; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct Config { + boolean: bool, + float: f32, + map: HashMap<u8, char>, + nested: Nested, + option: Option<String>, + tuple: (u32, u32), +} + +#[derive(Debug, Deserialize)] +struct Nested { + a: String, + b: char, +} + +const CONFIG: &str = " +/* + * RON now has multi-line (C-style) block comments! + * They can be freely nested: + * /* This is a nested comment */ + * If you just want a single-line comment, + * do it like here: +// Just put two slashes before the comment and the rest of the line +// can be used freely! +*/ + +// Note that block comments can not be started in a line comment +// (Putting a /* here will have no effect) + +( + boolean: true, + float: 8.2, + map: { + 1: '1', + 2: '4', + 3: '9', + 4: '1', + 5: '2', + 6: '3', + }, + nested: Nested( + a: \"Decode me!\", + b: 'z', + ), + option: Some(\t \"Weird formatting!\" \n\n ), + tuple: (3 /*(2 + 1)*/, 7 /*(2 * 5 - 3)*/), +)"; + +fn main() { + let config: Config = match from_str(CONFIG) { + Ok(x) => x, + Err(e) => { + println!("Failed to load config: {}", e); + + std::process::exit(1); + } + }; + + println!("Config: {:?}", &config); +} diff --git a/third_party/rust/ron/examples/decode_file.rs b/third_party/rust/ron/examples/decode_file.rs new file mode 100644 index 0000000000..72ca8a05f3 --- /dev/null +++ b/third_party/rust/ron/examples/decode_file.rs @@ -0,0 +1,37 @@ +#![allow(dead_code)] + +use std::{collections::HashMap, fs::File}; + +use ron::de::from_reader; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct Config { + boolean: bool, + float: f32, + map: HashMap<u8, char>, + nested: Nested, + tuple: (u32, u32), + vec: Vec<Nested>, +} + +#[derive(Debug, Deserialize)] +struct Nested { + a: String, + b: char, +} + +fn main() { + let input_path = format!("{}/examples/example.ron", env!("CARGO_MANIFEST_DIR")); + let f = File::open(&input_path).expect("Failed opening file"); + let config: Config = match from_reader(f) { + Ok(x) => x, + Err(e) => { + println!("Failed to load config: {}", e); + + std::process::exit(1); + } + }; + + println!("Config: {:?}", &config); +} diff --git a/third_party/rust/ron/examples/encode.rs b/third_party/rust/ron/examples/encode.rs new file mode 100644 index 0000000000..30e9688951 --- /dev/null +++ b/third_party/rust/ron/examples/encode.rs @@ -0,0 +1,50 @@ +use std::{collections::HashMap, iter::FromIterator}; + +use ron::ser::{to_string_pretty, PrettyConfig}; +use serde::Serialize; + +#[derive(Serialize)] +struct Config { + float: (f32, f64), + tuple: TupleStruct, + map: HashMap<u8, char>, + nested: Nested, + var: Variant, + array: Vec<()>, +} + +#[derive(Serialize)] +struct TupleStruct((), bool); + +#[derive(Serialize)] +enum Variant { + A(u8, &'static str), +} + +#[derive(Serialize)] +struct Nested { + a: String, + b: char, +} + +fn main() { + let data = Config { + float: (2.18, -1.1), + tuple: TupleStruct((), false), + map: HashMap::from_iter(vec![(0, '1'), (1, '2'), (3, '5'), (8, '1')]), + nested: Nested { + a: "Hello from \"RON\"".to_string(), + b: 'b', + }, + var: Variant::A(!0, ""), + array: vec![(); 3], + }; + + let pretty = PrettyConfig::new() + .depth_limit(2) + .separate_tuple_members(true) + .enumerate_arrays(true); + let s = to_string_pretty(&data, pretty).expect("Serialization failed"); + + println!("{}", s); +} diff --git a/third_party/rust/ron/examples/example.ron b/third_party/rust/ron/examples/example.ron new file mode 100644 index 0000000000..011ac5782d --- /dev/null +++ b/third_party/rust/ron/examples/example.ron @@ -0,0 +1,22 @@ +( + boolean: true, + float: 8.2, + map: { + 1: '1', + 2: '4', + 3: '9', + 4: '1', + 5: '2', + 6: '3', + }, + nested: Nested( + a: "Decode me!", + b: 'z', + ), + tuple: (3, 7), + vec: [ + (a: "Nested 1", b: 'x'), + (a: "Nested 2", b: 'y'), + (a: "Nested 3", b: 'z'), + ], +)
\ No newline at end of file diff --git a/third_party/rust/ron/examples/transcode.rs b/third_party/rust/ron/examples/transcode.rs new file mode 100644 index 0000000000..115ac469d1 --- /dev/null +++ b/third_party/rust/ron/examples/transcode.rs @@ -0,0 +1,31 @@ +use ron::value::Value; +use serde::Serialize; + +fn main() { + let data = r#" + Scene( // class name is optional + materials: { // this is a map + "metal": ( + reflectivity: 1.0, + ), + "plastic": ( + reflectivity: 0.5, + ), + }, + entities: [ // this is an array + ( + name: "hero", + material: "metal", + ), + ( + name: "monster", + material: "plastic", + ), + ], + ) + "#; + + let value: Value = data.parse().expect("Failed to deserialize"); + let mut ser = serde_json::Serializer::pretty(std::io::stdout()); + value.serialize(&mut ser).expect("Failed to serialize"); +} |