Struct nickel_lang_core::program::Program

source ·
pub struct Program<EC: EvalCache> {
    pub color_opt: ColorOpt,
    pub field: FieldPath,
    /* private fields */
}
Expand description

A Nickel program.

Manage a file database, which stores the original source code of the program and eventually the code of imported expressions, and a dictionary which stores corresponding parsed terms.

Fields§

§color_opt: ColorOpt

The color option to use when reporting errors.

§field: FieldPath

A specific field to act on. It is empty by default, which means that the whole program will 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.

Implementations§

source§

impl<EC: EvalCache> Program<EC>

source

pub fn new_from_stdin(trace: impl Write + 'static) -> Result<Self>

Create a program by reading it from the standard input.

source

pub fn new_from_files<I, P>( paths: I, trace: impl Write + 'static ) -> Result<Self>
where I: IntoIterator<Item = P>, P: Into<OsString>,

Create program from possibly multiple files. Each input path is turned into a Term::Import and the main program will be the BinaryOp::Merge of all the inputs.

source

pub fn new_from_file( path: impl Into<OsString>, trace: impl Write + 'static ) -> Result<Self>

source

pub fn new_from_source<T, S>( source: T, source_name: S, trace: impl Write + 'static ) -> Result<Self>
where T: Read, S: Into<OsString> + Clone,

Create a program by reading it from a generic source.

source

pub fn parse_override( &mut self, assignment: String, priority: MergePriority ) -> Result<FieldOverride, ParseError>

Parse an assignment of the form path.to_field=value as an override, with the provided merge priority. Assignments are typically provided by the user on the command line, as part of the customize mode.

This method simply calls FieldOverride::parse with the crate::cache::Cache of the current program.

source

pub fn parse_field_path( &mut self, path: String ) -> Result<FieldPath, ParseError>

Parse a dot-separated field path of the form path.to.field.

This method simply calls FieldPath::parse with the crate::cache::Cache of the current program.

source

pub fn add_overrides( &mut self, overrides: impl IntoIterator<Item = FieldOverride> )

source

pub fn add_import_paths<P>(&mut self, paths: impl Iterator<Item = P>)
where PathBuf: From<P>,

Adds import paths to the end of the list.

source

pub fn parse(&mut self) -> Result<RichTerm, Error>

Only parse the program, don’t typecheck or evaluate. returns the RichTerm AST

source

pub fn eval(&mut self) -> Result<RichTerm, Error>

Parse if necessary, typecheck and then evaluate the program.

source

pub fn eval_full(&mut self) -> Result<RichTerm, Error>

Same as eval, but proceeds to a full evaluation.

source

pub fn eval_full_for_export(&mut self) -> Result<RichTerm, Error>

Same as eval, but proceeds to a full evaluation. Optionally take a set of overrides that are to be applied to the term (in practice, to be merged with).

Skips record fields marked not_exported.

§Arguments
  • override is a list of overrides in the form of an iterator of FieldOverrides. Each override is imported in a separate in-memory source, for complete isolation (this way, overrides can’t accidentally or intentionally capture other fields of the configuration). A stub record is then built, which has all fields defined by overrides, and values are an import referring to the corresponding isolated value. This stub is finally merged with the current program before being evaluated for import.
source

pub fn eval_deep(&mut self) -> Result<RichTerm, Error>

Same as eval_full, but does not substitute all variables.

source

pub fn query(&mut self) -> Result<Field, Error>

Prepare for evaluation, then fetch the metadata of self.field, or list the fields of the whole program if self.field is empty.

source

pub fn typecheck(&mut self) -> Result<(), Error>

Load, parse, and typecheck the program and the standard library, if not already done.

source

pub fn report<E>(&mut self, error: E, format: ErrorFormat)

Wrapper for report.

source

pub fn report_as_str<E>(&mut self, error: E) -> String

Build an error report as a string and return it.

source

pub fn eval_record_spine(&mut self) -> Result<RichTerm, Error>

Evaluate a program into a record spine, a form suitable for extracting the general structure of a configuration, and in particular its interface (fields that might need to be filled).

This form is used to extract documentation through nickel doc, for example.

§Record spine

By record spine, we mean that the result is a tree of evaluated nested records, and leafs are either non-record values in WHNF or partial expressions left unevaluated1. For example, the record spine of:

{
  foo = {bar = 1 + 1} & {baz.subbaz = [some_func "some_arg"] @ ["snd" ++ "_elt"]},
  input,
  depdt = input & {extension = 2},
}

is

{
  foo = {
    bar = 2,
    baz = {
      subbaz = [some_func "some_arg", "snd" ++ "_elt"],
    },
  },
  input,
  depdt = input & {extension = 2},
}

To evaluate a term to a record spine, we first evaluate it to a WHNF and then:

  • If the result is a record, we recursively evaluate subfields to record spines
  • If the result isn’t a record, it is returned as it is
  • If the evaluation fails with crate::error::EvalError::MissingFieldDef, the original term is returned unevaluated1
  • If any other error occurs, the evaluation fails and returns the error.

  1. Because we want to handle partial configurations as well, crate::error::EvalError::MissingFieldDef errors are ignored: if this is encountered when evaluating a field, this field is just left as it is and the evaluation proceeds. 

source

pub fn extract_doc(&mut self) -> Result<ExtractedDocumentation, Error>

Extract documentation from the program

source

pub fn set_skip_stdlib(&mut self)

source

pub fn pprint_ast( &mut self, out: &mut impl Write, apply_transforms: bool ) -> Result<(), Error>

Auto Trait Implementations§

§

impl<EC> !Freeze for Program<EC>

§

impl<EC> !RefUnwindSafe for Program<EC>

§

impl<EC> !Send for Program<EC>

§

impl<EC> !Sync for Program<EC>

§

impl<EC> Unpin for Program<EC>
where EC: Unpin, <EC as Cache>::UpdateIndex: Unpin,

§

impl<EC> !UnwindSafe for Program<EC>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

source§

fn exact_from(value: T) -> U

source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

source§

fn exact_into(self) -> U

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

source§

fn wrapping_into(self) -> U