Skip to content

Commit

Permalink
feat(day01): initial monorepo using bazel with a rust example
Browse files Browse the repository at this point in the history
  • Loading branch information
dorayx committed Sep 3, 2023
1 parent 5e0172e commit 25494e9
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.3.2
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
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 //...
26 changes: 26 additions & 0 deletions .gitignore
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
5 changes: 5 additions & 0 deletions README.md
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) |
22 changes: 22 additions & 0 deletions WORKSPACE.bazel
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"],
)
26 changes: 26 additions & 0 deletions challenges/day-01/BUILD.bazel
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",
)
7 changes: 7 additions & 0 deletions challenges/day-01/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions challenges/day-01/Cargo.toml
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]
28 changes: 28 additions & 0 deletions challenges/day-01/README.md
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/)
11 changes: 11 additions & 0 deletions challenges/day-01/src/main.rs
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);
}
24 changes: 24 additions & 0 deletions challenges/day-01/src/substring.rs
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");
}
}

0 comments on commit 25494e9

Please sign in to comment.