|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package lib |
| 17 | + |
| 18 | +import ( |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" |
| 22 | + "github.com/arduino/arduino-cli/arduino/utils" |
| 23 | +) |
| 24 | + |
| 25 | +// matcherTokensFromQueryString parses the query string into tokens of interest |
| 26 | +// for the qualifier-value pattern matching. |
| 27 | +func matcherTokensFromQueryString(query string) []string { |
| 28 | + escaped := false |
| 29 | + quoted := false |
| 30 | + tokens := []string{} |
| 31 | + sb := &strings.Builder{} |
| 32 | + |
| 33 | + for _, r := range query { |
| 34 | + // Short circuit the loop on backslash so that all other paths can clear |
| 35 | + // the escaped flag. |
| 36 | + if !escaped && r == '\\' { |
| 37 | + escaped = true |
| 38 | + continue |
| 39 | + } |
| 40 | + |
| 41 | + if r == '"' { |
| 42 | + if !escaped { |
| 43 | + quoted = !quoted |
| 44 | + } else { |
| 45 | + sb.WriteRune(r) |
| 46 | + } |
| 47 | + } else if !quoted && r == ' ' { |
| 48 | + tokens = append(tokens, strings.ToLower(sb.String())) |
| 49 | + sb.Reset() |
| 50 | + } else { |
| 51 | + sb.WriteRune(r) |
| 52 | + } |
| 53 | + escaped = false |
| 54 | + } |
| 55 | + if sb.Len() > 0 { |
| 56 | + tokens = append(tokens, strings.ToLower(sb.String())) |
| 57 | + } |
| 58 | + |
| 59 | + return tokens |
| 60 | +} |
| 61 | + |
| 62 | +// defaulLibraryMatchExtractor returns a string describing the library that |
| 63 | +// is used for the simple search. |
| 64 | +func defaultLibraryMatchExtractor(lib *librariesindex.Library) string { |
| 65 | + res := lib.Name + " " + |
| 66 | + lib.Latest.Paragraph + " " + |
| 67 | + lib.Latest.Sentence + " " + |
| 68 | + lib.Latest.Author + " " |
| 69 | + for _, include := range lib.Latest.ProvidesIncludes { |
| 70 | + res += include + " " |
| 71 | + } |
| 72 | + return res |
| 73 | +} |
| 74 | + |
| 75 | +var qualifiers map[string]func(lib *librariesindex.Library) string = map[string]func(lib *librariesindex.Library) string{ |
| 76 | + "name": func(lib *librariesindex.Library) string { return lib.Name }, |
| 77 | + "architectures": func(lib *librariesindex.Library) string { return strings.Join(lib.Latest.Architectures, " ") }, |
| 78 | + "author": func(lib *librariesindex.Library) string { return lib.Latest.Author }, |
| 79 | + "category": func(lib *librariesindex.Library) string { return lib.Latest.Category }, |
| 80 | + "dependencies": func(lib *librariesindex.Library) string { |
| 81 | + names := make([]string, len(lib.Latest.Dependencies)) |
| 82 | + for i, dep := range lib.Latest.Dependencies { |
| 83 | + names[i] = dep.GetName() |
| 84 | + } |
| 85 | + return strings.Join(names, " ") |
| 86 | + }, |
| 87 | + "license": func(lib *librariesindex.Library) string { return lib.Latest.License }, |
| 88 | + "maintainer": func(lib *librariesindex.Library) string { return lib.Latest.Maintainer }, |
| 89 | + "paragraph": func(lib *librariesindex.Library) string { return lib.Latest.Paragraph }, |
| 90 | + "provides": func(lib *librariesindex.Library) string { return strings.Join(lib.Latest.ProvidesIncludes, " ") }, |
| 91 | + "sentence": func(lib *librariesindex.Library) string { return lib.Latest.Sentence }, |
| 92 | + "types": func(lib *librariesindex.Library) string { return strings.Join(lib.Latest.Types, " ") }, |
| 93 | + "version": func(lib *librariesindex.Library) string { return lib.Latest.Version.String() }, |
| 94 | + "website": func(lib *librariesindex.Library) string { return lib.Latest.Website }, |
| 95 | +} |
| 96 | + |
| 97 | +// MatcherFromQueryString returns a closure that takes a library as a |
| 98 | +// parameter and returns true if the library matches the query. |
| 99 | +func MatcherFromQueryString(query string) func(*librariesindex.Library) bool { |
| 100 | + // A qv-query is one using <qualifier>[:=]<value> syntax. |
| 101 | + qvQuery := strings.Contains(query, ":") || strings.Contains(query, "=") |
| 102 | + |
| 103 | + if !qvQuery { |
| 104 | + queryTerms := utils.SearchTermsFromQueryString(query) |
| 105 | + return func(lib *librariesindex.Library) bool { |
| 106 | + return utils.Match(defaultLibraryMatchExtractor(lib), queryTerms) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + queryTerms := matcherTokensFromQueryString(query) |
| 111 | + |
| 112 | + return func(lib *librariesindex.Library) bool { |
| 113 | + matched := true |
| 114 | + for _, term := range queryTerms { |
| 115 | + if sepIdx := strings.IndexAny(term, ":="); sepIdx != -1 { |
| 116 | + qualifier, separator, target := term[:sepIdx], term[sepIdx], term[sepIdx+1:] |
| 117 | + if extractor, ok := qualifiers[qualifier]; ok { |
| 118 | + switch separator { |
| 119 | + case ':': |
| 120 | + matched = (matched && utils.Match(extractor(lib), []string{target})) |
| 121 | + continue |
| 122 | + case '=': |
| 123 | + matched = (matched && strings.ToLower(extractor(lib)) == target) |
| 124 | + continue |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + // We perform the usual match in the following cases: |
| 129 | + // 1. Unknown qualifier names revert to basic search terms. |
| 130 | + // 2. Terms that do not use qv-syntax. |
| 131 | + matched = (matched && utils.Match(defaultLibraryMatchExtractor(lib), []string{term})) |
| 132 | + } |
| 133 | + return matched |
| 134 | + } |
| 135 | +} |
0 commit comments