use std::fmt;
use crate::path::pattern::{PathPattern, PathPatternError};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OutputSpec(PathPattern);
impl OutputSpec {
pub fn parse(s: &str) -> Result<Self, PathPatternError> {
PathPattern::parse(s).map(Self)
}
#[must_use]
pub fn pattern(&self) -> &PathPattern {
&self.0
}
#[must_use]
pub fn into_pattern(self) -> PathPattern {
self.0
}
}
impl fmt::Display for OutputSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg(test)]
mod tests {
use crate::path::OutputSpec;
#[test]
fn parses_literal_output() {
let s = OutputSpec::parse("dist/bundle.js").unwrap();
assert!(s.pattern().is_literal());
}
#[test]
fn parses_glob_output() {
let s = OutputSpec::parse("dist/**/*.js").unwrap();
assert!(s.pattern().is_glob());
}
#[test]
fn rejects_invalid_output() {
assert!(OutputSpec::parse("../escape").is_err());
}
#[test]
fn round_trip() {
let original = "dist/**/*.js";
let s = OutputSpec::parse(original).unwrap();
assert_eq!(s.to_string(), original);
let reparsed = OutputSpec::parse(&s.to_string()).unwrap();
assert_eq!(s, reparsed);
}
#[test]
fn input_and_output_specs_are_distinct_types() {
use crate::path::InputSpec;
let same_text = "src/lib.rs";
let _i = InputSpec::parse(same_text).unwrap();
let _o = OutputSpec::parse(same_text).unwrap();
}
}