summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ron/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /third_party/rust/ron/examples
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
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.rs67
-rw-r--r--third_party/rust/ron/examples/decode_file.rs36
-rw-r--r--third_party/rust/ron/examples/encode.rs49
-rw-r--r--third_party/rust/ron/examples/example.ron22
-rw-r--r--third_party/rust/ron/examples/transcode.rs31
5 files changed, 205 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..56a9f30074
--- /dev/null
+++ b/third_party/rust/ron/examples/decode.rs
@@ -0,0 +1,67 @@
+#![allow(dead_code)]
+
+use ron::de::from_str;
+use serde::Deserialize;
+use std::collections::HashMap;
+
+#[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..691e9367e3
--- /dev/null
+++ b/third_party/rust/ron/examples/decode_file.rs
@@ -0,0 +1,36 @@
+#![allow(dead_code)]
+
+use ron::de::from_reader;
+use serde::Deserialize;
+use std::{collections::HashMap, fs::File};
+
+#[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..ad1a237ba5
--- /dev/null
+++ b/third_party/rust/ron/examples/encode.rs
@@ -0,0 +1,49 @@
+use ron::ser::{to_string_pretty, PrettyConfig};
+use serde::Serialize;
+use std::{collections::HashMap, iter::FromIterator};
+
+#[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");
+}