Skip to main content

librashader_presets/parse/
mod.rs

1use std::path::Path;
2
3use nom_locate::LocatedSpan;
4use std::str;
5
6mod preset;
7mod token;
8mod value;
9
10pub(crate) type Span<'a> = LocatedSpan<&'a str>;
11pub(crate) use token::Token;
12
13use crate::context::{VideoDriver, WildcardContext};
14use crate::error::ParsePresetError;
15use crate::parse::preset::resolve_values;
16use crate::parse::value::parse_preset;
17use crate::{ShaderFeatures, ShaderPreset};
18
19pub(crate) fn remove_if<T>(values: &mut Vec<T>, f: impl FnMut(&T) -> bool) -> Option<T> {
20    values.iter().position(f).map(|idx| values.remove(idx))
21}
22
23impl ShaderPreset {
24    /// Try to parse the shader preset at the given path.
25    ///
26    /// This will add path defaults to the wildcard resolution context.
27    pub fn try_parse(
28        path: impl AsRef<Path>,
29        shader_features: ShaderFeatures,
30    ) -> Result<ShaderPreset, ParsePresetError> {
31        let mut context = WildcardContext::new();
32        context.add_path_defaults(path.as_ref());
33        let values = parse_preset(path, context)?;
34        Ok(resolve_values(values, shader_features))
35    }
36
37    /// Try to parse the shader preset at the given path.
38    ///
39    /// This will add path and driver defaults to the wildcard resolution context.
40    pub fn try_parse_with_driver_context(
41        path: impl AsRef<Path>,
42        shader_features: ShaderFeatures,
43        driver: VideoDriver,
44    ) -> Result<ShaderPreset, ParsePresetError> {
45        let mut context = WildcardContext::new();
46        context.add_path_defaults(path.as_ref());
47        context.add_video_driver_defaults(driver);
48        let values = parse_preset(path, context)?;
49        Ok(resolve_values(values, shader_features))
50    }
51
52    /// Try to parse the shader preset at the given path, with the exact provided context.
53    ///
54    /// This function does not change any of the values in the provided context, except calculating `VID-FINAL-ROT`
55    /// if `CORE-REQ-ROT` and `VID-USER-ROT` is present.
56    pub fn try_parse_with_context(
57        path: impl AsRef<Path>,
58        shader_features: ShaderFeatures,
59        context: WildcardContext,
60    ) -> Result<ShaderPreset, ParsePresetError> {
61        let values = parse_preset(path, context)?;
62        Ok(resolve_values(values, shader_features))
63    }
64}
65
66#[cfg(test)]
67mod test {
68    use crate::ShaderPreset;
69    use librashader_common::shader_features::ShaderFeatures;
70    use std::path::PathBuf;
71
72    #[test]
73    pub fn parse_preset() {
74        let root = PathBuf::from("../test/shaders_slang/ntsc/ntsc-256px-svideo.slangp");
75        let basic = ShaderPreset::try_parse(root, ShaderFeatures::empty());
76        eprintln!("{basic:#?}");
77        assert!(basic.is_ok());
78    }
79}