Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfxb committed Jul 11, 2024
1 parent b48ff2c commit 5eeded1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 66 deletions.
2 changes: 1 addition & 1 deletion wright/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Build script for wright.
//! This is used for capturing build environment info which is used at runtime.
//! This is used for capturing build environment info which is used at runtime.
use rustc_version::{version_meta, Channel};

Expand Down
19 changes: 7 additions & 12 deletions wright/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// If complile without the standard library if the user chooses to do so.
#![cfg_attr(not(any(feature = "std", test)), no_std)]

// We want to enforce good stuff by default.
#![deny(missing_copy_implementations, missing_debug_implementations)]
#![deny(rustdoc::broken_intra_doc_links)]
#![warn(missing_docs)]

// Compiler directive to get docs.rs (which uses the nightly version of the rust compiler) to show
// info about featurer required for various modules and functionality.
//
Expand All @@ -21,22 +19,19 @@
))]
compile_error!("Memory mapped files not available on WASM targets");

// If the "none" feature is enabled, make sure the user has no other features enabled.
//
// If the "none" feature is enabled, make sure the user has no other features enabled.
//
// Currently all of the features besides "none" depend on "std" so if both "none" and "std"
// are present, raise an error at compile time.
//
// Make sure to keep this updated as more features are added.
#[cfg(all(
feature = "none",
any(feature = "std")
))]
// are present, raise an error at compile time.
//
// Make sure to keep this updated as more features are added.
#[cfg(all(feature = "none", any(feature = "std")))]
compile_error!("feature \"none\" is enabled, which restricts the usage of any other features including \"std\".");

/// The version of this copy of Wright.
pub const WRIGHT_VERSION: &str = build_info::PKG_VERSION;

/// Build information about this copy of wright, provided using <https://crates.io/crates/built>.
/// Build information about this copy of wright, provided using <https://crates.io/crates/built>.
pub mod build_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
Expand Down
57 changes: 29 additions & 28 deletions wright/src/reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
//! [codespan-reporting]: https://crates.io/crates/codespan-reporting
//! [ariadne]: https://crates.io/crates/ariadne
use codespan_reporting::diagnostic::Label;
use crate::source_tracking::SourceMap;
use crate::source_tracking::{fragment::Fragment, source::SourceId};
use codespan_reporting::diagnostic::Diagnostic as CRDiagnostic;
use codespan_reporting::diagnostic::Label;
use codespan_reporting::files::Error as CRError;
use codespan_reporting::term::Config;
use crate::source_tracking::SourceMap;
use crate::source_tracking::{fragment::Fragment, source::SourceId};
use std::sync::Mutex;
use termcolor::{ColorChoice, StandardStream, WriteColor};

#[cfg(doc)]
use crate::source_tracking::source::Source;

// Publicly re-export codespan's severity type, since it's pretty much exactly what we want/use.
// Publicly re-export codespan's severity type, since it's pretty much exactly what we want/use.
pub use codespan_reporting::diagnostic::Severity;

/// The style/priority applied to a [Highlight] being rendered.
/// The style/priority applied to a [Highlight] being rendered.
pub use codespan_reporting::diagnostic::LabelStyle;

/// The global-static color choice for printing to the standard output.
/// The global-static color choice for printing to the standard output.
static STDOUT_COLOR_CHOICE: Mutex<ColorChoice> = Mutex::new(ColorChoice::Auto);

/// Set the [ColorChoice] to use when printing [Diagnostic]s to the standard output.
pub fn set_stdout_color(c: ColorChoice) {
*STDOUT_COLOR_CHOICE.lock().unwrap() = c;
}

/// Get the current [ColorChoice] used when printing [Diagnostic]s to the standard output.
/// Get the current [ColorChoice] used when printing [Diagnostic]s to the standard output.
pub fn get_stdout_color() -> ColorChoice {
*STDOUT_COLOR_CHOICE.lock().unwrap()
}

/// Wright's [Diagnostic] type wraps one from [codespan_reporting] to make it compatible with
/// Wright's [Diagnostic] type wraps one from [codespan_reporting] to make it compatible with
/// things like [Fragment] and [SourceId].
#[derive(Debug)]
pub struct Diagnostic(pub CRDiagnostic<SourceId>);
Expand All @@ -47,34 +47,31 @@ impl Diagnostic {
Diagnostic(CRDiagnostic::new(severity))
}

