1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

tsc: use serde to (de)serialize CompiledFileMetadata (#5481)

Co-authored-by: Bert Belder <bertbelder@gmail.com>
This commit is contained in:
Yiyu Lin 2020-05-17 03:47:26 +08:00 committed by GitHub
parent bfd4baf2d3
commit f12dffca9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,6 +30,7 @@ use futures::future::FutureExt;
use log::info; use log::info;
use regex::Regex; use regex::Regex;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize;
use serde_json::json; use serde_json::json;
use serde_json::Value; use serde_json::Value;
use sourcemap::SourceMap; use sourcemap::SourceMap;
@ -180,45 +181,24 @@ impl CompilerConfig {
/// Includes source code path and state hash. /// Includes source code path and state hash.
/// version_hash is used to validate versions of the file /// version_hash is used to validate versions of the file
/// and could be used to remove stale file in cache. /// and could be used to remove stale file in cache.
#[derive(Deserialize, Serialize)]
pub struct CompiledFileMetadata { pub struct CompiledFileMetadata {
pub source_path: PathBuf, pub source_path: PathBuf,
pub version_hash: String, pub version_hash: String,
} }
static SOURCE_PATH: &str = "source_path";
static VERSION_HASH: &str = "version_hash";
impl CompiledFileMetadata { impl CompiledFileMetadata {
pub fn from_json_string(metadata_string: String) -> Option<Self> { pub fn from_json_string(
// TODO: use serde for deserialization metadata_string: String,
let maybe_metadata_json: serde_json::Result<serde_json::Value> = ) -> Result<Self, serde_json::Error> {
serde_json::from_str(&metadata_string); serde_json::from_str::<Self>(&metadata_string)
if let Ok(metadata_json) = maybe_metadata_json {
let source_path = metadata_json[SOURCE_PATH].as_str().map(PathBuf::from);
let version_hash = metadata_json[VERSION_HASH].as_str().map(String::from);
if source_path.is_none() || version_hash.is_none() {
return None;
}
return Some(CompiledFileMetadata {
source_path: source_path.unwrap(),
version_hash: version_hash.unwrap(),
});
}
None
} }
pub fn to_json_string(&self) -> Result<String, serde_json::Error> { pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
let mut value_map = serde_json::map::Map::new(); serde_json::to_string(self)
}
}
value_map.insert(SOURCE_PATH.to_owned(), json!(&self.source_path));
value_map.insert(VERSION_HASH.to_string(), json!(&self.version_hash));
serde_json::to_string(&value_map)
}
}
/// Creates the JSON message send to compiler.ts's onmessage. /// Creates the JSON message send to compiler.ts's onmessage.
fn req( fn req(
request_type: msg::CompilerRequestType, request_type: msg::CompilerRequestType,
@ -522,7 +502,7 @@ impl TsCompiler {
.get_cache_filename_with_extension(url, "meta"); .get_cache_filename_with_extension(url, "meta");
if let Ok(metadata_bytes) = self.disk_cache.get(&cache_key) { if let Ok(metadata_bytes) = self.disk_cache.get(&cache_key) {
if let Ok(metadata) = std::str::from_utf8(&metadata_bytes) { if let Ok(metadata) = std::str::from_utf8(&metadata_bytes) {
if let Some(read_metadata) = if let Ok(read_metadata) =
CompiledFileMetadata::from_json_string(metadata.to_string()) CompiledFileMetadata::from_json_string(metadata.to_string())
{ {
return Some(read_metadata); return Some(read_metadata);