0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-30 09:08:00 -04:00
denoland-deno/cli/media_type.rs

148 lines
4.1 KiB
Rust
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
2018-11-29 22:03:00 -05:00
use serde::Serialize;
use serde::Serializer;
use std::path::Path;
use std::path::PathBuf;
// Warning! The values in this enum are duplicated in tsc/99_main_compiler.js
// Update carefully!
2019-08-26 17:02:34 -04:00
#[allow(non_camel_case_types)]
#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Debug)]
2019-08-26 17:02:34 -04:00
pub enum MediaType {
JavaScript = 0,
2019-10-02 10:46:36 -04:00
JSX = 1,
TypeScript = 2,
Dts = 3,
TSX = 4,
Json = 5,
Wasm = 6,
BuildInfo = 7,
Unknown = 8,
}
impl<'a> From<&'a Path> for MediaType {
fn from(path: &'a Path) -> Self {
MediaType::from_path(path)
}
}
impl<'a> From<&'a PathBuf> for MediaType {
fn from(path: &'a PathBuf) -> Self {
MediaType::from_path(path)
}
}
impl<'a> From<&'a String> for MediaType {
fn from(specifier: &'a String) -> Self {
MediaType::from_path(&PathBuf::from(specifier))
}
}
impl MediaType {
fn from_path(path: &Path) -> Self {
match path.extension() {
None => MediaType::Unknown,
Some(os_str) => match os_str.to_str() {
Some("ts") => MediaType::TypeScript,
Some("tsx") => MediaType::TSX,
Some("js") => MediaType::JavaScript,
Some("jsx") => MediaType::JSX,
Some("mjs") => MediaType::JavaScript,
Some("cjs") => MediaType::JavaScript,
Some("json") => MediaType::Json,
Some("wasm") => MediaType::Wasm,
_ => MediaType::Unknown,
},
}
}
/// Convert a MediaType to a `ts.Extension`.
///
/// *NOTE* This is defined in TypeScript as a string based enum. Changes to
/// that enum in TypeScript should be reflected here.
pub fn as_ts_extension(&self) -> &str {
match self {
MediaType::JavaScript => ".js",
MediaType::JSX => ".jsx",
MediaType::TypeScript => ".ts",
MediaType::Dts => ".d.ts",
MediaType::TSX => ".tsx",
MediaType::Json => ".json",
// TypeScript doesn't have an "unknown", so we will treat WASM as JS for
// mapping purposes, though in reality, it is unlikely to ever be passed
// to the compiler.
MediaType::Wasm => ".js",
MediaType::BuildInfo => ".tsbuildinfo",
// TypeScript doesn't have an "unknown", so we will treat WASM as JS for
// mapping purposes, though in reality, it is unlikely to ever be passed
// to the compiler.
MediaType::Unknown => ".js",
}
}
2019-08-26 17:02:34 -04:00
}
impl Serialize for MediaType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let value = match self {
MediaType::JavaScript => 0 as i32,
MediaType::JSX => 1 as i32,
MediaType::TypeScript => 2 as i32,
MediaType::Dts => 3 as i32,
MediaType::TSX => 4 as i32,
MediaType::Json => 5 as i32,
MediaType::Wasm => 6 as i32,
MediaType::BuildInfo => 7 as i32,
MediaType::Unknown => 8 as i32,
};
Serialize::serialize(&value, serializer)
}
}
2019-08-26 17:02:34 -04:00
pub fn enum_name_media_type(mt: MediaType) -> &'static str {
match mt {
MediaType::JavaScript => "JavaScript",
2019-10-02 10:46:36 -04:00
MediaType::JSX => "JSX",
2019-08-26 17:02:34 -04:00
MediaType::TypeScript => "TypeScript",
MediaType::Dts => "Dts",
2019-10-02 10:46:36 -04:00
MediaType::TSX => "TSX",
2019-08-26 17:02:34 -04:00
MediaType::Json => "Json",
MediaType::Wasm => "Wasm",
MediaType::BuildInfo => "BuildInfo",
2019-08-26 17:02:34 -04:00
MediaType::Unknown => "Unknown",
}
}
2020-09-15 10:33:14 -04:00
#[test]
fn test_map_file_extension() {
assert_eq!(
MediaType::from(Path::new("foo/bar.ts")),
MediaType::TypeScript
);
assert_eq!(MediaType::from(Path::new("foo/bar.tsx")), MediaType::TSX);
assert_eq!(
MediaType::from(Path::new("foo/bar.d.ts")),
MediaType::TypeScript
);
assert_eq!(
MediaType::from(Path::new("foo/bar.js")),
MediaType::JavaScript
);
assert_eq!(MediaType::from(Path::new("foo/bar.jsx")), MediaType::JSX);
assert_eq!(MediaType::from(Path::new("foo/bar.json")), MediaType::Json);
assert_eq!(MediaType::from(Path::new("foo/bar.wasm")), MediaType::Wasm);
assert_eq!(
MediaType::from(Path::new("foo/bar.cjs")),
MediaType::JavaScript
);
assert_eq!(
MediaType::from(Path::new("foo/bar.txt")),
MediaType::Unknown
);
assert_eq!(MediaType::from(Path::new("foo/bar")), MediaType::Unknown);
}