Stub in source_reader module
This commit is contained in:
parent
5226578593
commit
77755b3eb5
23
src/lib.rs
23
src/lib.rs
|
|
@ -8,42 +8,39 @@ use {
|
||||||
syn::{parse, LitStr},
|
syn::{parse, LitStr},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod source_reader;
|
||||||
|
|
||||||
struct ShaderModule {
|
struct ShaderModule {
|
||||||
source_file: std::path::PathBuf,
|
|
||||||
source_text: String,
|
source_text: String,
|
||||||
naga_module: naga::Module,
|
naga_module: naga::Module,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ShaderModule {
|
impl ShaderModule {
|
||||||
fn try_new(source_file: impl Into<std::path::PathBuf>) -> Result<Self, Error> {
|
fn try_new(source_file: impl Into<std::path::PathBuf>) -> Result<Self, Error> {
|
||||||
let source_file = source_file.into();
|
let source_filename = source_file.into();
|
||||||
let source_text = fs::read_to_string(&source_file).map_err(|err| {
|
let source_file = source_reader::read_source_file(source_filename)?;
|
||||||
Error::from_message(format!(
|
let source_text = source_file.full_text().to_string();
|
||||||
"Could not open \"{}\": {}",
|
|
||||||
&source_file.as_os_str().to_string_lossy(),
|
|
||||||
err
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let naga_module = wgsl::parse_str(&source_text).map_err(|err| {
|
let naga_module = wgsl::parse_str(&source_text).map_err(|err| {
|
||||||
if let Some(location) = err.location(&source_text) {
|
if let Some(location) = err.location(&source_text) {
|
||||||
|
let (inner_source_filename, inner_line_num) =
|
||||||
|
source_file.map_source_line_num(location.line_number as usize);
|
||||||
Error::Message(format!(
|
Error::Message(format!(
|
||||||
"Error in {} at line {}, column {}:\n{}",
|
"Error in {} at line {}, column {}:\n{}",
|
||||||
&source_file.as_os_str().to_string_lossy(),
|
&inner_source_filename.as_os_str().to_string_lossy(),
|
||||||
location.line_number,
|
inner_line_num,
|
||||||
location.line_position,
|
location.line_position,
|
||||||
err.message()
|
err.message()
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Error::Message(format!(
|
Error::Message(format!(
|
||||||
"Error in {}:\n{}",
|
"Error in {}:\n{}",
|
||||||
&source_file.as_os_str().to_string_lossy(),
|
&source_file.root_filename().as_os_str().to_string_lossy(),
|
||||||
err.message()
|
err.message()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
})?;
|
})?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
source_file,
|
|
||||||
source_text,
|
source_text,
|
||||||
naga_module,
|
naga_module,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
use {
|
||||||
|
crate::Error,
|
||||||
|
std::{
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SourceMap {
|
||||||
|
filename: PathBuf,
|
||||||
|
offset: usize,
|
||||||
|
total_lines: usize,
|
||||||
|
insertions: Vec<SourceMap>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SourceFile {
|
||||||
|
source_text: String,
|
||||||
|
source_map: SourceMap,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceFile {
|
||||||
|
pub fn root_filename(&self) -> &Path {
|
||||||
|
&self.source_map.filename
|
||||||
|
}
|
||||||
|
pub fn full_text(&self) -> &str {
|
||||||
|
&self.source_text
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn map_source_line_num(&self, line_num: usize) -> (&Path, usize) {
|
||||||
|
(&self.source_map.filename, line_num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_source_file(filename: PathBuf) -> Result<SourceFile, Error> {
|
||||||
|
let source_text = fs::read_to_string(&filename).map_err(|err| {
|
||||||
|
Error::from_message(format!(
|
||||||
|
"Could not open \"{}\": {}",
|
||||||
|
&filename.as_os_str().to_string_lossy(),
|
||||||
|
err
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
Ok(SourceFile {
|
||||||
|
source_text,
|
||||||
|
source_map: SourceMap {
|
||||||
|
filename,
|
||||||
|
offset: 0,
|
||||||
|
total_lines: 0,
|
||||||
|
insertions: vec![],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue