use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ExtensionSource {
Unpacked(PathBuf),
Packed(PathBuf),
Base64(String),
}
impl ExtensionSource {
#[inline]
#[must_use]
pub fn unpacked(path: impl Into<PathBuf>) -> Self {
Self::Unpacked(path.into())
}
#[inline]
#[must_use]
pub fn packed(path: impl Into<PathBuf>) -> Self {
Self::Packed(path.into())
}
#[inline]
#[must_use]
pub fn base64(data: impl Into<String>) -> Self {
Self::Base64(data.into())
}
}
impl ExtensionSource {
#[inline]
#[must_use]
pub fn path(&self) -> Option<&PathBuf> {
match self {
Self::Unpacked(path) | Self::Packed(path) => Some(path),
Self::Base64(_) => None,
}
}
#[inline]
#[must_use]
pub fn is_unpacked(&self) -> bool {
matches!(self, Self::Unpacked(_))
}
#[inline]
#[must_use]
pub fn is_packed(&self) -> bool {
matches!(self, Self::Packed(_))
}
#[inline]
#[must_use]
pub fn is_base64(&self) -> bool {
matches!(self, Self::Base64(_))
}
}
impl From<PathBuf> for ExtensionSource {
fn from(path: PathBuf) -> Self {
if path.is_dir() {
Self::Unpacked(path)
} else {
Self::Packed(path)
}
}
}
impl From<&str> for ExtensionSource {
fn from(path: &str) -> Self {
Self::from(PathBuf::from(path))
}
}
impl From<String> for ExtensionSource {
fn from(path: String) -> Self {
Self::from(PathBuf::from(path))
}
}
#[cfg(test)]
mod tests {
use super::ExtensionSource;
use std::path::PathBuf;
#[test]
fn test_unpacked_constructor() {
let source = ExtensionSource::unpacked("./extension");
assert!(source.is_unpacked());
assert!(!source.is_packed());
assert!(!source.is_base64());
}
#[test]
fn test_packed_constructor() {
let source = ExtensionSource::packed("./extension.xpi");
assert!(source.is_packed());
assert!(!source.is_unpacked());
assert!(!source.is_base64());
}
#[test]
fn test_base64_constructor() {
let source = ExtensionSource::base64("UEsDBBQ...");
assert!(source.is_base64());
assert!(!source.is_unpacked());
assert!(!source.is_packed());
assert!(source.path().is_none());
}
#[test]
fn test_path_accessor() {
let unpacked = ExtensionSource::unpacked("./ext");
assert_eq!(unpacked.path(), Some(&PathBuf::from("./ext")));
let packed = ExtensionSource::packed("./ext.xpi");
assert_eq!(packed.path(), Some(&PathBuf::from("./ext.xpi")));
let base64 = ExtensionSource::base64("data");
assert_eq!(base64.path(), None);
}
#[test]
fn test_from_pathbuf_directory() {
let source = ExtensionSource::from(PathBuf::from("."));
assert!(source.is_unpacked());
}
#[test]
fn test_from_pathbuf_file() {
let source = ExtensionSource::from(PathBuf::from("./nonexistent.xpi"));
assert!(source.is_packed());
}
#[test]
fn test_from_str() {
let source = ExtensionSource::from("./extension");
assert!(source.path().is_some());
}
#[test]
fn test_from_string() {
let source = ExtensionSource::from(String::from("./extension"));
assert!(source.path().is_some());
}
#[test]
fn test_clone() {
let source = ExtensionSource::unpacked("./ext");
let cloned = source.clone();
assert_eq!(source, cloned);
}
#[test]
fn test_debug() {
let source = ExtensionSource::unpacked("./ext");
let debug_str = format!("{:?}", source);
assert!(debug_str.contains("Unpacked"));
}
}