mirror of
https://gitlab.com/gabmus/envision.git
synced 2025-07-29 12:18:56 +00:00
feat: profile struct with serialize/deserialize
This commit is contained in:
parent
aee780d2bc
commit
eb29b27a67
6 changed files with 200 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
89
Cargo.lock
generated
Normal file
89
Cargo.lock
generated
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.59"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rex2"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.163"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.163"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.96"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
[package]
|
||||||
|
name = "rex2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
serde = { version = "1.0.163", features = [
|
||||||
|
"derive"
|
||||||
|
] }
|
||||||
|
serde_json = "1.0.96"
|
5
src/main.rs
Normal file
5
src/main.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pub mod profile;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
83
src/profile.rs
Normal file
83
src/profile.rs
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
fs::{create_dir_all, File},
|
||||||
|
io::{BufReader, BufWriter},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct Profile {
|
||||||
|
monado_path: String,
|
||||||
|
openovr_path: String,
|
||||||
|
libsurvive_path: Option<String>,
|
||||||
|
basalt_path: Option<String>,
|
||||||
|
mercury_path: Option<String>,
|
||||||
|
libsurvive_enabled: bool,
|
||||||
|
basalt_enabled: bool,
|
||||||
|
mercury_enabled: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_profile(path: String) -> Result<Profile, serde_json::Error> {
|
||||||
|
let file = File::open(path).unwrap();
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
serde_json::from_reader(reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dump_profile(profile: Profile, path_s: String) -> () {
|
||||||
|
let path = Path::new(&path_s);
|
||||||
|
match path.parent() {
|
||||||
|
Some(parent) => {
|
||||||
|
if !parent.is_dir() {
|
||||||
|
create_dir_all(parent).expect("Could not create dir")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
};
|
||||||
|
let file = match path.is_file() {
|
||||||
|
true => File::open(path).expect("Could not open file"),
|
||||||
|
false => File::create(path).expect("Could not create file")
|
||||||
|
};
|
||||||
|
let writer = BufWriter::new(file);
|
||||||
|
serde_json::to_writer_pretty(writer, &profile).expect("Could not write profile")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{dump_profile, Profile, load_profile};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_can_be_loaded() {
|
||||||
|
let profile = load_profile(String::from("./test/files/profile.json"));
|
||||||
|
match profile {
|
||||||
|
Ok(profile) => {
|
||||||
|
assert_eq!(profile.monado_path, "/home/user/monado");
|
||||||
|
assert_eq!(profile.openovr_path, "/home/user/openovr");
|
||||||
|
assert_eq!(
|
||||||
|
profile.libsurvive_path.as_deref(),
|
||||||
|
Some("/home/user/libsurvive")
|
||||||
|
);
|
||||||
|
assert_eq!(profile.mercury_path, None);
|
||||||
|
assert_eq!(profile.basalt_path, None);
|
||||||
|
assert_eq!(profile.libsurvive_enabled, true);
|
||||||
|
assert_eq!(profile.basalt_enabled, false);
|
||||||
|
assert_eq!(profile.mercury_enabled, false);
|
||||||
|
}
|
||||||
|
Err(_) => assert!(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_can_be_dumped() {
|
||||||
|
let p = Profile {
|
||||||
|
monado_path: String::from("/home/user/monado"),
|
||||||
|
openovr_path: String::from("/home/user/openovr"),
|
||||||
|
libsurvive_path: Some(String::from("/home/user/libsurvive")),
|
||||||
|
basalt_path: None,
|
||||||
|
mercury_path: None,
|
||||||
|
libsurvive_enabled: true,
|
||||||
|
basalt_enabled: false,
|
||||||
|
mercury_enabled: false,
|
||||||
|
};
|
||||||
|
dump_profile(p, String::from("./target/testout/testprofile.json"))
|
||||||
|
}
|
||||||
|
}
|
10
test/files/profile.json
Normal file
10
test/files/profile.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"monado_path": "/home/user/monado",
|
||||||
|
"openovr_path": "/home/user/openovr",
|
||||||
|
"libsurvive_path": "/home/user/libsurvive",
|
||||||
|
"basalt_path": null,
|
||||||
|
"mercury_path": null,
|
||||||
|
"libsurvive_enabled": true,
|
||||||
|
"basalt_enabled": false,
|
||||||
|
"mercury_enabled": false
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue