Skip to content

Commit 51983b0

Browse files
josephfrazierstefanbuck
authored andcommitted
Add Rust plugin with 'extern crate'/'use' support (OctoLinker#153)
This live-resolver PR must be merged first: OctoLinker/api#18 Resolves OctoLinker#45 Test page: https://github.com/rust-lang/cargo/blob/07c1d9900de40c59b898d08d64273447560ffbe3/tests/config.rs#L1-L5
1 parent 2e96d19 commit 51983b0

File tree

7 files changed

+54
-0
lines changed

7 files changed

+54
-0
lines changed

lib/pattern-preset.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const presets = {
5050
],
5151
githubClasses: ['type-viml'],
5252
},
53+
54+
'Rust': {
55+
pathSubstrings: ['.rs'],
56+
githubClasses: ['type-rust'],
57+
},
5358
};
5459

5560
export default function (presetName) {

lib/plugins/rust/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { RUST_CRATE } from '../../../packages/helper-grammar-regex-collection/index.js';
2+
import insertLink from '../../insert-link';
3+
import preset from '../../pattern-preset';
4+
5+
export default class Rust {
6+
7+
getPattern() {
8+
return preset('Rust');
9+
}
10+
11+
parseBlob(blob) {
12+
insertLink(blob.el, RUST_CRATE, {
13+
resolver: 'rustCrate',
14+
target: '$1',
15+
});
16+
}
17+
}

lib/resolver/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export { default as javascriptUniversal } from './javascript-universal.js';
88
export { default as rubyUniversal } from './ruby-universal.js';
99
export { default as dockerImage } from './docker-image.js';
1010
export { default as vimPlugin } from './vim-plugin.js';
11+
export { default as rustCrate } from './rust-crate.js';

lib/resolver/rust-crate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import liveResolverQuery from './live-resolver-query.js';
2+
3+
export default function ({ target }) {
4+
return liveResolverQuery({ type: 'crates', target });
5+
}

packages/helper-grammar-regex-collection/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const HOMEBREW = /(?:depends_on|conflicts_with)(?: cask:| formula:)? (['"][^'"\s
66
const TYPESCRIPT_REFERENCE = /\/{3}\s?<reference path=(['"][^'"\s]+['"])/g;
77
const DOCKER_FROM = /FROM\s([^\n]*)/g;
88
const VIM_PLUGIN = /(?:(?:(?:Neo)?Bundle(?:Lazy|Fetch)?)|Plug(?:in)?)\s(['"][^'"\s]+['"])/g;
9+
const RUST_CRATE = /(?:extern crate|use) ([^:; ]+)/g;
910

1011
export {
1112
REQUIRE,
@@ -16,4 +17,5 @@ export {
1617
TYPESCRIPT_REFERENCE,
1718
DOCKER_FROM,
1819
VIM_PLUGIN,
20+
RUST_CRATE,
1921
};

packages/helper-grammar-regex-collection/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ const fixtures = {
150150
"Plugin'ctrlp.vim'",
151151
],
152152
},
153+
RUST_CRATE: {
154+
valid: [
155+
['extern crate pcre;', ['pcre']],
156+
['extern crate std as ruststd;', ['std']],
157+
['use std::option::Option::{Some, None};', ['std']],
158+
],
159+
invalid: [
160+
"extern create 'pcre'",
161+
],
162+
},
153163
};
154164

155165
describe('helper-grammar-regex-collection', () => {

test/resolver/rust-crate.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import assert from 'assert';
2+
import rustCrate from '../../lib/resolver/rust-crate.js';
3+
4+
describe('rust-crate', () => {
5+
it('resolves hamcrest using the live-resolver', () => {
6+
assert.deepEqual(
7+
rustCrate({ target: 'hamcrest' }),
8+
{
9+
url: 'https://githublinker.herokuapp.com/q/crates/hamcrest',
10+
method: 'GET',
11+
}
12+
);
13+
});
14+
});

0 commit comments

Comments
 (0)