Skip to content

Commit

Permalink
Turn devenv into a library
Browse files Browse the repository at this point in the history
This is a small example of how to turn the devenv binary into both a
library and a binary. The reason this works is that the
devenv/Cargo.toml file would allow you to specify one or more binaries,
and only a single library. However, because we are using the default
values for everything, we do not need to explicitly define these
sections. The library entry point will be devenv/src/lib.rs.

In the lib.rs file, we specify that we also want to use the log module.
So we define and export this module. So now anything that imports the
devenv package can just use devenv::log::* in order to access the pieces
defined inside of that module.

There is a problem including the command module since main.rs defines an
App struct, and then the command module defines an implementation of
that struct. The problem here being that lib.rs does not define the App
struct, so when the command module has use crate::App, it is not defined
as it is in the case of the devenv binary.
  • Loading branch information
ankhers committed May 27, 2024
1 parent a1290a1 commit 784fc12
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 32 deletions.
7 changes: 1 addition & 6 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ resolver = "2"
members = [
"devenv",
"devenv-run-tests",
"devenv_core",
]
2 changes: 1 addition & 1 deletion devenv-run-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2018"
[dependencies]
clap = { version = "3", features = ["derive"] }

devenv_core = { path = "../devenv_core" }
devenv= { path = "../devenv" }
6 changes: 6 additions & 0 deletions devenv-run-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use clap::Parser;
use devenv::log::Level;
use devenv::log::Logger;
use std::fs;
use std::path::PathBuf;

Expand Down Expand Up @@ -30,6 +32,10 @@ struct TestResult {
}

fn run_tests_in_directory(args: &Args) -> Result<Vec<TestResult>, Box<dyn std::error::Error>> {
let logger = Logger::new(Level::Info);

logger.info("Running Tests");

let cwd = std::env::current_dir()?;
let cwd = cwd.display();

Expand Down
1 change: 0 additions & 1 deletion devenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ which = "6.0.0"
whoami = "1.5.1"
xdg = "2.5.2"

devenv_core = { path = "../devenv_core" }
schemars = "0.8.16"
1 change: 1 addition & 0 deletions devenv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod log;
8 changes: 0 additions & 8 deletions devenv_core/Cargo.toml

This file was deleted.

14 changes: 0 additions & 14 deletions devenv_core/src/lib.rs

This file was deleted.

1 change: 0 additions & 1 deletion package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pkgs.rustPlatform.buildRustPackage {
"Cargo.lock"
"devenv(/\.*)?"
"devenv-run-tests(/\.*)?"
"devenv_core(/\.*)?"
];

cargoLock = {
Expand Down

0 comments on commit 784fc12

Please sign in to comment.