Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] remove active_support, setup rust #2

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Rust
Cargo.lock
/lib/case_transform/native.bundle
/target/

*.gem
*.rbc
/.config
Expand Down
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "case_transform"
version = "0.1.0"
authors = ["L. Preston Sego III <LPSego3+dev@gmail.com>"]

[lib]
crate-type = ["staticlib"]

[dependencies]
Inflector = "0.3.1"
libc = "*"
helix = { git = "https://github.com/rustbridge/helix" }

# [dependencies.libcruby-sys]
# path = "./crates/libcruby-sys"
#
# [dependencies.helix]
# path = "./"

[profile.release]
lto = true
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in authorizable.gemspec
gemspec

gem 'helix_runtime', github: 'rustbridge/helix', require: 'helix_runtime/ruby'
19 changes: 8 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
GIT
remote: git://github.com/rustbridge/helix.git
revision: 2c057f04bf130f2a25ef8ac006290c0f81abe350
specs:
helix_runtime (0.5.0)

PATH
remote: .
specs:
case_transform (0.1)
activesupport
helix_runtime

GEM
remote: https://rubygems.org/
specs:
activesupport (5.0.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
ast (2.3.0)
awesome_print (1.7.0)
byebug (9.0.5)
codeclimate-test-reporter (0.6.0)
simplecov (>= 0.7.1, < 1.0.0)
coderay (1.1.1)
concurrent-ruby (1.0.2)
docile (1.1.5)
i18n (0.7.0)
json (2.0.2)
method_source (0.8.2)
minitest (5.9.0)
Expand Down Expand Up @@ -49,9 +48,6 @@ GEM
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
slop (3.6.0)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unicode-display_width (1.1.1)

PLATFORMS
Expand All @@ -61,6 +57,7 @@ DEPENDENCIES
awesome_print
case_transform!
codeclimate-test-reporter
helix_runtime!
minitest
pry-byebug
rake
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ or
```bash
gem install case_transform
```

### Possible? Dpendencies

```
sudo apt-get install libsigsegv-dev
```

## Usage

```ruby
Expand Down
45 changes: 37 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'rake/clean'
require 'rake/testtask'
require 'bundler/setup'

# rubocop config copied from AMS
begin
Expand Down Expand Up @@ -32,18 +35,44 @@ else
end
end

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.ruby_opts = ['-r./test/test_helper.rb']
t.ruby_opts << ' -w' unless ENV['NO_WARN'] == 'true'
t.verbose = true

directory 'target'
directory 'lib/turbo_blank'

task :cargo_build do
sh 'cargo build --release'
sh 'gcc ' \
'-Wl,-force_load,target/release/libcase_transform.a ' \
'--shared -Wl,-undefined,dynamic_lookup -o lib/case_transform/native.bundle'
end
CLEAN.include('target')

file 'lib/case_transform/native.bundle' => ['lib/case_transform', :cargo_build] do
sh 'gcc ' \
'-Wl,-force_load,target/release/libcase_transform.a ' \
'--shared -Wl,-undefined,dynamic_lookup -o lib/case_transform/native.bundle'

end
CLOBBER.include('lib/case_transform/native.bundle')

task irb: 'lib/case_transform/native.bundle' do
exec 'irb -Ilib -rcase_transform'
end

task benchmark: 'lib/case_transform/native.bundle' do
exec 'ruby -Ilib benchmark.rb'
end

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList['test/**/*_test.rb']
end
#
task :test => "lib/case_transform/native.bundle"
task default: [:test, :rubocop]


desc 'CI test task'
task ci: [:default]
5 changes: 3 additions & 2 deletions case_transform.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 2.0'

s.add_runtime_dependency 'activesupport'

s.add_development_dependency 'rake'

# Rust
s.add_runtime_dependency 'helix_runtime'

# Quality Control
s.add_development_dependency 'rubocop'
s.add_development_dependency 'codeclimate-test-reporter'
Expand Down
15 changes: 9 additions & 6 deletions lib/case_transform.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/string'

require 'case_transform/version'

require 'helix_runtime'

RubyString = String
require 'case_transform/native'

module CaseTransform
module_function

Expand All @@ -16,7 +19,7 @@ def camel(value)
when Array then value.map { |item| camel(item) }
when Hash then value.deep_transform_keys! { |key| camel(key) }
when Symbol then camel(value.to_s).to_sym
when String then value.underscore.camelize
when String then value.to_snake_case.to_class_case
else value
end
end
Expand All @@ -30,7 +33,7 @@ def camel_lower(value)
when Array then value.map { |item| camel_lower(item) }
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
when Symbol then camel_lower(value.to_s).to_sym
when String then value.underscore.camelize(:lower)
when String then value.to_snake_case.to_camel_case
else value
end
end
Expand All @@ -45,7 +48,7 @@ def dash(value)
when Array then value.map { |item| dash(item) }
when Hash then value.deep_transform_keys! { |key| dash(key) }
when Symbol then dash(value.to_s).to_sym
when String then value.underscore.dasherize
when String then value.to_snake_case.to_kebab_case
else value
end
end
Expand All @@ -60,7 +63,7 @@ def underscore(value)
when Array then value.map { |item| underscore(item) }
when Hash then value.deep_transform_keys! { |key| underscore(key) }
when Symbol then underscore(value.to_s).to_sym
when String then value.underscore
when String then value.to_snake_case
else value
end
end
Expand Down
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[macro_use]
extern crate helix;
extern crate inflector;

// // dash: kebab-case
// use inflector::cases::kebabcase::to_kebab_case;
// // underscore: snake_case
// use inflector::cases::snakecase::to_snake_case;
// // camel_lower: camelCase
// use inflector::cases::camelcase::to_camel_case;
// // camel: ClassCase (PascalCase)
// use inflector::cases::classcase::to_class_case;
use inflector::Inflector;

declare_types! {
reopen class RubyString {
def to_snake_case(self) -> String {
self.to_string().to_snake_case()
}

def to_camel_case(self) -> String {
self.to_string().to_camel_case()
}

def to_class_case(self) -> String {
self.to_string().to_class_case()
}

def to_kebab_case(self) -> String {
self.to_string().to_kebab_case()
}
}
}


// TODO: Helix is eventually going to implement this
use helix::{UncheckedValue, ToRust};

impl ToString for RubyString {
fn to_string(&self) -> String {
let checked = self.0.to_checked().unwrap();
checked.to_rust()
}
}