[feat] set video config
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
pub fn to_json(vdf_data: &str) -> String {
|
||||
let linebreak = match std::env::consts::OS {
|
||||
"macos" => "\r",
|
||||
"macos" => "\n", //"\r",
|
||||
"windows" => "\n",
|
||||
"linux" => "\n",
|
||||
_ => "\n",
|
||||
};
|
||||
|
||||
// NOTE: 这样会跳过顶层{}
|
||||
let startpoint = vdf_data.find('{').unwrap_or(0);
|
||||
let vdf_data = &vdf_data[startpoint..];
|
||||
|
||||
@@ -31,13 +32,15 @@ pub fn to_json(vdf_data: &str) -> String {
|
||||
json_data.push_str(&line);
|
||||
}
|
||||
|
||||
// let json_str = json_data
|
||||
json_data = json_data
|
||||
.replace(",}", "}")
|
||||
.trim_start_matches(": ")
|
||||
.trim_end_matches(',')
|
||||
.to_string();
|
||||
// json_data = format!("{{{}}}", json_str);
|
||||
|
||||
json_data
|
||||
return json_data;
|
||||
}
|
||||
|
||||
pub fn to_vdf(json_data: &str) -> String {
|
||||
@@ -48,33 +51,33 @@ pub fn to_vdf(json_data: &str) -> String {
|
||||
}
|
||||
|
||||
fn build_vdf(json_value: &serde_json::Value, vdf_data: &mut String, indent_level: usize) {
|
||||
match json_value {
|
||||
serde_json::Value::Object(obj) => {
|
||||
for (key, value) in obj {
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str(&format!("\"{}\"\n", key));
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str("{\n");
|
||||
build_vdf(value, vdf_data, indent_level + 1);
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str("}\n");
|
||||
}
|
||||
}
|
||||
serde_json::Value::String(s) => {
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str(&format!("\"{}\"\t\t\"{}\"\n", s, s));
|
||||
}
|
||||
_ => {
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str(&format!("\"{}\"\t\t\"{}\"\n", json_value, json_value));
|
||||
}
|
||||
}
|
||||
match json_value {
|
||||
serde_json::Value::Object(obj) => {
|
||||
for (key, value) in obj {
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str(&format!("\"{}\"\n", key));
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str("{\n");
|
||||
build_vdf(value, vdf_data, indent_level + 1);
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str("}\n");
|
||||
}
|
||||
}
|
||||
serde_json::Value::String(s) => {
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str(&format!("\"{}\"\t\t\"{}\"\n", s, s));
|
||||
}
|
||||
_ => {
|
||||
vdf_data.push_str(&"\t".repeat(indent_level));
|
||||
vdf_data.push_str(&format!("\"{}\"\t\t\"{}\"\n", json_value, json_value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
static VDF_DATA: &str = r#""users"
|
||||
static VDF_DATA: &str = r#""users"
|
||||
{
|
||||
"76561198315078806"
|
||||
{
|
||||
@@ -101,7 +104,7 @@ mod tests {
|
||||
}
|
||||
"#;
|
||||
|
||||
static JSON_DATA: &str = r#"{
|
||||
static JSON_DATA: &str = r#"{
|
||||
"users": {
|
||||
"76561198315078806": {
|
||||
"AccountName": "_jerry_dota2",
|
||||
@@ -126,12 +129,11 @@ mod tests {
|
||||
}
|
||||
}"#;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_to_json() {
|
||||
// let expected_json = r#"{"key1": "value1","key2": "value2","subkey": {"key3": "value3"}}"#;
|
||||
let json_data = to_json(VDF_DATA);
|
||||
println!("{}", json_data);
|
||||
println!("{}", json_data);
|
||||
|
||||
// 解析json
|
||||
let json_value: serde_json::Value = serde_json::from_str(&json_data).unwrap();
|
||||
@@ -140,11 +142,11 @@ mod tests {
|
||||
// assert_eq!(to_json(vdf_data), expected_json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_vdf() {
|
||||
// let json_data = r#"{"key1": "value1","key2": "value2","subkey": {"key3": "value3"}}"#;
|
||||
let vdf_data = to_vdf(JSON_DATA);
|
||||
#[test]
|
||||
fn test_to_vdf() {
|
||||
// let json_data = r#"{"key1": "value1","key2": "value2","subkey": {"key3": "value3"}}"#;
|
||||
let vdf_data = to_vdf(JSON_DATA);
|
||||
|
||||
println!("{}", vdf_data);
|
||||
}
|
||||
println!("{}", vdf_data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user