Skip to content

Commit

Permalink
feat(CodeBlock): Added 'rust' to supported languages
Browse files Browse the repository at this point in the history
  • Loading branch information
HHogg committed Mar 2, 2024
1 parent 01ebb4e commit 557b3e6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion workspaces/package/src/Code/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { forwardRef, memo } from 'react';
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import css from 'react-syntax-highlighter/dist/esm/languages/prism/css';
import json from 'react-syntax-highlighter/dist/esm/languages/prism/json';
import rust from 'react-syntax-highlighter/dist/esm/languages/prism/rust';
import tsx from 'react-syntax-highlighter/dist/esm/languages/prism/tsx';
import typescript from 'react-syntax-highlighter/dist/esm/languages/prism/typescript';
import style from 'react-syntax-highlighter/dist/esm/styles/prism/synthwave84';
Expand All @@ -10,10 +11,16 @@ import './CodeBlock.css';

SyntaxHighlighter.registerLanguage('css', css);
SyntaxHighlighter.registerLanguage('json', json);
SyntaxHighlighter.registerLanguage('rust', rust);
SyntaxHighlighter.registerLanguage('tsx', tsx);
SyntaxHighlighter.registerLanguage('typescript', typescript);

export type TypeCodeBlockLanguage = 'css' | 'json' | 'tsx' | 'typescript';
export type TypeCodeBlockLanguage =
| 'css'
| 'json'
| 'rust'
| 'tsx'
| 'typescript';

/**
* Provides some syntax highlighting, courtesy of PrismJS.
Expand Down
2 changes: 2 additions & 0 deletions workspaces/site/src/docs/catalog/snippets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import { TypeCodeBlockLanguage } from 'preshape';
import SnippetCss from './css.txt?raw';
import SnippetJson from './json.txt?raw';
import SnippetRust from './rust.txt?raw';
import SnippetTsx from './tsx.txt?raw';
import SnippetTypescript from './typescript.txt?raw';

export const snippets: Record<TypeCodeBlockLanguage, string> = {
css: SnippetCss,
json: SnippetJson,
rust: SnippetRust,
tsx: SnippetTsx,
typescript: SnippetTypescript,
};
42 changes: 42 additions & 0 deletions workspaces/site/src/docs/catalog/snippets/rust.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub type Sequence = [u8; 12];

/// Returns the length of a sequence, given by 0 as an empty indicator
pub fn length(sequence: &Sequence) -> usize {
sequence.iter().position(|&x| x == 0).unwrap_or(12)
}

/// Rotates a sequence from the start index in the given direction.
fn rotate(sequence: &Sequence, start_index: usize, forwards: bool) -> Sequence {
let mut reordered = Sequence::default();
let sequence_length = length(sequence);
let mut index: usize = start_index;
let mut count: usize = 0;

while count < sequence_length {
reordered[count] = sequence[index];

if forwards {
index = (index + 1) % sequence_length;
} else {
index = (index + sequence_length - 1) % sequence_length;
}

count += 1;
}

reordered
}

/// A symmetrical sequence is one that starting
/// at any index and going in one direction.
/// Whatever path has been taken, can be taken
/// in the opposite direction.
pub fn is_symmetrical(sequence: &Sequence) -> bool {
for i in 0..length(sequence) {
if &rotate(sequence, i, false) == sequence {
return true;
}
}

false
}

0 comments on commit 557b3e6

Please sign in to comment.