use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SexContext {
#[default]
Pure,
Sex,
}
impl SexContext {
pub fn is_pure(&self) -> bool {
matches!(self, SexContext::Pure)
}
pub fn is_sex(&self) -> bool {
matches!(self, SexContext::Sex)
}
}
impl std::fmt::Display for SexContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SexContext::Pure => write!(f, "pure"),
SexContext::Sex => write!(f, "sex"),
}
}
}
#[derive(Debug, Clone)]
pub struct FileContext {
pub path: PathBuf,
pub sex_context: SexContext,
}
impl FileContext {
pub fn new(path: PathBuf) -> Self {
let sex_context = super::file_sex_context(&path);
Self { path, sex_context }
}
pub fn with_context(path: PathBuf, sex_context: SexContext) -> Self {
Self { path, sex_context }
}
pub fn is_sex(&self) -> bool {
self.sex_context.is_sex()
}
pub fn is_pure(&self) -> bool {
self.sex_context.is_pure()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sex_context_default() {
let ctx = SexContext::default();
assert_eq!(ctx, SexContext::Pure);
assert!(ctx.is_pure());
assert!(!ctx.is_sex());
}
#[test]
fn test_sex_context_display() {
assert_eq!(SexContext::Pure.to_string(), "pure");
assert_eq!(SexContext::Sex.to_string(), "sex");
}
#[test]
fn test_file_context_new() {
let ctx = FileContext::new(PathBuf::from("io.sex.dol"));
assert_eq!(ctx.sex_context, SexContext::Sex);
assert!(ctx.is_sex());
assert!(!ctx.is_pure());
}
#[test]
fn test_file_context_pure() {
let ctx = FileContext::new(PathBuf::from("container.dol"));
assert_eq!(ctx.sex_context, SexContext::Pure);
assert!(ctx.is_pure());
assert!(!ctx.is_sex());
}
#[test]
fn test_file_context_with_context() {
let ctx = FileContext::with_context(PathBuf::from("test.dol"), SexContext::Sex);
assert_eq!(ctx.sex_context, SexContext::Sex);
assert!(ctx.is_sex());
}
}