diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 08c8370e98..d20635235a 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -80,17 +80,6 @@ def rules_rust_dependencies(): url = "https://github.com/bazelbuild/apple_support/releases/download/1.3.0/apple_support.1.3.0.tar.gz", ) - # process_wrapper needs a low-dependency way to process json. - maybe( - http_archive, - name = "rules_rust_tinyjson", - sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16", - url = "https://crates.io/api/v1/crates/tinyjson/2.3.0/download", - strip_prefix = "tinyjson-2.3.0", - type = "tar.gz", - build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel", - ) - _RUST_TOOLCHAIN_VERSIONS = [ rust_common.default_version, "nightly/{}".format(DEFAULT_NIGHTLY_ISO_DATE), diff --git a/util/process_wrapper/BUILD.bazel b/util/process_wrapper/BUILD.bazel index c5276dd9cb..772f5d48ee 100644 --- a/util/process_wrapper/BUILD.bazel +++ b/util/process_wrapper/BUILD.bazel @@ -5,12 +5,9 @@ load("//rust/private:rust.bzl", "rust_binary_without_process_wrapper") rust_binary_without_process_wrapper( name = "process_wrapper", - srcs = glob(["*.rs"]), + srcs = glob(["**/*.rs"]), edition = "2018", visibility = ["//visibility:public"], - deps = [ - "@rules_rust_tinyjson//:tinyjson", - ], ) rust_test( diff --git a/util/process_wrapper/main.rs b/util/process_wrapper/main.rs index 6d985b34af..6492a1e1fc 100644 --- a/util/process_wrapper/main.rs +++ b/util/process_wrapper/main.rs @@ -16,6 +16,7 @@ mod flags; mod options; mod output; mod rustc; +mod tinyjson; mod util; use std::fs::{copy, OpenOptions}; diff --git a/util/process_wrapper/rustc.rs b/util/process_wrapper/rustc.rs index 67b75f9c36..116ba9a3b8 100644 --- a/util/process_wrapper/rustc.rs +++ b/util/process_wrapper/rustc.rs @@ -14,7 +14,7 @@ use std::convert::{TryFrom, TryInto}; -use tinyjson::JsonValue; +use crate::tinyjson::JsonValue; use crate::output::LineOutput; diff --git a/util/process_wrapper/tinyjson/LICENSE.txt b/util/process_wrapper/tinyjson/LICENSE.txt new file mode 100644 index 0000000000..24ad753e53 --- /dev/null +++ b/util/process_wrapper/tinyjson/LICENSE.txt @@ -0,0 +1,20 @@ +the MIT License + +Copyright (c) 2016 rhysd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR +THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/util/process_wrapper/tinyjson/README.md b/util/process_wrapper/tinyjson/README.md new file mode 100644 index 0000000000..9312aa49e1 --- /dev/null +++ b/util/process_wrapper/tinyjson/README.md @@ -0,0 +1,3 @@ +This directory contains code from [tinyjson](https://github.com/rhysd/tinyjson). + +It's vendored in rules_rust as a workaround for an issue where, if tinyjson were built as an independent crate, the resulting library would contain absolute path strings pointing into the Bazel sandbox directory, breaking build reproducibility. (https://github.com/bazelbuild/rules_rust/issues/1530) diff --git a/util/process_wrapper/tinyjson/generator.rs b/util/process_wrapper/tinyjson/generator.rs new file mode 100644 index 0000000000..0bd82d79f3 --- /dev/null +++ b/util/process_wrapper/tinyjson/generator.rs @@ -0,0 +1,99 @@ +use super::JsonValue; +use std::collections::HashMap; +use std::error; +use std::fmt; + +#[derive(Debug)] +pub struct JsonGenerateError { + msg: &'static str, +} + +impl JsonGenerateError { + fn new(msg: &'static str) -> Self { + JsonGenerateError { msg } + } + + pub fn message(&self) -> &str { + self.msg + } +} + +impl fmt::Display for JsonGenerateError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Generate error: {}", &self.msg) + } +} + +impl error::Error for JsonGenerateError {} + +pub type JsonGenerateResult = Result; + +fn quote(s: &str) -> String { + let mut to = '"'.to_string(); + for c in s.chars() { + match c { + '\\' => to.push_str("\\\\"), + '\u{0008}' => to.push_str("\\b"), + '\u{000c}' => to.push_str("\\f"), + '\n' => to.push_str("\\n"), + '\r' => to.push_str("\\r"), + '\t' => to.push_str("\\t"), + '"' => to.push_str("\\\""), + c if c.is_control() => to.push_str(&format!("\\u{:04x}", c as u32)), + c => to.push(c), + } + } + to.push('"'); + to +} + +fn array(array: &[JsonValue]) -> JsonGenerateResult { + let mut to = '['.to_string(); + for elem in array.iter() { + let s = stringify(elem)?; + to += &s; + to.push(','); + } + if !array.is_empty() { + to.pop(); // Remove trailing comma + } + to.push(']'); + Ok(to) +} + +fn object(m: &HashMap) -> JsonGenerateResult { + let mut to = '{'.to_string(); + for (k, v) in m { + to += "e(k); + to.push(':'); + let s = stringify(v)?; + to += &s; + to.push(','); + } + if !m.is_empty() { + to.pop(); // Remove trailing comma + } + to.push('}'); + Ok(to) +} + +fn number(f: f64) -> JsonGenerateResult { + if f.is_infinite() { + Err(JsonGenerateError::new("JSON cannot represent inf")) + } else if f.is_nan() { + Err(JsonGenerateError::new("JSON cannot represent NaN")) + } else { + Ok(f.to_string()) + } +} + +pub fn stringify(value: &JsonValue) -> JsonGenerateResult { + match value { + JsonValue::Number(n) => number(*n), + JsonValue::Boolean(b) => Ok(b.to_string()), + JsonValue::String(s) => Ok(quote(s)), + JsonValue::Null => Ok("null".to_string()), + JsonValue::Array(a) => array(a), + JsonValue::Object(o) => object(o), + } +} diff --git a/util/process_wrapper/tinyjson/json_value.rs b/util/process_wrapper/tinyjson/json_value.rs new file mode 100644 index 0000000000..9ca69c1f11 --- /dev/null +++ b/util/process_wrapper/tinyjson/json_value.rs @@ -0,0 +1,209 @@ +use super::generator::{stringify, JsonGenerateResult}; +use std::collections::HashMap; +use std::convert::TryInto; +use std::error; +use std::fmt; +use std::ops::{Index, IndexMut}; + +const NULL: () = (); + +#[derive(Debug, Clone, PartialEq)] +pub enum JsonValue { + Number(f64), + Boolean(bool), + String(String), + Null, + Array(Vec), + Object(HashMap), +} + +pub trait InnerAsRef { + fn json_value_as(v: &JsonValue) -> Option<&Self>; +} + +macro_rules! impl_inner_ref { + ($to:ty, $pat:pat => $val:expr) => { + impl InnerAsRef for $to { + fn json_value_as(v: &JsonValue) -> Option<&$to> { + use JsonValue::*; + match v { + $pat => Some($val), + _ => None, + } + } + } + }; +} + +impl_inner_ref!(f64, Number(n) => n); +impl_inner_ref!(bool, Boolean(b) => b); +impl_inner_ref!(String, String(s) => s); +impl_inner_ref!((), Null => &NULL); +impl_inner_ref!(Vec, Array(a) => a); +impl_inner_ref!(HashMap, Object(h) => h); + +pub trait InnerAsRefMut { + fn json_value_as_mut(v: &mut JsonValue) -> Option<&mut Self>; +} + +macro_rules! impl_inner_ref_mut { + ($to:ty, $pat:pat => $val:expr) => { + impl InnerAsRefMut for $to { + fn json_value_as_mut(v: &mut JsonValue) -> Option<&mut $to> { + use JsonValue::*; + match v { + $pat => Some($val), + _ => None, + } + } + } + }; +} + +impl_inner_ref_mut!(f64, Number(n) => n); +impl_inner_ref_mut!(bool, Boolean(b) => b); +impl_inner_ref_mut!(String, String(s) => s); +impl_inner_ref_mut!(Vec, Array(a) => a); +impl_inner_ref_mut!(HashMap, Object(h) => h); + +// Note: matches! is available from Rust 1.42 +macro_rules! is_xxx { + ($name:ident, $variant:pat) => { + pub fn $name(&self) -> bool { + match self { + $variant => true, + _ => false, + } + } + }; +} + +impl JsonValue { + pub fn get(&self) -> Option<&T> { + T::json_value_as(self) + } + + pub fn get_mut(&mut self) -> Option<&mut T> { + T::json_value_as_mut(self) + } + + is_xxx!(is_bool, JsonValue::Boolean(_)); + is_xxx!(is_number, JsonValue::Number(_)); + is_xxx!(is_string, JsonValue::String(_)); + is_xxx!(is_null, JsonValue::Null); + is_xxx!(is_array, JsonValue::Array(_)); + is_xxx!(is_object, JsonValue::Object(_)); + + pub fn stringify(&self) -> JsonGenerateResult { + stringify(self) + } +} + +impl<'a> Index<&'a str> for JsonValue { + type Output = JsonValue; + + fn index(&self, key: &'a str) -> &Self::Output { + let obj = match self { + JsonValue::Object(o) => o, + _ => panic!( + "Attempted to access to an object with key '{}' but actually it was {:?}", + key, self + ), + }; + + match obj.get(key) { + Some(json) => json, + None => panic!("Key '{}' was not found in {:?}", key, self), + } + } +} + +impl Index for JsonValue { + type Output = JsonValue; + + fn index(&self, index: usize) -> &'_ Self::Output { + let array = match self { + JsonValue::Array(a) => a, + _ => panic!( + "Attempted to access to an array with index {} but actually the value was {:?}", + index, self, + ), + }; + &array[index] + } +} + +impl<'a> IndexMut<&'a str> for JsonValue { + fn index_mut(&mut self, key: &'a str) -> &mut Self::Output { + let obj = match self { + JsonValue::Object(o) => o, + _ => panic!( + "Attempted to access to an object with key '{}' but actually it was {:?}", + key, self + ), + }; + + if let Some(json) = obj.get_mut(key) { + json + } else { + panic!("Key '{}' was not found in object", key) + } + } +} + +impl IndexMut for JsonValue { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + let array = match self { + JsonValue::Array(a) => a, + _ => panic!( + "Attempted to access to an array with index {} but actually the value was {:?}", + index, self, + ), + }; + + &mut array[index] + } +} + +#[derive(Debug)] +pub struct UnexpectedValue { + value: JsonValue, + expected: &'static str, +} + +impl fmt::Display for UnexpectedValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Unexpected JSON value: {:?}. Expected {} value", + self.value, self.expected + ) + } +} + +impl error::Error for UnexpectedValue {} + +macro_rules! impl_try_into { + ($ty:ty, $pat:pat => $val:expr) => { + impl TryInto<$ty> for JsonValue { + type Error = UnexpectedValue; + + fn try_into(self) -> Result<$ty, UnexpectedValue> { + match self { + $pat => Ok($val), + v => Err(UnexpectedValue { + value: v, + expected: stringify!($ty), + }), + } + } + } + }; +} + +impl_try_into!(f64, JsonValue::Number(n) => n); +impl_try_into!(bool, JsonValue::Boolean(b) => b); +impl_try_into!(String, JsonValue::String(s) => s); +impl_try_into!((), JsonValue::Null => ()); +impl_try_into!(Vec, JsonValue::Array(a) => a); +impl_try_into!(HashMap, JsonValue::Object(o) => o); diff --git a/util/process_wrapper/tinyjson/mod.rs b/util/process_wrapper/tinyjson/mod.rs new file mode 100644 index 0000000000..377c014bf9 --- /dev/null +++ b/util/process_wrapper/tinyjson/mod.rs @@ -0,0 +1,7 @@ +#![allow(dead_code)] + +mod generator; +mod json_value; +mod parser; + +pub use json_value::JsonValue; diff --git a/util/process_wrapper/tinyjson/parser.rs b/util/process_wrapper/tinyjson/parser.rs new file mode 100644 index 0000000000..0e300f4cb4 --- /dev/null +++ b/util/process_wrapper/tinyjson/parser.rs @@ -0,0 +1,413 @@ +use std::char; +use std::collections::HashMap; +use std::error; +use std::fmt; +use std::iter::Peekable; +use std::str::FromStr; + +use super::JsonValue; + +#[derive(Debug)] +pub struct JsonParseError { + msg: String, + line: usize, + col: usize, +} + +impl JsonParseError { + fn new(msg: String, line: usize, col: usize) -> JsonParseError { + JsonParseError { msg, line, col } + } +} + +impl fmt::Display for JsonParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Parse error at line:{}, col:{}: {}", + self.line, self.col, &self.msg, + ) + } +} + +impl error::Error for JsonParseError {} + +pub type JsonParseResult = Result; + +// Note: char::is_ascii_whitespace is not available because some characters are not defined as +// whitespace character in JSON spec. For example, U+000C FORM FEED is whitespace in Rust but +// it isn't in JSON. +fn is_whitespace(c: char) -> bool { + matches!(c, '\u{0020}' | '\u{000a}' | '\u{000d}' | '\u{0009}') +} + +pub struct JsonParser +where + I: Iterator, +{ + chars: Peekable, + line: usize, + col: usize, +} + +impl> JsonParser { + pub fn new(it: I) -> Self { + JsonParser { + chars: it.peekable(), + line: 1, + col: 0, + } + } + + fn err(&self, msg: String) -> Result { + Err(JsonParseError::new(msg, self.line, self.col)) + } + + fn unexpected_eof(&self) -> Result { + Err(JsonParseError::new( + String::from("Unexpected EOF"), + self.line, + self.col, + )) + } + + fn next_pos(&mut self, c: char) { + if c == '\n' { + self.col = 0; + self.line += 1; + } else { + self.col += 1; + } + } + + fn peek(&mut self) -> Result { + while let Some(c) = self.chars.peek().copied() { + if !is_whitespace(c) { + return Ok(c); + } + self.next_pos(c); + self.chars.next().unwrap(); + } + self.unexpected_eof() + } + + fn next(&mut self) -> Option { + while let Some(c) = self.chars.next() { + self.next_pos(c); + if !is_whitespace(c) { + return Some(c); + } + } + None + } + + fn consume(&mut self) -> Result { + if let Some(c) = self.next() { + Ok(c) + } else { + self.unexpected_eof() + } + } + + fn consume_no_skip(&mut self) -> Result { + if let Some(c) = self.chars.next() { + self.next_pos(c); + Ok(c) + } else { + self.unexpected_eof() + } + } + + fn parse_object(&mut self) -> JsonParseResult { + if self.consume()? != '{' { + return self.err(String::from("Object must starts with '{'")); + } + + if self.peek()? == '}' { + self.consume().unwrap(); + return Ok(JsonValue::Object(HashMap::new())); + } + + let mut m = HashMap::new(); + loop { + let key = match self.parse_any()? { + JsonValue::String(s) => s, + v => return self.err(format!("Key of object must be string but found {:?}", v)), + }; + + let c = self.consume()?; + if c != ':' { + return self.err(format!( + "':' is expected after key of object but actually found '{}'", + c + )); + } + + m.insert(key, self.parse_any()?); + + match self.consume()? { + ',' => {} + '}' => return Ok(JsonValue::Object(m)), + c => { + return self.err(format!( + "',' or '}}' is expected for object but actually found '{}'", + c.escape_debug(), + )) + } + } + } + } + + fn parse_array(&mut self) -> JsonParseResult { + if self.consume()? != '[' { + return self.err(String::from("Array must starts with '['")); + } + + if self.peek()? == ']' { + self.consume().unwrap(); + return Ok(JsonValue::Array(vec![])); + } + + let mut v = vec![self.parse_any()?]; + loop { + match self.consume()? { + ',' => {} + ']' => return Ok(JsonValue::Array(v)), + c => { + return self.err(format!( + "',' or ']' is expected for array but actually found '{}'", + c + )) + } + } + + v.push(self.parse_any()?); // Next element + } + } + + fn push_utf16(&self, s: &mut String, utf16: &mut Vec) -> Result<(), JsonParseError> { + if utf16.is_empty() { + return Ok(()); + } + + match String::from_utf16(utf16) { + Ok(utf8) => s.push_str(&utf8), + Err(err) => return self.err(format!("Invalid UTF-16 sequence {:?}: {}", &utf16, err)), + } + utf16.clear(); + Ok(()) + } + + fn parse_string(&mut self) -> JsonParseResult { + if self.consume()? != '"' { + return self.err(String::from("String must starts with double quote")); + } + + let mut utf16 = Vec::new(); // Buffer for parsing \uXXXX UTF-16 characters + let mut s = String::new(); + loop { + let c = match self.consume_no_skip()? { + '\\' => match self.consume_no_skip()? { + '\\' => '\\', + '/' => '/', + '"' => '"', + 'b' => '\u{0008}', + 'f' => '\u{000c}', + 'n' => '\n', + 'r' => '\r', + 't' => '\t', + 'u' => { + let mut u = 0u16; + for _ in 0..4 { + let c = self.consume()?; + if let Some(h) = c.to_digit(16) { + u = u * 0x10 + h as u16; + } else { + return self.err(format!("Unicode character must be \\uXXXX (X is hex character) format but found character '{}'", c)); + } + } + utf16.push(u); + // Additional \uXXXX character may follow. UTF-16 characters must be converted + // into UTF-8 string as sequence because surrogate pairs must be considered + // like "\uDBFF\uDFFF". + continue; + } + c => return self.err(format!("'\\{}' is invalid escaped character", c)), + }, + '"' => { + self.push_utf16(&mut s, &mut utf16)?; + return Ok(JsonValue::String(s)); + } + // Note: c.is_control() is not available here because JSON accepts 0x7f (DEL) in + // string literals but 0x7f is control character. + // Rough spec of JSON says string literal cannot contain control characters. But it + // can actually contain 0x7f. + c if (c as u32) < 0x20 => { + return self.err(format!( + "String cannot contain control character {}", + c.escape_debug(), + )); + } + c => c, + }; + + self.push_utf16(&mut s, &mut utf16)?; + + s.push(c); + } + } + + fn parse_constant(&mut self, s: &'static str) -> Option { + for c in s.chars() { + match self.consume_no_skip() { + Ok(x) if x != c => { + return Some(JsonParseError::new( + format!("Unexpected character '{}' while parsing '{}'", c, s), + self.line, + self.col, + )); + } + Ok(_) => {} + Err(e) => return Some(e), + } + } + None + } + + fn parse_null(&mut self) -> JsonParseResult { + match self.parse_constant("null") { + Some(err) => Err(err), + None => Ok(JsonValue::Null), + } + } + + fn parse_true(&mut self) -> JsonParseResult { + match self.parse_constant("true") { + Some(err) => Err(err), + None => Ok(JsonValue::Boolean(true)), + } + } + + fn parse_false(&mut self) -> JsonParseResult { + match self.parse_constant("false") { + Some(err) => Err(err), + None => Ok(JsonValue::Boolean(false)), + } + } + + fn parse_number(&mut self) -> JsonParseResult { + let neg = if self.peek()? == '-' { + self.consume_no_skip().unwrap(); + true + } else { + false + }; + + let mut s = String::new(); + let mut saw_dot = false; + let mut saw_exp = false; + + while let Some(d) = self.chars.peek() { + match d { + '0'..='9' => s.push(*d), + '.' => { + saw_dot = true; + break; + } + 'e' | 'E' => { + saw_exp = true; + break; + } + _ => break, + } + self.consume_no_skip().unwrap(); + } + + if s.is_empty() { + return self.err("Integer part must not be empty in number literal".to_string()); + } + + if s.starts_with('0') && s.len() > 1 { + return self + .err("Integer part of number must not start with 0 except for '0'".to_string()); + } + + if saw_dot { + s.push(self.consume_no_skip().unwrap()); // eat '.' + while let Some(d) = self.chars.peek() { + match d { + '0'..='9' => s.push(*d), + 'e' | 'E' => { + saw_exp = true; + break; + } + _ => break, + } + self.consume_no_skip().unwrap(); + } + if s.ends_with('.') { + return self.err("Fraction part of number must not be empty".to_string()); + } + } + + if saw_exp { + s.push(self.consume_no_skip().unwrap()); // eat 'e' or 'E' + if let Some('+') | Some('-') = self.chars.peek() { + s.push(self.consume_no_skip().unwrap()); + } + + let mut saw_digit = false; + while let Some(d) = self.chars.peek() { + match d { + '0'..='9' => s.push(*d), + _ => break, + } + saw_digit = true; + self.consume_no_skip().unwrap(); + } + + if !saw_digit { + return self.err("Exponent part must not be empty in number literal".to_string()); + } + } + + match s.parse::() { + Ok(n) => Ok(JsonValue::Number(if neg { -n } else { n })), + Err(err) => self.err(format!("Invalid number literal '{}': {}", s, err)), + } + } + + fn parse_any(&mut self) -> JsonParseResult { + match self.peek()? { + '0'..='9' | '-' => self.parse_number(), + '"' => self.parse_string(), + '[' => self.parse_array(), + '{' => self.parse_object(), + 't' => self.parse_true(), + 'f' => self.parse_false(), + 'n' => self.parse_null(), + c => self.err(format!("Invalid character: {}", c.escape_debug())), + } + } + + pub fn parse(&mut self) -> JsonParseResult { + let v = self.parse_any()?; + + if let Some(c) = self.next() { + return self.err(format!( + "Expected EOF but got character '{}'", + c.escape_debug(), + )); + } + + Ok(v) + } +} + +impl FromStr for JsonValue { + type Err = JsonParseError; + + fn from_str(s: &str) -> Result { + JsonParser::new(s.chars()).parse() + } +}