reala-subscription 0.0.0

Simple subscription fetcher and parser.
Documentation
//! Data modules for subscription parsing

use std::sync::Arc;

use crate::Error;

mod clash;
mod url;

#[derive(Debug, Clone)]
/// A proxy configuration.
pub enum Proxy {
    /// 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>,
    },
}

impl Proxy {
    #[inline]
    /// Parses a Clash-compatible config.
    ///
    /// If no compatible proxies are found, returns `Ok(None)`.
    pub fn parse_clash_compatible(config: &str) -> Result<Vec<Self>, Error> {
        let parsed = self::clash::Config::parse(config)?;

        let parsed: Vec<_> = parsed
            .proxies
            .into_iter()
            .filter_map(|p| match p {
                self::clash::Proxy::Shadowsocks {
                    server,
                    port,
                    password,
                    cipher,
                } => Some(Self::Shadowsocks {
                    server,
                    port,
                    password,
                    cipher,
                }),
                _ => None,
            })
            .collect();

        Ok(parsed)
    }
}