reala-subscription 0.0.0

Simple subscription fetcher and parser.
Documentation
//! Subscription in Clash's YAML configuration format.

use std::sync::Arc;

use crate::Error;

#[derive(Debug, Clone)]
#[derive(serde::Deserialize)]
#[serde(tag = "type")]
/// A proxy configuration.
pub(crate) enum Proxy {
    #[serde(rename = "ss")]
    /// Shadowsocks 2017 or 2022.
    Shadowsocks {
        /// The server address.
        server: Arc<str>,

        /// The server port.
        port: u16,

        /// The password
        password: Arc<str>,

        /// The cipher suite.
        cipher: Arc<str>,
    },

    #[serde(other)]
    /// All unknown proxy types.
    Unknown,
}

#[derive(Debug, Clone)]
#[derive(serde::Deserialize)]
pub(crate) struct Config {
    /// Proxies.
    pub(crate) proxies: Vec<Proxy>,
}

impl Config {
    #[inline]
    /// Parses the given Clash configuration YAML string and returns available
    /// [`Proxy`]s.
    ///
    /// The unknown proxy types are filtered out.
    ///
    /// If the configuration contains no valid proxies, `Ok(None)` is returned.
    ///
    /// # Errors
    ///
    /// Yaml parsing errors.
    pub(crate) fn parse(s: &str) -> Result<Self, Error> {
        serde_saphyr::from_str(s).map_err(|_| Error::Parse)
    }
}