git.delta.rocks / jrsonnet / refs/commits / 3fc6c25f159a

difftreelog

style fix clippy warnings

Yaroslav Bolyukin2021-01-24parent: #2634495.patch.diff
in: master

9 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
42 }42 }
43 }43 }
44 Val::Null => buf.push_str("null"),44 Val::Null => buf.push_str("null"),
45 Val::Str(s) => buf.push_str(&escape_string_json(&s)),45 Val::Str(s) => buf.push_str(&escape_string_json(s)),
46 Val::Num(n) => write!(buf, "{}", n).unwrap(),46 Val::Num(n) => write!(buf, "{}", n).unwrap(),
47 Val::Arr(items) => {47 Val::Arr(items) => {
48 buf.push('[');48 buf.push('[');
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
445 },445 },
446 Val::Arr(a) => {446 Val::Arr(a) => {
447 base64::encode(a.iter().map(|v| {447 base64::encode(a.iter().map(|v| {
448 Ok(v?.clone().unwrap_num()? as u8)448 Ok(v?.unwrap_num()? as u8)
449 }).collect::<Result<Vec<_>>>()?).into()449 }).collect::<Result<Vec<_>>>()?).into()
450 },450 },
451 _ => unreachable!()451 _ => unreachable!()
589 name: &str,589 name: &str,
590 args: &ArgsDesc,590 args: &ArgsDesc,
591) -> Result<Val> {591) -> Result<Val> {
592 if let Some(f) = BUILTINS.with(|builtins| builtins.get(name).map(|f| *f)) {592 if let Some(f) = BUILTINS.with(|builtins| builtins.get(name).copied()) {
593 return Ok(f(context, loc, args)?);593 return Ok(f(context, loc, args)?);
594 }594 }
595 throw!(IntrinsicNotFound(name.into()))595 throw!(IntrinsicNotFound(name.into()))
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
473 }473 }
474 v.get(n as usize)?474 v.get(n as usize)?
475 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?475 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
476 .clone()
477 }476 }
478 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),477 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
479 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(478 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
1#![allow(clippy::type_complexity)]
2
1use crate::{error::Result, Val};3use crate::{error::Result, Val};
2use jrsonnet_parser::ParamsDesc;4use jrsonnet_parser::ParamsDesc;
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
34 }34 }
35 let mut debug = f.debug_struct("ObjValue");35 let mut debug = f.debug_struct("ObjValue");
36 for (name, member) in self.0.this_entries.iter() {36 for (name, member) in self.0.this_entries.iter() {
37 debug.field(&name, member);37 debug.field(name, member);
38 }38 }
39 #[cfg(feature = "unstable")]39 #[cfg(feature = "unstable")]
40 {40 {
140 .evaluate()?)140 .evaluate()?)
141 }141 }
142142
143 pub fn ptr_eq(a: &ObjValue, b: &ObjValue) -> bool {143 pub fn ptr_eq(a: &Self, b: &Self) -> bool {
144 Rc::ptr_eq(&a.0, &b.0)144 Rc::ptr_eq(&a.0, &b.0)
145 }145 }
146}146}
modifiedcrates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth
29pub struct TypeLocError(Box<TypeError>, ValuePathStack);29pub struct TypeLocError(Box<TypeError>, ValuePathStack);
30impl From<TypeError> for TypeLocError {30impl From<TypeError> for TypeLocError {
31 fn from(e: TypeError) -> Self {31 fn from(e: TypeError) -> Self {
32 TypeLocError(Box::new(e), ValuePathStack(Vec::new()))32 Self(Box::new(e), ValuePathStack(Vec::new()))
33 }33 }
34}34}
35impl From<TypeLocError> for LocError {35impl From<TypeLocError> for LocError {
61 write!(out, "{}", err)?;61 write!(out, "{}", err)?;
6262
63 for (i, line) in out.lines().enumerate() {63 for (i, line) in out.lines().enumerate() {
64 if line.trim().len() == 0 {64 if line.trim().is_empty() {
65 continue;65 continue;
66 }66 }
67 if i != 0 {67 if i != 0 {
118impl Display for ValuePathItem {118impl Display for ValuePathItem {
119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120 match self {120 match self {
121 ValuePathItem::Field(name) => write!(f, ".{}", name)?,121 Self::Field(name) => write!(f, ".{}", name)?,
122 ValuePathItem::Index(idx) => write!(f, "[{}]", idx)?,122 Self::Index(idx) => write!(f, "[{}]", idx)?,
123 }123 }
124 Ok(())124 Ok(())
125 }125 }
140impl CheckType for ComplexValType {140impl CheckType for ComplexValType {
141 fn check(&self, value: &Val) -> Result<()> {141 fn check(&self, value: &Val) -> Result<()> {
142 match self {142 match self {
143 ComplexValType::Any => Ok(()),143 Self::Any => Ok(()),
144 ComplexValType::Simple(s) => s.check(value),144 Self::Simple(s) => s.check(value),
145 ComplexValType::Char => match value {145 Self::Char => match value {
146 Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),146 Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),
147 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),147 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
148 },148 },
149 ComplexValType::BoundedNumber(from, to) => {149 Self::BoundedNumber(from, to) => {
150 if let Val::Num(n) = value {150 if let Val::Num(n) = value {
151 if from.map(|from| from > *n).unwrap_or(false)151 if from.map(|from| from > *n).unwrap_or(false)
152 || to.map(|to| to <= *n).unwrap_or(false)152 || to.map(|to| to <= *n).unwrap_or(false)
153 {153 {
154 return Err(TypeError::BoundsFailed(*n, from.clone(), to.clone()).into());154 return Err(TypeError::BoundsFailed(*n, *from, *to).into());
155 }155 }
156 Ok(())156 Ok(())
157 } else {157 } else {
158 Err(TypeError::ExpectedGot(self.clone(), value.value_type()).into())158 Err(TypeError::ExpectedGot(self.clone(), value.value_type()).into())
159 }159 }
160 }160 }
161 ComplexValType::Array(elem_type) => match value {161 Self::Array(elem_type) => match value {
162 Val::Arr(a) => {162 Val::Arr(a) => {
163 for (i, item) in a.iter().enumerate() {163 for (i, item) in a.iter().enumerate() {
164 push_type(164 push_type(
170 }170 }
171 Ok(())171 Ok(())
172 }172 }
173 v => return Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),173 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
174 },174 },
175 ComplexValType::ArrayRef(elem_type) => match value {175 Self::ArrayRef(elem_type) => match value {
176 Val::Arr(a) => {176 Val::Arr(a) => {
177 for (i, item) in a.iter().enumerate() {177 for (i, item) in a.iter().enumerate() {
178 push_type(178 push_type(
184 }184 }
185 Ok(())185 Ok(())
186 }186 }
187 v => return Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),187 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
188 },188 },
189 ComplexValType::ObjectRef(elems) => match value {189 Self::ObjectRef(elems) => match value {
190 Val::Obj(obj) => {190 Val::Obj(obj) => {
191 for (k, v) in elems.iter() {191 for (k, v) in elems.iter() {
192 if let Some(got_v) = obj.get((*k).into())? {192 if let Some(got_v) = obj.get((*k).into())? {
202 );202 );
203 }203 }
204 }204 }
205 return Ok(());205 Ok(())
206 }206 }
207 v => return Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),207 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
208 },208 },
209 ComplexValType::Union(types) => {209 Self::Union(types) => {
210 let mut errors = Vec::new();210 let mut errors = Vec::new();
211 for ty in types.iter() {211 for ty in types.iter() {
212 match ty.check(value) {212 match ty.check(value) {
219 },219 },
220 }220 }
221 }221 }
222 return Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into());222 Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())
223 }223 }
224 ComplexValType::UnionRef(types) => {224 Self::UnionRef(types) => {
225 let mut errors = Vec::new();225 let mut errors = Vec::new();
226 for ty in types.iter() {226 for ty in types.iter() {
227 match ty.check(value) {227 match ty.check(value) {
234 },234 },
235 }235 }
236 }236 }
237 return Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into());237 Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())
238 }238 }
239 ComplexValType::Sum(types) => {239 Self::Sum(types) => {
240 for ty in types.iter() {240 for ty in types.iter() {
241 ty.check(value)?241 ty.check(value)?
242 }242 }
243 Ok(())243 Ok(())
244 }244 }
245 ComplexValType::SumRef(types) => {245 Self::SumRef(types) => {
246 for ty in types.iter() {246 for ty in types.iter() {
247 ty.check(value)?247 ty.check(value)?
248 }248 }
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
176pub enum ArrValue {176pub enum ArrValue {
177 Lazy(Rc<Vec<LazyVal>>),177 Lazy(Rc<Vec<LazyVal>>),
178 Eager(Rc<Vec<Val>>),178 Eager(Rc<Vec<Val>>),
179 Extended(Box<(ArrValue, ArrValue)>),179 Extended(Box<(Self, Self)>),
180}180}
181impl ArrValue {181impl ArrValue {
182 pub fn len(&self) -> usize {182 pub fn len(&self) -> usize {
183 match self {183 match self {
184 ArrValue::Lazy(l) => l.len(),184 Self::Lazy(l) => l.len(),
185 ArrValue::Eager(e) => e.len(),185 Self::Eager(e) => e.len(),
186 ArrValue::Extended(v) => v.0.len() + v.1.len(),186 Self::Extended(v) => v.0.len() + v.1.len(),
187 }187 }
188 }188 }
189189
193193
194 pub fn get(&self, index: usize) -> Result<Option<Val>> {194 pub fn get(&self, index: usize) -> Result<Option<Val>> {
195 match self {195 match self {
196 ArrValue::Lazy(vec) => {196 Self::Lazy(vec) => {
197 if let Some(v) = vec.get(index) {197 if let Some(v) = vec.get(index) {
198 Ok(Some(v.evaluate()?))198 Ok(Some(v.evaluate()?))
199 } else {199 } else {
200 Ok(None)200 Ok(None)
201 }201 }
202 }202 }
203 ArrValue::Eager(vec) => Ok(vec.get(index).cloned()),203 Self::Eager(vec) => Ok(vec.get(index).cloned()),
204 ArrValue::Extended(v) => {204 Self::Extended(v) => {
205 let a_len = v.0.len();205 let a_len = v.0.len();
206 if a_len > index {206 if a_len > index {
207 v.0.get(index)207 v.0.get(index)
214214
215 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {215 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
216 match self {216 match self {
217 ArrValue::Lazy(vec) => vec.get(index).cloned(),217 Self::Lazy(vec) => vec.get(index).cloned(),
218 ArrValue::Eager(vec) => vec218 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
219 .get(index)
220 .cloned()
221 .map(|val| LazyVal::new_resolved(val)),
222 ArrValue::Extended(v) => {219 Self::Extended(v) => {
223 let a_len = v.0.len();220 let a_len = v.0.len();
224 if a_len > index {221 if a_len > index {
225 v.0.get_lazy(index)222 v.0.get_lazy(index)
232229
233 pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {230 pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {
234 Ok(match self {231 Ok(match self {
235 ArrValue::Lazy(vec) => {232 Self::Lazy(vec) => {
236 let mut out = Vec::with_capacity(vec.len());233 let mut out = Vec::with_capacity(vec.len());
237 for item in vec.iter() {234 for item in vec.iter() {
238 out.push(item.evaluate()?);235 out.push(item.evaluate()?);
239 }236 }
240 Rc::new(out)237 Rc::new(out)
241 }238 }
242 ArrValue::Eager(vec) => vec.clone(),239 Self::Eager(vec) => vec.clone(),
243 ArrValue::Extended(v) => {240 Self::Extended(_v) => {
244 let mut out = Vec::with_capacity(self.len());241 let mut out = Vec::with_capacity(self.len());
245 for item in self.iter() {242 for item in self.iter() {
246 out.push(item?);243 out.push(item?);
252249
253 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {250 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
254 (0..self.len()).map(move |idx| match self {251 (0..self.len()).map(move |idx| match self {
255 ArrValue::Lazy(l) => l[idx].evaluate(),252 Self::Lazy(l) => l[idx].evaluate(),
256 ArrValue::Eager(e) => Ok(e[idx].clone()),253 Self::Eager(e) => Ok(e[idx].clone()),
257 ArrValue::Extended(_) => self.get(idx).map(|e| e.unwrap()),254 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
258 })255 })
259 }256 }
260257
261 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {258 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
262 (0..self.len()).map(move |idx| match self {259 (0..self.len()).map(move |idx| match self {
263 ArrValue::Lazy(l) => l[idx].clone(),260 Self::Lazy(l) => l[idx].clone(),
264 ArrValue::Eager(e) => LazyVal::new_resolved(e[idx].clone()),261 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
265 ArrValue::Extended(_) => self.get_lazy(idx).unwrap(),262 Self::Extended(_) => self.get_lazy(idx).unwrap(),
266 })263 })
267 }264 }
268265
269 pub fn reversed(self) -> Self {266 pub fn reversed(self) -> Self {
270 match self {267 match self {
271 ArrValue::Lazy(vec) => {268 Self::Lazy(vec) => {
272 let mut out = (&vec as &Vec<_>).clone();269 let mut out = (&vec as &Vec<_>).clone();
273 out.reverse();270 out.reverse();
274 Self::Lazy(Rc::new(out))271 Self::Lazy(Rc::new(out))
275 }272 }
276 ArrValue::Eager(vec) => {273 Self::Eager(vec) => {
277 let mut out = (&vec as &Vec<_>).clone();274 let mut out = (&vec as &Vec<_>).clone();
278 out.reverse();275 out.reverse();
279 Self::Eager(Rc::new(out))276 Self::Eager(Rc::new(out))
280 }277 }
281 ArrValue::Extended(b) => ArrValue::Extended(Box::new((b.1.reversed(), b.0.reversed()))),278 Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
282 }279 }
283 }280 }
284281
285 pub fn ptr_eq(a: &ArrValue, b: &ArrValue) -> bool {282 pub fn ptr_eq(a: &Self, b: &Self) -> bool {
286 match (a, b) {283 match (a, b) {
287 (ArrValue::Lazy(a), ArrValue::Lazy(b)) => Rc::ptr_eq(a, b),284 (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),
288 (ArrValue::Eager(a), ArrValue::Eager(b)) => Rc::ptr_eq(a, b),285 (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),
289 _ => false,286 _ => false,
290 }287 }
291 }288 }
359 self.assert_type(context, ValType::Num)?;356 self.assert_type(context, ValType::Num)?;
360 self.unwrap_num()357 self.unwrap_num()
361 }358 }
362 pub fn value_type(&self) -> ValType {359 pub const fn value_type(&self) -> ValType {
363 match self {360 match self {
364 Self::Str(..) => ValType::Str,361 Self::Str(..) => ValType::Str,
365 Self::Num(..) => ValType::Num,362 Self::Num(..) => ValType::Num,
378 Self::Null => "null".into(),375 Self::Null => "null".into(),
379 Self::Str(s) => s.clone(),376 Self::Str(s) => s.clone(),
380 v => manifest_json_ex(377 v => manifest_json_ex(
381 &v,378 v,
382 &ManifestJsonOptions {379 &ManifestJsonOptions {
383 padding: "",380 padding: "",
384 mtype: ManifestType::ToString,381 mtype: ManifestType::ToString,
556 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(553 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(
557 "primitiveEquals operates on primitive types, got object".into(),554 "primitiveEquals operates on primitive types, got object".into(),
558 )),555 )),
559 (a, b) if is_function_like(&a) && is_function_like(&b) => {556 (a, b) if is_function_like(a) && is_function_like(b) => {
560 throw!(RuntimeError("cannot test equality of functions".into()))557 throw!(RuntimeError("cannot test equality of functions".into()))
561 }558 }
562 (_, _) => false,559 (_, _) => false,
598 }595 }
599 Ok(true)596 Ok(true)
600 }597 }
601 (a, b) => Ok(primitive_equals(&a, &b)?),598 (a, b) => Ok(primitive_equals(a, b)?),
602 }599 }
603}600}
604601
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
68 IStr(STR_POOL.with(|pool| {68 IStr(STR_POOL.with(|pool| {
69 let mut pool = pool.borrow_mut();69 let mut pool = pool.borrow_mut();
70 if let Some((k, _)) = pool.get_key_value(str) {70 if let Some((k, _)) = pool.get_key_value(str) {
71 return k.clone();71 k.clone()
72 } else {72 } else {
73 let rc: Rc<str> = str.into();73 let rc: Rc<str> = str.into();
74 pool.insert(rc.clone(), ());74 pool.insert(rc.clone(), ());
modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
133 union: &[ComplexValType],133 union: &[ComplexValType],
134) -> std::fmt::Result {134) -> std::fmt::Result {
135 for (i, v) in union.iter().enumerate() {135 for (i, v) in union.iter().enumerate() {
136 let should_add_braces = match v {136 let should_add_braces =
137 ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union => true,137 matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);
138 _ => false,
139 };
140 if i != 0 {138 if i != 0 {
141 write!(f, " {} ", if is_union { '|' } else { '&' })?;139 write!(f, " {} ", if is_union { '|' } else { '&' })?;
142 }140 }