split string into vector

This commit is contained in:
jb-alvarado 2022-03-21 18:06:49 +01:00
parent 4bfecda757
commit 95b80c5edf
3 changed files with 40 additions and 0 deletions

7
Cargo.lock generated
View File

@ -160,6 +160,7 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"shlex",
"simplelog",
"tokio",
"walkdir",
@ -940,6 +941,12 @@ dependencies = [
"yaml-rust",
]
[[package]]
name = "shlex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]]
name = "simplelog"
version = "0.11.2"

View File

@ -21,6 +21,7 @@ serde_yaml = "0.8"
simplelog = { version = "^0.11.2", features = ["paris"] }
tokio = { version = "1.16.1", features = ["rt-multi-thread"] }
walkdir = "2"
shlex = "1.1"
[target.x86_64-unknown-linux-musl.dependencies]
openssl = { version = "0.10", features = ["vendored"] }

32
examples/string_to_arr.rs Normal file
View File

@ -0,0 +1,32 @@
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_yaml::{self};
use shlex::split;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Processing {
pub mode: String,
pub volume: f64,
pub settings: String,
}
fn main() {
let s = r#"
mode: "playlist"
volume: 0.5
settings: -i input.mp4 -c:v libx264 -metadata service_provider='ffplayout Inc.' -f mpegts out.mp4
"#;
let config: Processing =
serde_yaml::from_str(s).expect("Could not read config");
let pattern = Regex::new(r#"[^\s"']+|"([^"]*)"|'([^']*)'"#).unwrap();
let matches: Vec<String> = pattern
.find_iter(config.settings.as_str())
.map(|m| m.as_str().to_string())
.collect();
println!("{:#?}", matches);
println!("{:#?}", split(config.settings.as_str()));
}