use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BencodeValue {
Int(i64),
Bytes(Vec<u8>),
List(Vec<BencodeValue>),
Dict(BTreeMap<Vec<u8>, BencodeValue>),
}
impl BencodeValue {
pub fn as_int(&self) -> Option<i64> {
match self {
Self::Int(v) => Some(*v),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Self::Bytes(v) => Some(v),
_ => None,
}
}
pub fn as_list(&self) -> Option<&[BencodeValue]> {
match self {
Self::List(v) => Some(v),
_ => None,
}
}
pub fn as_dict(&self) -> Option<&BTreeMap<Vec<u8>, BencodeValue>> {
match self {
Self::Dict(v) => Some(v),
_ => None,
}
}
pub fn dict_get(&self, key: &[u8]) -> Option<&BencodeValue> {
self.as_dict()?.get(key)
}
pub fn dict_get_str(&self, key: &str) -> Option<&BencodeValue> {
self.dict_get(key.as_bytes())
}
}