use crate::permission::Permission;
use proc_macro::TokenStream;
use syn::{Path, Token, punctuated::Punctuated};

pub fn parse_permissions(perms: TokenStream) -> Vec<Permission> {
    let paths: Punctuated<Path, Token![,]> = syn::parse::Parser::parse2(
        Punctuated::<Path, Token![,]>::parse_terminated,
        perms.into(),
    )
    .unwrap();

    paths
        .into_iter()
        .filter_map(|path| {
            match (
                path.segments.first().map(|s| s.ident.to_string()),
                path.segments.last().map(|s| s.ident.to_string()),
            ) {
                (Some(ns), Some(action)) if ns == "fs" && action == "write" => {
                    Some(Permission::FsWrite)
                }
                (Some(ns), Some(action)) if ns == "fs" && action == "read" => {
                    Some(Permission::FsRead)
                }
                _ => None,
            }
        })
        .collect()
}
