@@ -8,9 +8,10 @@ import {
8
8
Partner ,
9
9
Recommended ,
10
10
Retired ,
11
- Type ,
11
+ Type as TypeLabel ,
12
12
Updatable ,
13
13
} from '../nls' ;
14
+ import URI from '@theia/core/lib/common/uri' ;
14
15
15
16
export const LibraryServicePath = '/services/library-service' ;
16
17
export const LibraryService = Symbol ( 'LibraryService' ) ;
@@ -55,6 +56,7 @@ export interface LibrarySearch extends Searchable.Options {
55
56
readonly topic ?: LibrarySearch . Topic ;
56
57
}
57
58
export namespace LibrarySearch {
59
+ export const Default : LibrarySearch = { type : 'All' , topic : 'All' } ;
58
60
export const TypeLiterals = [
59
61
'All' ,
60
62
'Updatable' ,
@@ -66,6 +68,11 @@ export namespace LibrarySearch {
66
68
'Retired' ,
67
69
] as const ;
68
70
export type Type = typeof TypeLiterals [ number ] ;
71
+ export namespace Type {
72
+ export function is ( arg : unknown ) : arg is Type {
73
+ return typeof arg === 'string' && TypeLiterals . includes ( arg as Type ) ;
74
+ }
75
+ }
69
76
export const TypeLabels : Record < Type , string > = {
70
77
All : All ,
71
78
Updatable : Updatable ,
@@ -90,6 +97,11 @@ export namespace LibrarySearch {
90
97
'Uncategorized' ,
91
98
] as const ;
92
99
export type Topic = typeof TopicLiterals [ number ] ;
100
+ export namespace Topic {
101
+ export function is ( arg : unknown ) : arg is Topic {
102
+ return typeof arg === 'string' && TopicLiterals . includes ( arg as Topic ) ;
103
+ }
104
+ }
93
105
export const TopicLabels : Record < Topic , string > = {
94
106
All : All ,
95
107
Communication : nls . localize (
@@ -126,8 +138,60 @@ export namespace LibrarySearch {
126
138
string
127
139
> = {
128
140
topic : nls . localize ( 'arduino/librarySearchProperty/topic' , 'Topic' ) ,
129
- type : Type ,
141
+ type : TypeLabel ,
130
142
} ;
143
+ export namespace UriParser {
144
+ export const authority = 'librarymanager' ;
145
+ export function parse ( uri : URI ) : LibrarySearch | undefined {
146
+ if ( uri . scheme !== 'http' ) {
147
+ throw new Error (
148
+ `Invalid 'scheme'. Expected 'http'. URI was: ${ uri . toString ( ) } .`
149
+ ) ;
150
+ }
151
+ if ( uri . authority !== authority ) {
152
+ throw new Error (
153
+ `Invalid 'authority'. Expected: '${ authority } '. URI was: ${ uri . toString ( ) } .`
154
+ ) ;
155
+ }
156
+ const segments = Searchable . UriParser . normalizedSegmentsOf ( uri ) ;
157
+ // Special magic handling for `Signal Input/Output`.
158
+ // TODO: IDE2 deserves a better lib/boards URL spec.
159
+ // https://github.com/arduino/arduino-ide/issues/1442#issuecomment-1252136377
160
+ if ( segments . length === 3 ) {
161
+ const [ type , topicHead , topicTail ] = segments ;
162
+ const maybeTopic = `${ topicHead } /${ topicTail } ` ;
163
+ if (
164
+ LibrarySearch . Topic . is ( maybeTopic ) &&
165
+ maybeTopic === 'Signal Input/Output' &&
166
+ LibrarySearch . Type . is ( type )
167
+ ) {
168
+ return {
169
+ type,
170
+ topic : maybeTopic ,
171
+ ...Searchable . UriParser . parseQuery ( uri ) ,
172
+ } ;
173
+ }
174
+ }
175
+ let searchOptions : LibrarySearch | undefined = undefined ;
176
+ const [ type , topic ] = segments ;
177
+ if ( ! type && ! topic ) {
178
+ searchOptions = LibrarySearch . Default ;
179
+ } else if ( LibrarySearch . Type . is ( type ) ) {
180
+ if ( ! topic ) {
181
+ searchOptions = { ...LibrarySearch . Default , type } ;
182
+ } else if ( LibrarySearch . Topic . is ( topic ) ) {
183
+ searchOptions = { type, topic } ;
184
+ }
185
+ }
186
+ if ( searchOptions ) {
187
+ return {
188
+ ...searchOptions ,
189
+ ...Searchable . UriParser . parseQuery ( uri ) ,
190
+ } ;
191
+ }
192
+ return undefined ;
193
+ }
194
+ }
131
195
}
132
196
133
197
export namespace LibraryService {
0 commit comments