Skip to main content

SourceFile

Struct SourceFile 

Source
pub struct SourceFile {
    pub name: String,
    pub text: String,
    /* private fields */
}
Expand description

One source file: its name and text.

Fields§

§name: String

The name the file was opened with, for diagnostics.

§text: String

The full text.

Implementations§

Source§

impl SourceFile

Source

pub fn line_column(&self, offset: u32) -> (usize, usize)

The 1-based line and column of a byte offset, with the line numbers that #line gives.

Examples found in repository?
examples/milkdrop_corpus.rs (line 54)
15fn main() {
16    let mut args = std::env::args().skip(1);
17    let Some(root) = args.next() else {
18        eprintln!("usage: milkdrop_corpus <preset folder> [examples per error]");
19        std::process::exit(2);
20    };
21    let examples: usize = args.next().and_then(|n| n.parse().ok()).unwrap_or(1);
22    let mut presets = Vec::new();
23    collect(Path::new(&root), &mut presets);
24    presets.sort();
25
26    let started = Instant::now();
27    let (mut shaders, mut failed) = (0usize, 0usize);
28    let (mut max_parens, mut max_braces) = (0usize, 0usize);
29    let mut groups: BTreeMap<String, Vec<String>> = BTreeMap::new();
30    for path in &presets {
31        let Ok(bytes) = std::fs::read(path) else {
32            continue;
33        };
34        let text = String::from_utf8_lossy(&bytes);
35        for (kind, shader) in shaders_of(&text) {
36            shaders += 1;
37            let source =
38                until_last_brace(&shader).replace("shader_body", "void shader_body_main()");
39            let (parens, braces) = nesting(&source);
40            max_parens = max_parens.max(parens);
41            max_braces = max_braces.max(braces);
42            let name = format!("{} [{kind}]", path.display());
43            let parsed = parse(&name, &source, Options::default());
44            let Some(first) = parsed.diagnostics.first() else {
45                continue;
46            };
47            failed += 1;
48            let group = groups.entry(group_of(&first.message)).or_default();
49            if group.len() < examples {
50                let line = parsed
51                    .files
52                    .get(first.span.file)
53                    .map(|f| {
54                        let (line, _) = f.line_column(first.span.start);
55                        f.text
56                            .lines()
57                            .nth(line - 1)
58                            .unwrap_or("")
59                            .trim()
60                            .to_string()
61                    })
62                    .unwrap_or_default();
63                group.push(format!("{}\n        {line}", first.display(&parsed.files)));
64            } else {
65                group.push(String::new());
66            }
67        }
68    }
69
70    let mut ranked: Vec<_> = groups.into_iter().collect();
71    ranked.sort_by_key(|(_, v)| std::cmp::Reverse(v.len()));
72    for (message, hits) in &ranked {
73        println!("{:>6}  {message}", hits.len());
74        for example in hits.iter().filter(|e| !e.is_empty()) {
75            println!("        {example}");
76        }
77    }
78    println!(
79        "{} presets, {shaders} shaders, {} parsed, {failed} failed ({:.2}% parsed) in {:.2?}",
80        presets.len(),
81        shaders - failed,
82        100.0 * (shaders - failed) as f64 / shaders.max(1) as f64,
83        started.elapsed()
84    );
85    println!("deepest nesting: {max_parens} parentheses, {max_braces} braces");
86}
Source

pub fn included_from(&self) -> Option<Span>

The #include directive that opened the file, if one did.

Source

pub fn line_text(&self, offset: u32) -> (&str, u32)

The text of the physical line around a byte offset, and the offset where that line starts.

Trait Implementations§

Source§

impl Clone for SourceFile

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SourceFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for SourceFile

Source§

impl PartialEq for SourceFile

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SourceFile

Auto Trait Implementations§

§

impl Forget for SourceFile

§

impl Freeze for SourceFile

§

impl Move for SourceFile

§

impl RefUnwindSafe for SourceFile

§

impl Send for SourceFile
where RawVecInner: Send,

§

impl Sync for SourceFile
where RawVecInner: Sync,

§

impl Unpin for SourceFile
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for SourceFile

§

impl UnwindSafe for SourceFile
where RawVecInner: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.