Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify SourceFile::new and new_empty #43

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ pub struct SourceFile {
}

impl SourceFile {
/// Create an empty SourceFile with the given name
pub fn new_empty(origin_path: &str) -> Result<Self> {
/// Create an empty SourceFile with the given name.
///
/// See [`SourceFile::new`][] for details.
pub fn new_empty(origin_path: &str) -> Self {
Self::new(origin_path, String::new())
}

/// Create a new source file with the given name and contents.
pub fn new(origin_path: &str, contents: String) -> Result<Self> {
Ok(SourceFile {
///
/// This is intended for situations where you have the contents already
/// and just want a SourceFile to manage it. This is appropriate for
/// strings that were constructed dynamically or for tests.
///
/// The origin_path will be used as the filename as well.
pub fn new(origin_path: &str, contents: String) -> Self {
SourceFile {
inner: Arc::new(SourceFileInner {
filename: LocalAsset::filename(origin_path)?,
filename: origin_path.to_owned(),
origin_path: origin_path.to_owned(),
contents,
}),
})
}
}

#[cfg(feature = "remote")]
Expand Down
4 changes: 2 additions & 2 deletions tests/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use miette::SourceCode;
fn substr_span() {
// Make the file
let contents = String::from("hello !there!");
let source = axoasset::SourceFile::new("file.md", contents).unwrap();
let source = axoasset::SourceFile::new("file.md", contents);

// Do some random parsing operation
let mut parse = source.contents().split('!');
Expand All @@ -23,7 +23,7 @@ fn substr_span() {
fn substr_span_invalid() {
// Make the file
let contents = String::from("hello !there!");
let source = axoasset::SourceFile::new("file.md", contents).unwrap();
let source = axoasset::SourceFile::new("file.md", contents);

// Get the span for a non-substring (string literal isn't pointing into the String)
let there_span = source.span_for_substr("there");
Expand Down