pub struct Session {
    pub sm: Arc<SourceMap>,
    pub diag_handler: Arc<DiagnosticHandler>,
}
Expand description

Represents the data associated with a compilation session for a single crate.

Note: TODO(zongz): This is a WIP structure. Currently only contains the part related to error diagnostic displaying.

Fields

sm: Arc<SourceMap>diag_handler: Arc<DiagnosticHandler>

Implementations

Construct a Session

Examples

// 1. You should create a new `SourceMap` wrapped with `Arc`.
let filename = fs::canonicalize(&PathBuf::from("./src/test_datas/code_snippet")).unwrap().display().to_string();
let src = std::fs::read_to_string(filename.clone()).unwrap();
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
sm.new_source_file(PathBuf::from(filename.clone()).into(), src.to_string());

// 2. You should create a new `DiagnosticHandler` wrapped with `Arc`.
let diag_handler = Arc::new(DiagnosticHandler::new_with_template_dir("./src/test_datas/locales/en-US").unwrap());

// 3. Create `Session`
let sess = Session::new(sm, diag_handler);

Construct a Session with file name and optional source code.

In the method, a SourceMap with a SourceFile will be created from filename and the optional source code code.

Note: code has higher priority than filename, If code is not None and the content in file filename is not the same as code, then the content in code will be used as the source code.

If code is None, the session will use the content of file filename as source code.

Examples
const CARGO_ROOT: &str = env!("CARGO_MANIFEST_DIR");
let mut cargo_file_path = PathBuf::from(CARGO_ROOT);
cargo_file_path.push("src/test_datas/code_snippet");
let abs_path = cargo_file_path.to_str().unwrap();

let sess = Session::new_with_file_and_code(abs_path, None);

The sess will take the content of file abs_path as source code.

const CARGO_ROOT: &str = env!("CARGO_MANIFEST_DIR");
let mut cargo_file_path = PathBuf::from(CARGO_ROOT);
cargo_file_path.push("src/test_datas/code_snippet");
let abs_path = cargo_file_path.to_str().unwrap();

let sess = Session::new_with_file_and_code(abs_path, Some("This is tmp source code"));

The sess will take “This is tmp source code” as source code.

Construct a Session with source code.

In the method, a SourceMap with a SourceFile will be created from an empty path.

Examples
let sess = Session::new_with_src_code("This is the source code");

Emit error diagnostic to terminal.

Panics

After emitting the error diagnositc, the program will panic.

Examples

If you want to emit an error diagnostic.


// 1. Create your own error type.
struct MyError;

// 2. Implement trait `SessionDiagnostic` manually.
impl SessionDiagnostic for MyError {
    fn into_diagnostic(self, sess: &Session) -> Result<Diagnostic<DiagnosticStyle>> {
        let mut diag = Diagnostic::<DiagnosticStyle>::new();
        // 1. Label Component
        let label_component = Box::new(Label::Error("error".to_string()));
        diag.append_component(label_component);
        Ok(diag)
    }
}

let result = std::panic::catch_unwind(|| {
   // 3. Create a Session.
   let sess = Session::new_with_src_code("test code").unwrap();
   // 4. Emit the error diagnostic.
   sess.emit_err(MyError {}).unwrap();
});
assert!(result.is_err());

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more