From 27958b04316fb05a6fb35534b0a06404b64e35ca Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Mon, 23 Mar 2026 01:25:06 +0000 Subject: [PATCH] refactor: cleanup --- --- a/bindings/jsonnet/src/lib.rs +++ b/bindings/jsonnet/src/lib.rs @@ -45,8 +45,8 @@ /// If this does not match `LIB_JSONNET_VERSION` /// then there is a mismatch between header and compiled library. #[no_mangle] -pub extern "C" fn jsonnet_version() -> &'static [u8; 8] { - b"v0.20.0\0" +pub extern "C" fn jsonnet_version() -> &'static [u8; 12] { + b"v0.22.0-rc1\0" } unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> { --- a/crates/jrsonnet-evaluator/src/async_import.rs +++ b/crates/jrsonnet-evaluator/src/async_import.rs @@ -3,11 +3,7 @@ use jrsonnet_gcmodule::Acyclic; use jrsonnet_ir::visit::Visitor; -use jrsonnet_ir::{ - ArgsDesc, AssertExpr, AssertStmt, BindSpec, CompSpec, Destruct, Expr, ExprParam, ExprParams, - FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData, ImportKind, ObjBody, Slice, - SliceDesc, Source, SourcePath, Spanned, -}; +use jrsonnet_ir::{IStr, Source, SourcePath}; use rustc_hash::FxHashMap; use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, State}; @@ -23,7 +19,7 @@ self.0.push(Import { path: ResolvePathOwned::Str(value.to_string()), expression, - }) + }); } } --- a/crates/jrsonnet-evaluator/src/lib.rs +++ b/crates/jrsonnet-evaluator/src/lib.rs @@ -5,7 +5,7 @@ extern crate self as jrsonnet_evaluator; mod arr; -// pub mod async_import; +pub mod async_import; mod ctx; mod dynamic; pub mod error; --- a/crates/jrsonnet-evaluator/src/obj/mod.rs +++ b/crates/jrsonnet-evaluator/src/obj/mod.rs @@ -1,5 +1,13 @@ use std::{ - any::Any, cell::{Cell, RefCell}, clone::Clone, cmp::Reverse, collections::hash_map::Entry, fmt::{self, Debug}, hash::{Hash, Hasher}, num::Saturating, ops::ControlFlow + any::Any, + cell::{Cell, RefCell}, + clone::Clone, + cmp::Reverse, + collections::hash_map::Entry, + fmt::{self, Debug}, + hash::{Hash, Hasher}, + num::Saturating, + ops::ControlFlow, }; use educe::Educe; --- a/crates/jrsonnet-ir-parser/src/lib.rs +++ b/crates/jrsonnet-ir-parser/src/lib.rs @@ -3,9 +3,9 @@ use jrsonnet_gcmodule::Acyclic; use jrsonnet_ir::{ unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec, - Destruct, DestructRest, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, - IfElse, IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, - Slice, SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility, + Destruct, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, + IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice, + SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility, }; use jrsonnet_lexer::{collect_lexed_str_block, Lexeme, Lexer, SyntaxKind, T}; @@ -30,7 +30,7 @@ } } -type R = Result; +type Result = std::result::Result; struct Parser<'a> { lexemes: Vec>, @@ -103,7 +103,7 @@ } } - fn eat(&mut self, t: SyntaxKind) -> R<()> { + fn eat(&mut self, t: SyntaxKind) -> Result<()> { if !self.at(t) { return Err(self.error(format!( "expected {}, got {}", @@ -138,15 +138,13 @@ } } - fn expect_ident(&mut self) -> R { + fn expect_ident(&mut self) -> Result { if !self.at(SyntaxKind::IDENT) { return Err(self.error(format!("expected identifier, got {}", self.current_desc()))); } let text = self.text(); if is_reserved(text) { - return Err(self.error(format!( - "expected identifier, got reserved word '{text}'" - ))); + return Err(self.error(format!("expected identifier, got reserved word '{text}'"))); } let s: IStr = text.into(); self.eat_any(); @@ -164,36 +162,38 @@ "assert" | "else" | "error" | "false" | "for" - | "function" | "if" - | "import" | "importstr" - | "importbin" | "in" - | "local" | "null" - | "tailstrict" | "then" - | "self" | "super" - | "true" + | "function" + | "if" | "import" + | "importstr" + | "importbin" + | "in" | "local" + | "null" | "tailstrict" + | "then" | "self" + | "super" | "true" ) } -fn spanned(p: &mut Parser<'_>, cb: impl FnOnce(&mut Parser<'_>) -> R) -> R> { +fn spanned( + p: &mut Parser<'_>, + cb: impl FnOnce(&mut Parser<'_>) -> Result, +) -> Result> { let start = p.span_start(); let v = cb(p)?; let end = p.span_end(); Ok(Spanned::new(v, Span(p.source.clone(), start, end))) } -fn parse_string_content(p: &mut Parser<'_>) -> R { +fn parse_string_content(p: &mut Parser<'_>) -> Result { let kind = p.peek(); let text = p.text(); let s = match kind { SyntaxKind::STRING_DOUBLE => { let inner = &text[1..text.len() - 1]; - unescape::unescape(inner) - .ok_or_else(|| p.error("invalid string escape".into()))? + unescape::unescape(inner).ok_or_else(|| p.error("invalid string escape".into()))? } SyntaxKind::STRING_SINGLE => { let inner = &text[1..text.len() - 1]; - unescape::unescape(inner) - .ok_or_else(|| p.error("invalid string escape".into()))? + unescape::unescape(inner).ok_or_else(|| p.error("invalid string escape".into()))? } SyntaxKind::STRING_DOUBLE_VERBATIM => { let inner = &text[2..text.len() - 1]; @@ -236,7 +236,7 @@ ) } -fn parse_number(p: &mut Parser<'_>) -> R { +fn parse_number(p: &mut Parser<'_>) -> Result { let text = p.text(); let n: f64 = text .replace('_', "") @@ -263,7 +263,7 @@ Some(t) } -fn assert_stmt(p: &mut Parser<'_>) -> R { +fn assert_stmt(p: &mut Parser<'_>) -> Result { p.eat(T![assert])?; let cond = spanned(p, expr)?; let msg = if p.try_eat(T![:]) { @@ -274,13 +274,13 @@ Ok(AssertStmt(cond, msg)) } -fn if_spec_data(p: &mut Parser<'_>) -> R { +fn if_spec_data(p: &mut Parser<'_>) -> Result { let v = spanned(p, |p| p.eat(T![if]))?; let cond = expr(p)?; Ok(IfSpecData { span: v.span, cond }) } -fn if_else(p: &mut Parser<'_>) -> R { +fn if_else(p: &mut Parser<'_>) -> Result { let cond = if_spec_data(p)?; p.eat(T![then])?; let cond_then = expr(p)?; @@ -296,7 +296,7 @@ }) } -fn slice_desc(p: &mut Parser<'_>, start: Option>) -> R { +fn slice_desc(p: &mut Parser<'_>, start: Option>) -> Result { p.eat(T![:])?; let end = if !p.at(T![:]) && !p.at(T![']']) { Some(spanned(p, expr)?) @@ -315,15 +315,12 @@ Ok(SliceDesc { start, end, step }) } -fn destruct(p: &mut Parser<'_>) -> R { +fn destruct(p: &mut Parser<'_>) -> Result { if p.at_ident() { return Ok(Destruct::Full(p.expect_ident()?)); } #[cfg(not(feature = "exp-destruct"))] - return Err(p.error(format!( - "expected identifier, got {}", - p.current_desc() - ))); + return Err(p.error(format!("expected identifier, got {}", p.current_desc()))); #[cfg(feature = "exp-destruct")] { if p.try_eat(T![?]) { @@ -343,17 +340,17 @@ } #[cfg(feature = "exp-destruct")] -fn destruct_rest(p: &mut Parser<'_>) -> R { +fn destruct_rest(p: &mut Parser<'_>) -> Result { p.eat(T![...])?; if p.at_ident() { - Ok(DestructRest::Keep(p.expect_ident()?)) + Ok(jrsonnet_ir::DestructRest::Keep(p.expect_ident()?)) } else { - Ok(DestructRest::Drop) + Ok(jrsonnet_ir::DestructRest::Drop) } } #[cfg(feature = "exp-destruct")] -fn destruct_array(p: &mut Parser<'_>) -> R { +fn destruct_array(p: &mut Parser<'_>) -> Result { p.eat(T!['['])?; let mut start = Vec::new(); let mut rest = None; @@ -391,7 +388,7 @@ } #[cfg(feature = "exp-destruct")] -fn destruct_object(p: &mut Parser<'_>) -> R { +fn destruct_object(p: &mut Parser<'_>) -> Result { p.eat(T!['{'])?; let mut fields = Vec::new(); let mut rest = None; @@ -426,7 +423,7 @@ Ok(Destruct::Object { fields, rest }) } -fn params(p: &mut Parser<'_>) -> R { +fn params(p: &mut Parser<'_>) -> Result { if p.at(T![')']) { return Ok(ExprParams::new(Vec::new())); } @@ -452,7 +449,7 @@ Ok(ExprParams::new(result)) } -fn args(p: &mut Parser<'_>) -> R { +fn args(p: &mut Parser<'_>) -> Result { if p.at(T![')']) { return Ok(ArgsDesc::new(Vec::new(), Vec::new())); } @@ -489,7 +486,7 @@ Ok(ArgsDesc::new(unnamed, named)) } -fn bind(p: &mut Parser<'_>) -> R { +fn bind(p: &mut Parser<'_>) -> Result { #[cfg(feature = "exp-destruct")] { if !p.at_ident() { @@ -520,7 +517,7 @@ } } -fn visibility(p: &mut Parser<'_>) -> R { +fn visibility(p: &mut Parser<'_>) -> Result { p.eat(T![:])?; if p.try_eat(T![:]) { if p.try_eat(T![:]) { @@ -533,7 +530,7 @@ } } -fn field_name(p: &mut Parser<'_>) -> R { +fn field_name(p: &mut Parser<'_>) -> Result { if p.at_ident() { Ok(FieldName::Fixed(p.expect_ident()?)) } else if is_string_token(p.peek()) { @@ -548,7 +545,7 @@ } } -fn field(p: &mut Parser<'_>) -> R { +fn field(p: &mut Parser<'_>) -> Result { let name = spanned(p, field_name)?; if p.at(T!['(']) { @@ -578,7 +575,7 @@ } } -fn member(p: &mut Parser<'_>) -> R { +fn member(p: &mut Parser<'_>) -> Result { if p.at(T![local]) { p.eat(T![local])?; Ok(Member::BindStmt(bind(p)?)) @@ -589,7 +586,7 @@ } } -fn for_spec(p: &mut Parser<'_>) -> R { +fn for_spec(p: &mut Parser<'_>) -> Result { p.eat(T![for])?; let d = destruct(p)?; p.eat(T![in])?; @@ -597,7 +594,7 @@ Ok(ForSpecData { destruct: d, over }) } -fn compspecs(p: &mut Parser<'_>) -> R> { +fn compspecs(p: &mut Parser<'_>) -> Result> { let mut specs = Vec::new(); specs.push(CompSpec::ForSpec(for_spec(p)?)); loop { @@ -613,7 +610,7 @@ Ok(specs) } -fn objinside(p: &mut Parser<'_>) -> R { +fn objinside(p: &mut Parser<'_>) -> Result { if p.at(T!['}']) { return Ok(ObjBody::MemberList(ObjMembers { locals: Rc::new(Vec::new()), @@ -641,26 +638,22 @@ match m { Member::Field(f) => { if field_member.is_some() { - return Err(p.error( - "object comprehension can only contain one field".into(), - )); + return Err( + p.error("object comprehension can only contain one field".into()) + ); } field_member = Some(f); } Member::BindStmt(b) => locals.push(b), Member::AssertStmt(_) => { - return Err(p.error( - "asserts are unsupported in object comprehension".into(), - )); + return Err(p.error("asserts are unsupported in object comprehension".into())); } } } Ok(ObjBody::ObjComp(ObjComp { locals: Rc::new(locals), field: Rc::new( - field_member.ok_or_else(|| { - p.error("missing object comprehension field".into()) - })?, + field_member.ok_or_else(|| p.error("missing object comprehension field".into()))?, ), compspecs: specs, })) @@ -683,7 +676,7 @@ } } -fn expr_basic(p: &mut Parser<'_>) -> R { +fn expr_basic(p: &mut Parser<'_>) -> Result { if let Some(lit) = literal(p) { return Ok(Expr::Literal(lit)); } @@ -825,7 +818,6 @@ } } -/// Flush accumulated index parts into an Expr::Index wrapping `e`. fn flush_index_parts(e: &mut Expr, parts: &mut Vec) { if parts.is_empty() { return; @@ -837,7 +829,7 @@ }; } -fn expr_suffix(p: &mut Parser<'_>) -> R { +fn expr_suffix(p: &mut Parser<'_>) -> Result { let mut e = expr_basic(p)?; // Accumulate consecutive index parts (.field, [expr], ?.field, ?.[expr]) // into a single Expr::Index. This is critical for null-coalesce semantics: @@ -1006,7 +998,7 @@ } } -fn expr_bp(p: &mut Parser<'_>, min_bp: u8) -> R { +fn expr_bp(p: &mut Parser<'_>, min_bp: u8) -> Result { let mut lhs = if let Some(op) = unary_op(p.peek()) { p.eat_any(); let rbp = prefix_binding_power(op); @@ -1038,11 +1030,11 @@ Ok(lhs) } -fn expr(p: &mut Parser<'_>) -> R { +fn expr(p: &mut Parser<'_>) -> Result { expr_bp(p, 0) } -pub fn parse(str: &str, settings: &ParserSettings) -> Result { +pub fn parse(str: &str, settings: &ParserSettings) -> Result { let mut p = Parser::new(str, settings.source.clone()); for lexeme in &p.lexemes { if let Some(desc) = lexeme.kind.error_description() { @@ -1056,20 +1048,14 @@ } let e = expr(&mut p)?; if !p.at_eof() { - return Err(p.error(format!( - "expected end of file, got {}", - p.current_desc(), - ))); + return Err(p.error(format!("expected end of file, got {}", p.current_desc(),))); } Ok(e) } pub fn string_to_expr(s: IStr, settings: &ParserSettings) -> Spanned { let len = s.len(); - Spanned::new( - Expr::Str(s), - Span(settings.source.clone(), 0, len as u32), - ) + Spanned::new(Expr::Str(s), Span(settings.source.clone(), 0, len as u32)) } #[cfg(test)] --- a/crates/jrsonnet-ir/src/visit.rs +++ b/crates/jrsonnet-ir/src/visit.rs @@ -21,6 +21,7 @@ } } +#[allow(unused_variables, reason = "used with exp-destruct")] pub fn visit_destruct(v: &mut V, destruct: &Destruct) { match destruct { Destruct::Full(_istr) => {} --- a/crates/jrsonnet-rowan-parser/src/lex.rs +++ b/crates/jrsonnet-rowan-parser/src/lex.rs @@ -11,9 +11,11 @@ } pub fn lex(input: &str) -> Vec> { - Lexer::new(input).map(|l| Lexeme { - kind: SyntaxKind::from_raw(l.kind.into_raw()), - text: l.text, - range: TextRange::new(TextSize::from(l.range.0), TextSize::from(l.range.1)), - }).collect() + Lexer::new(input) + .map(|l| Lexeme { + kind: SyntaxKind::from_raw(l.kind.into_raw()), + text: l.text, + range: TextRange::new(TextSize::from(l.range.0), TextSize::from(l.range.1)), + }) + .collect() } --- a/crates/jrsonnet-stdlib/src/manifest/ini.rs +++ b/crates/jrsonnet-stdlib/src/manifest/ini.rs @@ -3,8 +3,7 @@ use jrsonnet_evaluator::{ manifest::{ManifestFormat, ToStringFormat}, typed::{FromUntyped, Typed}, - ObjValue, Result, ResultExt, Val, - IStr, + IStr, ObjValue, Result, ResultExt, Val, }; pub struct IniFormat { --- a/xtask/src/sourcegen/kinds.rs +++ b/xtask/src/sourcegen/kinds.rs @@ -120,11 +120,15 @@ Self::Literal { name, .. } => match name.as_str() { "FLOAT" => "number".to_owned(), "IDENT" => "identifier".to_owned(), - "STRING_DOUBLE" | "STRING_SINGLE" | "STRING_DOUBLE_VERBATIM" - | "STRING_SINGLE_VERBATIM" | "STRING_BLOCK" => "string".to_owned(), + "STRING_DOUBLE" + | "STRING_SINGLE" + | "STRING_DOUBLE_VERBATIM" + | "STRING_SINGLE_VERBATIM" + | "STRING_BLOCK" => "string".to_owned(), "WHITESPACE" => "whitespace".to_owned(), - "SINGLE_LINE_SLASH_COMMENT" | "SINGLE_LINE_HASH_COMMENT" - | "MULTI_LINE_COMMENT" => "comment".to_owned(), + "SINGLE_LINE_SLASH_COMMENT" | "SINGLE_LINE_HASH_COMMENT" | "MULTI_LINE_COMMENT" => { + "comment".to_owned() + } _ => name.to_lowercase(), }, Self::Meta { name, .. } => name.to_lowercase(), -- gitstuff