logs: Silence missing file warnings
This commit is contained in:
		@ -3,13 +3,13 @@ extern crate xkbcommon;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use std::env;
 | 
					use std::env;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use rs::data::{ load_layout_from_resource, LoadError };
 | 
					use rs::data::{ Layout, LoadError };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use xkbcommon::xkb;
 | 
					use xkbcommon::xkb;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn check_layout(name: &str) {
 | 
					fn check_layout(name: &str) {
 | 
				
			||||||
    let layout = load_layout_from_resource(name)
 | 
					    let layout = Layout::from_resource(name)
 | 
				
			||||||
        .and_then(|layout| layout.build().map_err(LoadError::BadKeyMap))
 | 
					        .and_then(|layout| layout.build().map_err(LoadError::BadKeyMap))
 | 
				
			||||||
        .expect("layout broken");
 | 
					        .expect("layout broken");
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										95
									
								
								src/data.rs
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								src/data.rs
									
									
									
									
									
								
							@ -67,15 +67,6 @@ impl fmt::Display for LoadError {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn load_layout_from_resource(
 | 
					 | 
				
			||||||
    name: &str
 | 
					 | 
				
			||||||
) -> Result<Layout, LoadError> {
 | 
					 | 
				
			||||||
    let data = resources::get_keyboard(name)
 | 
					 | 
				
			||||||
                .ok_or(LoadError::MissingResource)?;
 | 
					 | 
				
			||||||
    serde_yaml::from_str(data)
 | 
					 | 
				
			||||||
                .map_err(|e| LoadError::BadResource(e))
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(Debug, PartialEq)]
 | 
					#[derive(Debug, PartialEq)]
 | 
				
			||||||
enum DataSource {
 | 
					enum DataSource {
 | 
				
			||||||
    File(PathBuf),
 | 
					    File(PathBuf),
 | 
				
			||||||
@ -101,11 +92,13 @@ fn load_layout(
 | 
				
			|||||||
    DataSource, // last attempt source
 | 
					    DataSource, // last attempt source
 | 
				
			||||||
    Option<(LoadError, DataSource)>, // first attempt source
 | 
					    Option<(LoadError, DataSource)>, // first attempt source
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
    let path = keyboards_path.map(|path| path.join(name).with_extension("yaml"));
 | 
					    let path = keyboards_path.map(|path|
 | 
				
			||||||
 | 
					        path.join(name).with_extension("yaml")
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let layout = match path {
 | 
					    let layout = match path {
 | 
				
			||||||
        Some(path) => Some((
 | 
					        Some(path) => Some((
 | 
				
			||||||
            Layout::from_yaml_stream(path.clone())
 | 
					            Layout::from_file(path.clone())
 | 
				
			||||||
                .map_err(LoadError::BadData)
 | 
					                .map_err(LoadError::BadData)
 | 
				
			||||||
                .and_then(|layout|
 | 
					                .and_then(|layout|
 | 
				
			||||||
                    layout.build().map_err(LoadError::BadKeyMap)
 | 
					                    layout.build().map_err(LoadError::BadKeyMap)
 | 
				
			||||||
@ -124,7 +117,7 @@ fn load_layout(
 | 
				
			|||||||
    let (layout, source) = match layout {
 | 
					    let (layout, source) = match layout {
 | 
				
			||||||
        Some((layout, path)) => (Ok(layout), path),
 | 
					        Some((layout, path)) => (Ok(layout), path),
 | 
				
			||||||
        None => (
 | 
					        None => (
 | 
				
			||||||
            load_layout_from_resource(name)
 | 
					            Layout::from_resource(name)
 | 
				
			||||||
                .and_then(|layout|
 | 
					                .and_then(|layout|
 | 
				
			||||||
                    layout.build().map_err(LoadError::BadKeyMap)
 | 
					                    layout.build().map_err(LoadError::BadKeyMap)
 | 
				
			||||||
                ),
 | 
					                ),
 | 
				
			||||||
@ -135,6 +128,24 @@ fn load_layout(
 | 
				
			|||||||
    (layout, source, failed_attempt)
 | 
					    (layout, source, failed_attempt)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn log_attempt_info(attempt: Option<(LoadError, DataSource)>) {
 | 
				
			||||||
 | 
					    match attempt {
 | 
				
			||||||
 | 
					        Some((
 | 
				
			||||||
 | 
					            LoadError::BadData(Error::Missing(_e)),
 | 
				
			||||||
 | 
					            DataSource::File(_file)
 | 
				
			||||||
 | 
					        )) => {
 | 
				
			||||||
 | 
					            // Missing file, not to worry. TODO: print in debug logging level
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Some((e, source)) => {
 | 
				
			||||||
 | 
					            eprintln!(
 | 
				
			||||||
 | 
					                "Failed to load layout from {}: {}, trying builtin",
 | 
				
			||||||
 | 
					                source, e
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        _ => {}
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn load_layout_with_fallback(
 | 
					fn load_layout_with_fallback(
 | 
				
			||||||
    name: &str
 | 
					    name: &str
 | 
				
			||||||
) -> ::layout::Layout {
 | 
					) -> ::layout::Layout {
 | 
				
			||||||
@ -144,12 +155,7 @@ fn load_layout_with_fallback(
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    let (layout, source, attempt) = load_layout(name, path.clone());
 | 
					    let (layout, source, attempt) = load_layout(name, path.clone());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if let Some((e, source)) = attempt {
 | 
					    log_attempt_info(attempt);
 | 
				
			||||||
        eprintln!(
 | 
					 | 
				
			||||||
            "Failed to load layout from {}: {}, trying builtin",
 | 
					 | 
				
			||||||
            source, e
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    let (layout, source, attempt) = match (layout, source) {
 | 
					    let (layout, source, attempt) = match (layout, source) {
 | 
				
			||||||
        (Err(e), source) => {
 | 
					        (Err(e), source) => {
 | 
				
			||||||
@ -162,12 +168,7 @@ fn load_layout_with_fallback(
 | 
				
			|||||||
        (res, source) => (res, source, None),
 | 
					        (res, source) => (res, source, None),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if let Some((e, source)) = attempt {
 | 
					    log_attempt_info(attempt);
 | 
				
			||||||
        eprintln!(
 | 
					 | 
				
			||||||
            "Failed to load layout from {}: {}, trying builtin",
 | 
					 | 
				
			||||||
            source, e
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    match (layout, source) {
 | 
					    match (layout, source) {
 | 
				
			||||||
        (Err(e), source) => {
 | 
					        (Err(e), source) => {
 | 
				
			||||||
@ -241,10 +242,15 @@ struct Outline {
 | 
				
			|||||||
    bounds: Bounds,
 | 
					    bounds: Bounds,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Errors encountered loading the layout into yaml
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub enum Error {
 | 
					pub enum Error {
 | 
				
			||||||
    Yaml(serde_yaml::Error),
 | 
					    Yaml(serde_yaml::Error),
 | 
				
			||||||
    Io(io::Error),
 | 
					    Io(io::Error),
 | 
				
			||||||
 | 
					    /// The file was missing.
 | 
				
			||||||
 | 
					    /// It's distinct from Io in order to make it matchable
 | 
				
			||||||
 | 
					    /// without calling io::Error::kind()
 | 
				
			||||||
 | 
					    Missing(io::Error),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl fmt::Display for Error {
 | 
					impl fmt::Display for Error {
 | 
				
			||||||
@ -252,21 +258,38 @@ impl fmt::Display for Error {
 | 
				
			|||||||
        match self {
 | 
					        match self {
 | 
				
			||||||
            Error::Yaml(e) => write!(f, "YAML: {}", e),
 | 
					            Error::Yaml(e) => write!(f, "YAML: {}", e),
 | 
				
			||||||
            Error::Io(e) => write!(f, "IO: {}", e),
 | 
					            Error::Io(e) => write!(f, "IO: {}", e),
 | 
				
			||||||
 | 
					            Error::Missing(e) => write!(f, "Missing: {}", e),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl From<io::Error> for Error {
 | 
				
			||||||
 | 
					    fn from(e: io::Error) -> Self {
 | 
				
			||||||
 | 
					        let kind = e.kind();
 | 
				
			||||||
 | 
					        match kind {
 | 
				
			||||||
 | 
					            io::ErrorKind::NotFound => Error::Missing(e),
 | 
				
			||||||
 | 
					            _ => Error::Io(e),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Layout {
 | 
					impl Layout {
 | 
				
			||||||
    fn from_yaml_stream(path: PathBuf) -> Result<Layout, Error> {
 | 
					    pub fn from_resource(name: &str) -> Result<Layout, LoadError> {
 | 
				
			||||||
 | 
					        let data = resources::get_keyboard(name)
 | 
				
			||||||
 | 
					                    .ok_or(LoadError::MissingResource)?;
 | 
				
			||||||
 | 
					        serde_yaml::from_str(data)
 | 
				
			||||||
 | 
					                    .map_err(LoadError::BadResource)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn from_file(path: PathBuf) -> Result<Layout, Error> {
 | 
				
			||||||
        let infile = BufReader::new(
 | 
					        let infile = BufReader::new(
 | 
				
			||||||
            fs::OpenOptions::new()
 | 
					            fs::OpenOptions::new()
 | 
				
			||||||
                .read(true)
 | 
					                .read(true)
 | 
				
			||||||
                .open(&path)
 | 
					                .open(&path)?
 | 
				
			||||||
                .map_err(Error::Io)?
 | 
					 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        serde_yaml::from_reader(infile)
 | 
					        serde_yaml::from_reader(infile).map_err(Error::Yaml)
 | 
				
			||||||
            .map_err(Error::Yaml)
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn build(self) -> Result<::layout::Layout, FormattingError> {
 | 
					    pub fn build(self) -> Result<::layout::Layout, FormattingError> {
 | 
				
			||||||
        let button_names = self.views.values()
 | 
					        let button_names = self.views.values()
 | 
				
			||||||
            .flat_map(|rows| {
 | 
					            .flat_map(|rows| {
 | 
				
			||||||
@ -520,9 +543,7 @@ mod tests {
 | 
				
			|||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn test_parse_path() {
 | 
					    fn test_parse_path() {
 | 
				
			||||||
        assert_eq!(
 | 
					        assert_eq!(
 | 
				
			||||||
            Layout::from_yaml_stream(
 | 
					            Layout::from_file(PathBuf::from("tests/layout.yaml")).unwrap(),
 | 
				
			||||||
                PathBuf::from("tests/layout.yaml")
 | 
					 | 
				
			||||||
            ).unwrap(),
 | 
					 | 
				
			||||||
            Layout {
 | 
					            Layout {
 | 
				
			||||||
                row_spacing: 0f64,
 | 
					                row_spacing: 0f64,
 | 
				
			||||||
                button_spacing: 0f64,
 | 
					                button_spacing: 0f64,
 | 
				
			||||||
@ -553,7 +574,7 @@ mod tests {
 | 
				
			|||||||
    /// Check if the default protection works
 | 
					    /// Check if the default protection works
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn test_empty_views() {
 | 
					    fn test_empty_views() {
 | 
				
			||||||
        let out = Layout::from_yaml_stream(PathBuf::from("tests/layout2.yaml"));
 | 
					        let out = Layout::from_file(PathBuf::from("tests/layout2.yaml"));
 | 
				
			||||||
        match out {
 | 
					        match out {
 | 
				
			||||||
            Ok(_) => assert!(false, "Data mistakenly accepted"),
 | 
					            Ok(_) => assert!(false, "Data mistakenly accepted"),
 | 
				
			||||||
            Err(e) => {
 | 
					            Err(e) => {
 | 
				
			||||||
@ -571,7 +592,7 @@ mod tests {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn test_extra_field() {
 | 
					    fn test_extra_field() {
 | 
				
			||||||
        let out = Layout::from_yaml_stream(PathBuf::from("tests/layout3.yaml"));
 | 
					        let out = Layout::from_file(PathBuf::from("tests/layout3.yaml"));
 | 
				
			||||||
        match out {
 | 
					        match out {
 | 
				
			||||||
            Ok(_) => assert!(false, "Data mistakenly accepted"),
 | 
					            Ok(_) => assert!(false, "Data mistakenly accepted"),
 | 
				
			||||||
            Err(e) => {
 | 
					            Err(e) => {
 | 
				
			||||||
@ -590,7 +611,7 @@ mod tests {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn test_layout_punctuation() {
 | 
					    fn test_layout_punctuation() {
 | 
				
			||||||
        let out = Layout::from_yaml_stream(PathBuf::from("tests/layout_key1.yaml"))
 | 
					        let out = Layout::from_file(PathBuf::from("tests/layout_key1.yaml"))
 | 
				
			||||||
            .unwrap()
 | 
					            .unwrap()
 | 
				
			||||||
            .build()
 | 
					            .build()
 | 
				
			||||||
            .unwrap();
 | 
					            .unwrap();
 | 
				
			||||||
@ -605,7 +626,7 @@ mod tests {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn test_layout_unicode() {
 | 
					    fn test_layout_unicode() {
 | 
				
			||||||
        let out = Layout::from_yaml_stream(PathBuf::from("tests/layout_key2.yaml"))
 | 
					        let out = Layout::from_file(PathBuf::from("tests/layout_key2.yaml"))
 | 
				
			||||||
            .unwrap()
 | 
					            .unwrap()
 | 
				
			||||||
            .build()
 | 
					            .build()
 | 
				
			||||||
            .unwrap();
 | 
					            .unwrap();
 | 
				
			||||||
@ -620,7 +641,7 @@ mod tests {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn parsing_fallback() {
 | 
					    fn parsing_fallback() {
 | 
				
			||||||
        assert!(load_layout_from_resource(FALLBACK_LAYOUT_NAME)
 | 
					        assert!(Layout::from_resource(FALLBACK_LAYOUT_NAME)
 | 
				
			||||||
            .and_then(|layout| layout.build().map_err(LoadError::BadKeyMap))
 | 
					            .and_then(|layout| layout.build().map_err(LoadError::BadKeyMap))
 | 
				
			||||||
            .is_ok()
 | 
					            .is_ok()
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user