-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(day01): initial monorepo using bazel with a rust example
- Loading branch information
Showing
11 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Test | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: bazelbuild/setup-bazelisk@v2 | ||
- uses: actions/cache@v3 | ||
with: | ||
path: "~/.cache/bazel" | ||
key: bazel | ||
- run: bazel build //... | ||
- run: bazel test //... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## IDE | ||
.idea | ||
|
||
## Bazel | ||
# Ignore all bazel-* symlinks. There is no full list since this can change | ||
# based on the name of the directory bazel is cloned into. | ||
/bazel-* | ||
|
||
# Directories for the Bazel IntelliJ plugin containing the generated | ||
# IntelliJ project files and plugin configuration. Seperate directories are | ||
# for the IntelliJ, Android Studio and CLion versions of the plugin. | ||
/.ijwb/ | ||
/.aswb/ | ||
/.clwb/ | ||
|
||
## Rust | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 15-Day Coding Challenges | ||
|
||
| Day | Date | Challenge | Link | | ||
|-----|------------|----------------------------------------------------------------------------------------------------------------|-------------------------------------------| | ||
| 01 | 3 Sep 2023 | Initialize this repository as a monorepo using Bazel for multi-language development, include a Rust program example and add a GitHub Action to test all the projects | [challenges/day-01](./challenges/day-01) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
# Loads the `rules_rust` repository rule | ||
# See https://github.com/bazelbuild/rules_rust/releases | ||
http_archive( | ||
name = "rules_rust", | ||
sha256 = "db89135f4d1eaa047b9f5518ba4037284b43fc87386d08c1d1fe91708e3730ae", | ||
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.27.0/rules_rust-v0.27.0.tar.gz"], | ||
) | ||
|
||
# Loads the `rules_rust_dependencies` and `rust_register_toolchains` function definitions | ||
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") | ||
|
||
# Adds the necessary dependencies for the Rust rules | ||
rules_rust_dependencies() | ||
|
||
# Registers Rust toolchains with the given versions and editions | ||
rust_register_toolchains( | ||
# Specifies the Rust edition to use for the registered toolchains | ||
edition = "2021", | ||
versions = ["1.72.0"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Loads the Rust rules and the `rust_binary` & `rust_test` function definitions | ||
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test") | ||
|
||
# Declares a Rust binary target with the given name | ||
rust_binary( | ||
name = "day_01", | ||
|
||
# Specifies the source file for the binary | ||
srcs = glob([ | ||
"src/**/*.rs", | ||
]), | ||
|
||
# Specifies the Rust edition to use for this binary | ||
edition = "2021", | ||
) | ||
|
||
# Declares a Rust test target with the given name | ||
rust_test( | ||
name = "day_01_test", | ||
|
||
# Uses the same crate name as the underlying “:day_01” crate | ||
crate = ":day_01", | ||
|
||
# Specifies the Rust edition to use for this test | ||
edition = "2021", | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "day-01" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Day 01 | ||
|
||
## Challenge description | ||
|
||
Initialize this repository as a monorepo using Bazel for multi-language development, include a Rust program example and add a GitHub Action to test all the projects. | ||
|
||
- [x] Monorepo | ||
- [x] Bazel | ||
- [x] Rust program | ||
- [x] GitHub Action | ||
|
||
## Getting Started | ||
|
||
```bash | ||
bazel run //challenges/day-01:day_01 | ||
bazel test //challenges/day-01:day_01_test | ||
``` | ||
|
||
## References | ||
|
||
- Doc: [Bazelisk](https://github.com/bazelbuild/bazelisk/blob/master/README.md) | ||
- Doc: [Bazel Versions](https://bazel.build/release#support-matrix) | ||
- Doc: [GitHub Action - bazelbuild/setup-bazelisk](https://github.com/bazelbuild/setup-bazelisk) | ||
- API: [rust_binary](http://bazelbuild.github.io/rules_rust/flatten.html#rust_binary) | ||
- API: [rust_test](http://bazelbuild.github.io/rules_rust/flatten.html#rust_test) | ||
- Article: [kriscfoster/multi-language-bazel-monorepo](https://github.com/kriscfoster/multi-language-bazel-monorepo) | ||
- Article: [Bazel with Rust](https://earthly.dev/blog/bazel-with-rust/) | ||
- Article: [Building Rust Workspace with Bazel](https://www.tweag.io/blog/2023-07-27-building-rust-workspace-with-bazel/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
mod substring; | ||
|
||
fn main() { | ||
let s = "Hello, World!"; | ||
|
||
let substring = substring::find_substring(s, "World"); | ||
println!("substring: {:?}", substring); | ||
|
||
let new_string = substring::replace_substring(s, "World", "Rust"); | ||
println!("New string: {}", new_string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pub fn find_substring<'a>(s: &'a str, substring: &str) -> Option<&'a str> { | ||
s.find(substring).map(|index| &s[index..index + substring.len()]) | ||
} | ||
|
||
pub fn replace_substring(s: &str, from: &str, to: &str) -> String { | ||
s.replace(from, to) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{find_substring, replace_substring}; | ||
|
||
#[test] | ||
fn test_find_substring() { | ||
assert_eq!(find_substring("hello", "ll"), Some("ll")); | ||
assert_eq!(find_substring("hello", "world"), None); | ||
} | ||
|
||
#[test] | ||
fn test_replace_substring() { | ||
assert_eq!(replace_substring("hello", "ll", "ww"), "hewwo"); | ||
assert_eq!(replace_substring("hello", "world", "ww"), "hello"); | ||
} | ||
} |