From patchwork Tue Mar 18 14:26:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hilton Chain X-Patchwork-Id: 40335 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 391AB27BBE9; Tue, 18 Mar 2025 14:30:38 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,FROM_SUSPICIOUS_NTLD,MAILING_LIST_MULTI, PDS_OTHER_BAD_TLD,RCVD_IN_DNSWL_BLOCKED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 9F78427BBE2 for ; Tue, 18 Mar 2025 14:30:37 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tuXxS-0006OL-Ji; Tue, 18 Mar 2025 10:30:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tuXxQ-0006GP-BF for guix-patches@gnu.org; Tue, 18 Mar 2025 10:30:16 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tuXxP-0001vl-O5; Tue, 18 Mar 2025 10:30:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=pEk75I1A9NmXZ9D/zh4q9scyVRAY149icZ/Q4bBikA8=; b=FeZAVV1Ks3eHRJMhzk1bhMzsYRk7Yh3YKo1TaO+4xN+K6p95MQqyNoLN/p0uRJzvSGlOJDVqB/yUPWKlRhPmUTUW1DWv2bZnXVOjeRxCUEzMAPi7i2QsJSShRJr/zctu5BU/DJ9RyAPAbabccAify+xlddD+5nZPZyPX+0Xe7lQVxR3unGeKpY/pTC6wdLHg3xfk1RTcm+ANbxAgUrNbf4/XZqTbQmuRGrYkjsRk8mew5ju94X8Z2meEthNwWX/B1YsxFeDtiPzqKPDmQBC1vHZRKWvMZyo2pKF/tZmpotR/mDahxyGiKd+w7heady6+TG0JDvsJ8anPRm2bgMYtTw==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tuXxP-0004v4-9O; Tue, 18 Mar 2025 10:30:15 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#77093] [PATCH rust-team v2 10/17] scripts: import: Support expressions defined by 'define. Resent-From: Hilton Chain Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Tue, 18 Mar 2025 14:30:14 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 77093 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 77093@debbugs.gnu.org Cc: Hilton Chain , Efraim Flashner , Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 77093-submit@debbugs.gnu.org id=B77093.174230817118356 (code B ref 77093); Tue, 18 Mar 2025 14:30:14 +0000 Received: (at 77093) by debbugs.gnu.org; 18 Mar 2025 14:29:31 +0000 Received: from localhost ([127.0.0.1]:41742 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tuXwf-0004lj-Gg for submit@debbugs.gnu.org; Tue, 18 Mar 2025 10:29:30 -0400 Received: from mx.boiledscript.com ([2a01:4f8:10b:392::42]:38154) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tuXv5-0004Uv-Su for 77093@debbugs.gnu.org; Tue, 18 Mar 2025 10:27:54 -0400 From: Hilton Chain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarare.space; s=mail; t=1742308070; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q2tsWLuz/lnLteMu2aRrCXUHhNIAVmLTRUC1j3a1vQk=; b=W64XFJwpCs840RtbiO0R18/NZ3Oi3b92HwmCqWWnvh5z+YoNa5CqSzNNDgY6thTpumjSbm E+BCTIxTa0yHLdXEpPSlj5ed//ArpCLbPX6ZhdY1RPWvMbV3Y78yMyirN3+R44/7tjbIV8 kd/PMGh4ZMuauNv2JOchwZQVj+/9gY5FI/Wd5CIQAIBwkVoe/ldccXmzMsqZhF7LInsI4T pwbB/2fluQA5AmsjK3wBRlfIa0z5kUTttFMDICuuXDOdFI8XEl04rjS9thtlGJ0DOg/s68 kcZhuifcywfrDBMHRJZz5UPNljXfJNsZuJqmxYhwZYsmqaOK0kG9vyZMqGBrng== Date: Tue, 18 Mar 2025 22:26:53 +0800 Message-ID: <592685c77f24f1052112c00965c9fa1deeedaf33.1742306960.git.hako@ultrarare.space> In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/utils.scm (find-definition-location): New procedure. (find-definition-insertion-location): Define with it. * guix/scripts/import.scm (import-as-definitions, guix-import): Support expressions defined by 'define. Change-Id: I03118e1a3372028b4f0530964aba871b4a1a4d25 --- guix/scripts/import.scm | 19 ++++++++++++++----- guix/utils.scm | 27 +++++++++++++++++++-------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/guix/scripts/import.scm b/guix/scripts/import.scm index 58a84d0db7..e53028e747 100644 --- a/guix/scripts/import.scm +++ b/guix/scripts/import.scm @@ -30,6 +30,7 @@ (define-module (guix scripts import) #:use-module (guix read-print) #:use-module (guix utils) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) #:use-module (ice-9 format) #:use-module (ice-9 match) #:export (%standard-import-options @@ -83,7 +84,8 @@ (define (import-as-definitions importer args proc) ((and expr (or ('package _ ...) ('let _ ...))) (proc (package->definition expr))) - ((and expr ('define-public _ ...)) + ((and expr (or ('define-public _ ...) + ('define _ ...))) (proc expr)) ((expressions ...) (for-each (lambda (expr) @@ -91,7 +93,8 @@ (define (import-as-definitions importer args proc) ((and expr (or ('package _ ...) ('let _ ...))) (proc (package->definition expr))) - ((and expr ('define-public _ ...)) + ((and expr (or ('define-public _ ...) + ('define _ ...))) (proc expr)))) expressions)) (x @@ -117,13 +120,19 @@ (define-command (guix-import . args) (show-version-and-exit "guix import")) ((or ("-i" file importer args ...) ("--insert" file importer args ...)) - (let ((find-and-insert + (let* ((importer-definers + `(,@(if (member importer '("crate")) + '(define) + '()) + define-public)) + (definer? (cut member <> importer-definers)) + (find-and-insert (lambda (expr) (match expr - (('define-public term _ ...) + (((? definer? definer) term _ ...) (let ((source-properties (find-definition-insertion-location - file term))) + file term #:definer definer))) (if source-properties (insert-expression source-properties expr) (let ((port (open-file file "a"))) diff --git a/guix/utils.scm b/guix/utils.scm index c7c23d9d5b..77ec6d992a 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -154,6 +154,7 @@ (define-module (guix utils) edit-expression delete-expression insert-expression + find-definition-location find-definition-insertion-location filtered-port @@ -520,24 +521,34 @@ (define (insert-expression source-properties expr) (string-append expr "\n\n" str)))) (edit-expression source-properties insert))) -(define (find-definition-insertion-location file term) - "Search in FILE for a top-level public definition whose defined term -alphabetically succeeds TERM. Return the location if found, or #f -otherwise." - (let ((search-term (symbol->string term))) +(define* (find-definition-location file term + #:key (definer 'define-public) + (pred string=)) + "Search in FILE for a top-level definition defined by DEFINER with defined +term comparing to TERM through PRED. Return the location if PRED returns #t, +or #f otherwise." + (let ((search-term (symbol->string term)) + (definer? (cut eq? definer <>))) (call-with-input-file file (lambda (port) (do ((syntax (read-syntax port) (read-syntax port))) ((match (syntax->datum syntax) - (('define-public current-term _ ...) - (string> (symbol->string current-term) - search-term)) + (((? definer?) current-term _ ...) + (pred (symbol->string current-term) + search-term)) ((? eof-object?) #t) (_ #f)) (and (not (eof-object? syntax)) (syntax-source syntax)))))))) +(define* (find-definition-insertion-location file term + #:key (definer 'define-public)) + "Search in FILE for a top-level definition defined by DEFINER with defined +term alphabetically succeeds TERM. Return the location if found, or #f +otherwise." + (find-definition-location file term #:definer definer #:pred string>)) + ;;; ;;; Keyword arguments.