gir-format-check-0.1.1/.gitignore010064400017500001750000000000231332143255200150370ustar0000000000000000Cargo.lock target/ gir-format-check-0.1.1/Cargo.toml.orig010064400017500001750000000006361332143362000157450ustar0000000000000000[package] name = "gir-format-check" license = "MIT" homepage = "https://github.com/gtk-rs/gir-format-check" authors = ["The Gtk-rs Project Developers"] keywords = ["gtk-rs", "gir", "check"] readme = "README.md" documentation = "https://docs.rs/crate/gir-format-check/" version = "0.1.1" description = "File format checker" repository = "https://github.com/gtk-rs/gir-format-check" [lib] name = "gir_format_check" gir-format-check-0.1.1/Cargo.toml0000644000000016520000000000000122160ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "gir-format-check" version = "0.1.1" authors = ["The Gtk-rs Project Developers"] description = "File format checker" homepage = "https://github.com/gtk-rs/gir-format-check" documentation = "https://docs.rs/crate/gir-format-check/" readme = "README.md" keywords = ["gtk-rs", "gir", "check"] license = "MIT" repository = "https://github.com/gtk-rs/gir-format-check" [lib] name = "gir_format_check" gir-format-check-0.1.1/LICENSE010064400017500001750000000020601332143255200140570ustar0000000000000000MIT License Copyright (c) 2018 Guillaume Gomez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gir-format-check-0.1.1/README.md010064400017500001750000000001111332143255200143240ustar0000000000000000# file-format-check Used to check `Gir.toml` files for `gtk-rs` crates. gir-format-check-0.1.1/src/check_gir.rs010064400017500001750000000106621332143357300161400ustar0000000000000000// Copyright 2013-2018, The Gtk-rs Project Developers. // See the COPYRIGHT file at the top-level directory of this distribution. // Licensed under the MIT license, see the LICENSE file or use utils; use errors::Errors; use std::cmp::Ordering; use std::path::Path; #[derive(Debug)] struct Elem<'a> { name: &'a str, lower: String, pos: usize, } impl<'a> Elem<'a> { fn new(name: &str, pos: usize) -> Elem { Elem { name, lower: name.to_lowercase(), pos, } } } impl<'a> Eq for Elem<'a> {} impl<'a> PartialEq for Elem<'a> { fn eq(&self, other: &Elem) -> bool { self.lower == other.lower } } impl<'a> PartialOrd for Elem<'a> { fn partial_cmp(&self, other: &Elem) -> Option { Some(self.cmp(other)) } } impl<'a> Ord for Elem<'a> { fn cmp(&self, other: &Elem) -> Ordering { if self.lower.ends_with("*") { // We always it to be first "because"! Ordering::Less } else { self.lower.cmp(&other.lower) } } } pub fn check_gir_content(content: &str) -> Errors { let lines = content.lines().collect::>(); let mut elems: Vec = Vec::with_capacity(10); let mut objects = Vec::with_capacity(10); let mut in_list = None; let mut in_object = false; let mut errors = 0; let mut messages = Vec::with_capacity(10); for pos in 0..lines.len() { if lines[pos].ends_with("[") { in_list = Some(pos); continue } else if in_list.is_some() && lines[pos].trim() == "]" { if !elems.is_empty() { let mut local_errors = 0; for it in 0..elems.len() - 1 { if elems[it] > elems[it + 1] { messages.push(format!("ERROR: \"{}\" should be after \"{}\"", elems[it].name, elems[it + 1].name)); local_errors += 1; } } if local_errors > 0 { elems.sort(); messages.push(format!("\n== Expected output ==\n{}\n{}]", lines[in_list.unwrap()], elems.iter() .map(|l| format!("{}\n", lines[l.pos])) .collect::())); errors += local_errors; } } elems.clear(); in_list = None; } else if in_list.is_some() { let trimmed = lines[pos].trim(); let mut len = trimmed.len(); if trimmed.ends_with("\",") { len -= 2; } else if trimmed.ends_with(",") { len -= 1; } else if trimmed.ends_with("\"") { len -= 1; } let start = if trimmed.starts_with("\"") { 1 } else { 0 }; elems.push(Elem::new(&trimmed[start..len], pos)); } else if lines[pos] == "[[object]]" { in_object = true; } else if in_object == true && lines[pos].starts_with("name = \"") { let trimmed = lines[pos].trim(); let len = trimmed.len() - 1; objects.push(Elem::new(&lines[pos].trim()[8..len], pos)); } else if lines[pos].trim().is_empty() { in_object = false; } } if !objects.is_empty() { let mut local_errors = 0; for it in 0..objects.len() - 1 { if objects[it] > objects[it + 1] { messages.push(format!("ERROR: \"{}\" should be after \"{}\"", objects[it].name, objects[it + 1].name)); local_errors += 1; } } if local_errors > 0 { objects.sort(); messages.push(format!("\n== Expected order ==\n{}", objects.iter() .map(|l| format!("{}\n", l.name)) .collect::())); errors += local_errors; } } Errors { nb_errors: errors, messages, } } pub fn check_gir_file>(p: P) -> Errors { let content = utils::read_file(p); check_gir_content(&content) } gir-format-check-0.1.1/src/errors.rs010064400017500001750000000007331332143255200155300ustar0000000000000000// Copyright 2013-2018, The Gtk-rs Project Developers. // See the COPYRIGHT file at the top-level directory of this distribution. // Licensed under the MIT license, see the LICENSE file or pub struct Errors { pub messages: Vec, pub nb_errors: usize, } impl Errors { pub fn has_errors(&self) -> bool { self.nb_errors > 0 } pub fn to_string(&self) -> String { self.messages.join("\n") } } gir-format-check-0.1.1/src/lib.rs010064400017500001750000000022401332143255200147550ustar0000000000000000// Copyright 2013-2018, The Gtk-rs Project Developers. // See the COPYRIGHT file at the top-level directory of this distribution. // Licensed under the MIT license, see the LICENSE file or mod check_gir; mod errors; mod utils; pub use check_gir::{check_gir_content, check_gir_file}; pub use errors::Errors; #[test] fn simple_check() { let content = r#"something = [ test1, test3, test2, ]"#; let errors = check_gir_content(content); assert_eq!(errors.nb_errors, 1); } #[test] fn simple_check2() { let content = r#" [[object]] name = "abc" [[object.func]] data = "lol" [[object]] name = "aac" [[object.func]] data = "another" "#; let errors = check_gir_content(content); assert_eq!(errors.nb_errors, 1); } #[test] fn simple_check3() { let content = r#"something = [ test1, test2, test2 ]"#; let errors = check_gir_content(content); assert_eq!(errors.nb_errors, 0); } #[test] fn simple_check4() { let content = r#"something = [ "test1", "test3", "test2", ]"#; let errors = check_gir_content(content); assert_eq!(errors.nb_errors, 1); } gir-format-check-0.1.1/src/utils.rs010064400017500001750000000010211332143255200153430ustar0000000000000000// Copyright 2013-2018, The Gtk-rs Project Developers. // See the COPYRIGHT file at the top-level directory of this distribution. // Licensed under the MIT license, see the LICENSE file or use std::fs::File; use std::io::Read; use std::path::Path; pub fn read_file>(p: P) -> String { let mut f = File::open(p).expect("read_file::open failed"); let mut content = String::new(); f.read_to_string(&mut content).expect("read_file::read_to_end failed"); content }