Skip to content

Commit

Permalink
Clean up unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
jneem committed May 24, 2024
1 parent cb6c2c4 commit 0cc7468
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 72 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl PackageCommand {
pub fn run(self, _global: GlobalOptions) -> CliResult<()> {
match &self.command {
Command::GenerateLockfile => {
self.load_manifest()?.lock()?;
self.load_manifest()?.regenerate_lock()?;
}
Command::DebugResolution => {
let path = self.find_manifest()?;
Expand Down
24 changes: 22 additions & 2 deletions core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ use crate::{
label::Label,
metrics::increment,
package::PackageMap,
position::TermPos,
term::{
make as mk_term, make::builder, record::Field, BinaryOp, MergePriority, RichTerm, Term,
make::{self as mk_term, builder},
record::Field,
BinaryOp, MergePriority, RichTerm, RuntimeContract, Term,
},
};

Expand Down Expand Up @@ -191,6 +194,9 @@ pub struct Program<EC: EvalCache> {
/// be evaluated, but it can be set by the user (for example by the `--field` argument of the
/// CLI) to evaluate only a specific field.
pub field: FieldPath,
/// Extra contracts to apply to the main program source. Note that the contract is applied to
/// the whole value before fields are extracted.
pub contracts: Vec<RuntimeContract>,
}

/// The Possible Input Sources, anything that a Nickel program can be created from
Expand Down Expand Up @@ -235,6 +241,7 @@ impl<EC: EvalCache> Program<EC> {
color_opt: clap::ColorChoice::Auto.into(),
overrides: Vec::new(),
field: FieldPath::new(),
contracts: Vec::new(),
})
}

Expand Down Expand Up @@ -278,6 +285,7 @@ impl<EC: EvalCache> Program<EC> {
color_opt: clap::ColorChoice::Auto.into(),
overrides: Vec::new(),
field: FieldPath::new(),
contracts: Vec::new(),
})
}

Expand Down Expand Up @@ -360,6 +368,10 @@ impl<EC: EvalCache> Program<EC> {
self.overrides.extend(overrides);
}

pub fn add_contract(&mut self, contract: RuntimeContract) {
self.contracts.push(contract);
}

/// Adds import paths to the end of the list.
pub fn add_import_paths<P>(&mut self, paths: impl Iterator<Item = P>)
where
Expand Down Expand Up @@ -407,7 +419,7 @@ impl<EC: EvalCache> Program<EC> {
fn prepare_eval_impl(&mut self, for_query: bool) -> Result<Closure, Error> {
// If there are no overrides, we avoid the boilerplate of creating an empty record and
// merging it with the current program
let prepared_body = if self.overrides.is_empty() {
let mut prepared_body = if self.overrides.is_empty() {
self.vm.prepare_eval(self.main_id)?
} else {
let mut record = builder::Record::new();
Expand Down Expand Up @@ -436,6 +448,14 @@ impl<EC: EvalCache> Program<EC> {
mk_term::op2(BinaryOp::Merge(Label::default().into()), t, built_record)
};

if !self.contracts.is_empty() {
prepared_body = RuntimeContract::apply_all(
prepared_body,
self.contracts.iter().cloned(),
TermPos::None,
);
}

let prepared = Closure::atomic_closure(prepared_body);

let result = if for_query {
Expand Down
5 changes: 0 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@
})
)
boost # implicit dependency of nix
openssl # for git2; not needed if we switch to gitoxide
];

# seems to be needed for consumer cargoArtifacts to be able to use
Expand All @@ -331,10 +330,6 @@
version
cargoArtifacts;

# openssl and pkg-config are for git2; not needed if we switch to gitoxide
buildInputs = with pkgs; [ pkg-config ];
nativeBuildInputs = with pkgs; [ openssl ];

cargoExtraArgs = "${cargoBuildExtraArgs} ${extraBuildArgs} --package ${cargoPackage}";
} // extraArgs);

Expand Down
2 changes: 1 addition & 1 deletion package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ readme.workspace = true

[dependencies]
directories.workspace = true
gix = { version = "0.63.0", features = ["blocking-network-client", "blocking-http-transport-reqwest-rust-tls"] }
gix = { version = "0.63.0", features = ["blocking-network-client", "blocking-http-transport-reqwest-rust-tls", "serde"] }
nickel-lang-core = { workspace = true, default-features = false }
serde.workspace = true
serde_json.workspace = true
Expand Down
9 changes: 4 additions & 5 deletions package/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::PathBuf;

use directories::ProjectDirs;
use serde::{Deserialize, Serialize};

pub mod error;
pub mod lock;
Expand All @@ -13,17 +12,17 @@ pub use manifest::ManifestFile;
/// A source includes the place to fetch a package from (e.g. git or a registry),
/// along with possibly some narrowing-down of the allowed versions (e.g. a range
/// of versions, or a git commit id).
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug)]
pub enum PackageSource {
// TODO: support branches other than HEAD
// TODO: allow targeting branches or revisions, and allow supplying a relative path
Git {
url: String,
url: gix::Url,
//tree: Option<git2::Oid>,
},
Path {
path: PathBuf,
},
// TODO: non-git packages
// TODO: packages in a repository
}

impl PackageSource {
Expand Down
Loading

0 comments on commit 0cc7468

Please sign in to comment.