/// Construct a new [Diagnostic] representing a wright compiler bug.
/// Construct a new [Diagnostic] representing a wright compiler bug.
#[inline]
pub fn bug() -> Self {
Self::new(Severity::Bug)
}
/// Construct a new [Diagnostic] representing an error.

/// Construct a new [Diagnostic] representing an error.
#[inline]
pub fn error() -> Self {
Self::new(Severity::Error)
}


/// Construct a new [Diagnostic] representing a warning.
/// Construct a new [Diagnostic] representing a warning.
#[inline]
pub fn warning() -> Self {
Self::new(Severity::Warning)
}


/// Construct a new [Diagnostic] representing a note.
/// Construct a new [Diagnostic] representing a note.
#[inline]
pub fn note() -> Self {
Self::new(Severity::Note)
}


/// Construct a new [Diagnostic] representing a note to the user.
/// Construct a new [Diagnostic] representing a note to the user.
#[inline]
pub fn help() -> Self {
Self::new(Severity::Note)
Expand All @@ -96,20 +93,25 @@ impl Diagnostic {
self
}

/// Add all the [Highlight]s from a given [Iterator] to this [Diagnostic].
/// Add all the [Highlight]s from a given [Iterator] to this [Diagnostic].
pub fn with_highlights(mut self, highlights: impl IntoIterator<Item = Highlight>) -> Self {
self.0.labels.extend(highlights.into_iter().map(Into::into));
self
}

/// Write this [Diagnostic] to a given [WriteColor]. This will error if any of the [Highlight]s are not in
/// Write this [Diagnostic] to a given [WriteColor]. This will error if any of the [Highlight]s are not in
/// the referenced [SourceMap], or if any were constructed from invalid [Fragment]s.
pub fn write(&self, map: &SourceMap, writer: &mut dyn WriteColor, config: &Config) -> Result<(), CRError> {
pub fn write(
&self,
map: &SourceMap,
writer: &mut dyn WriteColor,
config: &Config,
) -> Result<(), CRError> {
codespan_reporting::term::emit(writer, config, map, &self.0)
}

/// Print this [Diagnostic] to the standard output. This locks the standard output until the diagnostic is printed.
/// This uses the global [get_stdout_color] function to determine whether or not to use colors while printing.
/// This uses the global [get_stdout_color] function to determine whether or not to use colors while printing.
/// This uses the [Config::default] configuration from [codespan_reporting] when printing.
pub fn print(&self, map: &SourceMap) -> Result<(), codespan_reporting::files::Error> {
let stream = StandardStream::stdout(get_stdout_color());
Expand All @@ -118,17 +120,16 @@ impl Diagnostic {
}
}


/// A highlighted section of a [Source] that's used in a [Diagnostic].
#[derive(Clone, Debug)]
pub struct Highlight {
/// The style/importance of this [Highlight]. [Highlight]s with [LabelStyle::Primary] are given priority
/// when being displayed.
/// The style/importance of this [Highlight]. [Highlight]s with [LabelStyle::Primary] are given priority
/// when being displayed.
pub style: LabelStyle,
/// The [Fragment] containing the relevant section of code.
pub fragment: Fragment,
/// The message attached to this [Highlight]. This can be empty, in which case the [Fragment] will
/// just be underlined when displayed.
/// just be underlined when displayed.
pub message: String,
}

Expand All @@ -141,7 +142,7 @@ impl Highlight {
message: message.into(),
}
}

/// Construct a new [Highlight] with [LabelStyle::Secondary].
pub fn secondary(fragment: Fragment, message: impl Into<String>) -> Self {
Highlight {
Expand All @@ -160,11 +161,11 @@ impl Highlight {

impl From<Highlight> for Label<SourceId> {
fn from(value: Highlight) -> Self {
Label {
Label {
style: value.style,
file_id: value.fragment.source.id,
range: value.fragment.range,
message: value.message
message: value.message,
}
}
}
52 changes: 33 additions & 19 deletions wright/src/source_tracking.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Types and traits for tracking source code fed to the wright compiler.
use self::source::Source;
use dashmap::DashMap;
use filename::FileName;
use immutable_string::ImmutableString;
use source::SourceId;
use dashmap::DashMap;
use self::source::Source;
use std::sync::Arc;

#[cfg(feature = "reporting")]
Expand All @@ -21,10 +21,10 @@ pub type SourceRef = Arc<Source>;
/// Storage for [Source]s used and referenced in compiling a wright project.
#[derive(Debug, Default)]
pub struct SourceMap {
/// Internally, we use [DashMap] for a concurrent hashmap from [Source::id]s to their [Arc]'d
/// Internally, we use [DashMap] for a concurrent hashmap from [Source::id]s to their [Arc]'d
///
/// Each source is wrapped in an [Arc] to make them all accessible without holding a reference to this map
/// directly.
/// Each source is wrapped in an [Arc] to make them all accessible without holding a reference to this map
/// directly.
inner: DashMap<SourceId, SourceRef>,
}

Expand All @@ -44,22 +44,19 @@ impl SourceMap {
source
}


/// Get a reference to a [Source] stored in this [SourceMap] using it's [Source::id].
///
/// This is currently `O(1)` since [SourceMap] uses a [DashMap] internally.
///
///
/// This is currently `O(1)` since [SourceMap] uses a [DashMap] internally.
///
/// Returns [None] if the [Source] with the given [Source::id] is not in this [SourceMap].
pub fn get(&self, id: SourceId) -> Option<SourceRef> {
self.inner
.get(&id)
.map(|source| Arc::clone(&source))
self.inner.get(&id).map(|source| Arc::clone(&source))
}
}

#[cfg(feature = "reporting")]
impl<'f> codespan_reporting::files::Files<'f> for SourceMap {
type FileId = SourceId;
type FileId = SourceId;

type Name = FileName;

Expand All @@ -71,18 +68,35 @@ impl<'f> codespan_reporting::files::Files<'f> for SourceMap {
.ok_or(Error::FileMissing)
}

fn source(&'f self, id: Self::FileId) -> Result<Self::Source, codespan_reporting::files::Error> {
fn source(
&'f self,
id: Self::FileId,
) -> Result<Self::Source, codespan_reporting::files::Error> {
self.get(id)
.map(|source| source.source().clone())
.ok_or(Error::FileMissing)
}

fn line_index(&'f self, id: Self::FileId, byte_index: usize) -> Result<usize, codespan_reporting::files::Error> {
Ok(self.get(id).ok_or(Error::FileMissing)?.line_index(byte_index))

fn line_index(
&'f self,
id: Self::FileId,
byte_index: usize,
) -> Result<usize, codespan_reporting::files::Error> {
Ok(self
.get(id)
.ok_or(Error::FileMissing)?
.line_index(byte_index))
}

fn line_range(&'f self, id: Self::FileId, line_index: usize) -> Result<std::ops::Range<usize>, codespan_reporting::files::Error> {
Ok(self.get(id).ok_or(Error::FileMissing)?.get_line(line_index).range)
fn line_range(
&'f self,
id: Self::FileId,
line_index: usize,
) -> Result<std::ops::Range<usize>, codespan_reporting::files::Error> {
Ok(self
.get(id)
.ok_or(Error::FileMissing)?
.get_line(line_index)
.range)
}
}
7 changes: 6 additions & 1 deletion wright/src/source_tracking/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ impl Fragment {
/// Get the number of bytes between the start of the line that this [Fragment] starts on and the start of this
/// [Fragment]
pub fn starting_col_index(&self) -> usize {
self.range.start - Arc::clone(&self.source).get_line(self.line_indices().start).range.start
let line_start_index = Arc::clone(&self.source)
.get_line(self.line_indices().start)
.range
.start;

self.range.start - line_start_index
}
}

Expand Down
10 changes: 5 additions & 5 deletions wright/src/source_tracking/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub const FILE_LOCK_WARNING_TIME: Duration = Duration::from_secs(5);
/// This is just a global [u64] that gets incremented everytime a new source is instantiated.
static SOURCE_ID_GENERATOR: AtomicU64 = AtomicU64::new(1);

/// A process-unique source id, that is atomically generated and assigned to each [Source] on creation.
/// A process-unique source id, that is atomically generated and assigned to each [Source] on creation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SourceId(u64);

Expand Down Expand Up @@ -214,10 +214,10 @@ impl Source {
pub fn count_lines(&self) -> usize {
self.line_starts.len()
}
/// Get the line index that a byte index is on in this [Source].
///
/// If the byte index is greater than the length of the [Source] then the highest possible index will be returned.

/// Get the line index that a byte index is on in this [Source].
///
/// If the byte index is greater than the length of the [Source] then the highest possible index will be returned.
pub fn line_index(&self, byte_index: usize) -> usize {
// Get a list of the byte indices that lines start on.
let line_starts: &[usize] = self.line_starts();
Expand Down

0 comments on commit 5eeded1

Please sign in to comment.