sub_converter/parse/
sing_box.rs

1use crate::error::{Error, Result};
2use crate::formats::SingBoxConfig;
3use crate::ir::Node;
4
5pub struct SingBoxParser;
6
7impl super::Parser for SingBoxParser {
8    fn parse(&self, input: &str) -> Result<Vec<Node>> {
9        let input_trim = input.trim_start();
10        let cfg: SingBoxConfig = if input_trim.starts_with('{') {
11            serde_json::from_str(input).map_err(|e| Error::ParseError {
12                detail: format!("sing-box json: {e}"),
13            })?
14        } else {
15            let v: serde_json::Value =
16                serde_yaml::from_str(input).map_err(|e| Error::ParseError {
17                    detail: format!("sing-box yaml: {e}"),
18                })?;
19            serde_json::from_value(v).map_err(|e| Error::ParseError {
20                detail: format!("sing-box yaml->json: {e}"),
21            })?
22        };
23        Ok(cfg.outbounds.into_iter().map(Into::into).collect())
24    }
25}