Module: Cecil::Lang::Rust::Helpers
- Defined in:
- lib/cecil/lang/rust.rb
Overview
rubocop:disable Style/Documentation
Constant Summary collapse
- CHAR_TO_CUSTOM_ESCAPE_LITERAL =
{ "\n" => '\n', "\r" => '\r', "\t" => '\t', "\0" => '\0', "\\" => "\\\\", # \ => \\ '"' => '\\"' # " => \" }.freeze
Instance Method Summary collapse
-
#l(items) ⇒ String
Short for "list"; Accepts one or a list of strings and returns them joined with
", "
. -
#rchar(char) ⇒ Object
Escapes a character (string with one character) for use in a Rust string literal.
-
#rs(val) ⇒ Object
short for "rust value"; returns a Rust literal version of the input.
-
#s(val) ⇒ String
Short for "string content"; returns escaped version of the string that can be inserted into a string literal.
-
#unicode_escape(str) ⇒ Object
Escapes string as unicode character literals.
-
#unicode_escape_codepoint(char_int) ⇒ Object
Escapes codepoint as unicode character literal See https://doc.rust-lang.org/reference/tokens.html#unicode-escapes.
Instance Method Details
#l(items) ⇒ String
Short for "list"; Accepts one or a list of strings and returns them joined with ", "
17 |
# File 'lib/cecil/lang/rust.rb', line 17 def l(items) = Array(items).compact.join(", ") |
#rchar(char) ⇒ Object
Escapes a character (string with one character) for use in a Rust string literal.
42 43 44 45 46 47 48 |
# File 'lib/cecil/lang/rust.rb', line 42 def rchar(char) CHAR_TO_CUSTOM_ESCAPE_LITERAL[char] || case char when /^[ -~]$/ then char else unicode_escape(char) end end |
#rs(val) ⇒ Object
short for "rust value"; returns a Rust literal version of the input.
Currently handles strings, integers, floats, and booleans.
82 83 84 85 86 87 |
# File 'lib/cecil/lang/rust.rb', line 82 def rs(val) case val in String then %("#{s val}") in Integer | Float | true | false then val.to_s end end |
#s(val) ⇒ String
Short for "string content"; returns escaped version of the string that can be inserted into a string literal.
Useful for inserting data into a string or for outputting a string but using quotes to make it clear to the reader what the intended output will be.
65 |
# File 'lib/cecil/lang/rust.rb', line 65 def s(val) = val.each_char.map { rchar(_1) }.join |
#unicode_escape(str) ⇒ Object
Escapes string as unicode character literals
24 |
# File 'lib/cecil/lang/rust.rb', line 24 def unicode_escape(str) = str.each_codepoint.map { unicode_escape_codepoint(_1) }.join |
#unicode_escape_codepoint(char_int) ⇒ Object
Escapes codepoint as unicode character literal See https://doc.rust-lang.org/reference/tokens.html#unicode-escapes
21 |
# File 'lib/cecil/lang/rust.rb', line 21 def unicode_escape_codepoint(char_int) = "\\u{#{char_int.to_s(16)}}" |