1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
extern crate toml;
use toml::Value;
use std::error::Error;

/// # Examples
///
///```
///use cargo_toml_validate;
///let cargo_toml = "...";
///
///match cargo_toml_validate::validate(cargo_toml) {
///    Ok(()) => { /* Everything's fine */ },
///    Err(errors) => {
///        for error in errors {
///            println!("{:?}", error);
///        }
///    }
///}
///```
pub fn validate(cargo_toml: &str) -> Result<(), Vec<String>> {
    let toml_table = match cargo_toml.parse::<Value>() {
        Ok(table) => table,
        Err(err) => return Err(vec!(err.description().into()))
    };

    let package_section = toml_table["package"].as_table().unwrap();

    let mut errors = vec!();

    if package_section.get("description").is_none() {
        errors.push("Description is missing".into());
    }

    if package_section.get("license").is_none() {
        if package_section.get("license-file").is_none() {
            errors.push("license or license-file is missing".into());
        }
    }

    if package_section.get("homepage").is_none() {
        if package_section.get("repository").is_none() {
            errors.push("homepage or repository are missing".into());
        }
    }

    if errors.len() > 0 {
        Err(errors)
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn invalid_cargo_toml() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"0.1.0\"
authors = [\"Jan Schulte <hello@unexpected-co.de>\"]

[dependencies]
".into()
    }

    fn malformed_cargo_toml() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"0.1.0\"
autho hulte <hello@unexpected-co.de>\"]

[dependencies]
".into()
    }


    fn description_is_missing() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"2.0.0\"
authors = [\"Jan Schulte <hello@unexpected-co.de>\"]
license = \"MIT\"
repository = \"https://github.com/schultyy/cargo_toml_validate\"
".into()
    }

    fn repository_is_missing() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"2.0.0\"
authors = [\"Jan Schulte <hello@unexpected-co.de>\"]
license = \"MIT\"
description = \"This and that\"
".into()
    }

    fn license_is_missing() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"2.0.0\"
authors = [\"Jan Schulte <hello@unexpected-co.de>\"]
repository = \"https://github.com/schultyy/cargo_toml_validate\"
description = \"This and that\"
".into()
    }

    fn valid_cargo_toml() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"2.0.0\"
authors = [\"Jan Schulte <hello@unexpected-co.de>\"]
license = \"MIT\"
description = \"This and that\"
repository = \"https://github.com/schultyy/cargo_toml_validate\"
".into()
    }

    fn with_license_file() -> String {
"
[package]
name = \"cargo_toml_validate\"
version = \"2.0.0\"
authors = [\"Jan Schulte <hello@unexpected-co.de>\"]
license-file = \"license.md\"
description = \"This and that\"
repository = \"https://github.com/schultyy/cargo_toml_validate\"
".into()
    }

    #[test]
    fn it_should_fail_with_errors() {
        let results = validate(&invalid_cargo_toml());
        assert!(results.is_err());
        assert_eq!(3, results.unwrap_err().len());
    }

    #[test]
    fn it_should_fail_when_cargo_toml_is_malformed() {
        let results = validate(&malformed_cargo_toml());
        assert!(results.is_err());
        assert_eq!(1, results.unwrap_err().len());
    }

    #[test]
    fn it_should_fail_when_description_is_missing() {
        let results = validate(&description_is_missing());
        assert!(results.is_err());
        assert_eq!(1, results.unwrap_err().len());
    }

    #[test]
    fn it_should_fail_when_license_is_missing() {
        let results = validate(&license_is_missing());
        assert!(results.is_err());
        assert_eq!(1, results.unwrap_err().len());
    }

    #[test]
    fn it_should_fail_when_repository_is_missing() {
        let results = validate(&repository_is_missing());
        assert!(results.is_err());
        assert_eq!(1, results.unwrap_err().len());
    }

    #[test]
    fn it_passes_with_valid_cargo_toml() {
        let results = validate(&valid_cargo_toml());
        assert!(results.is_ok());
    }

    #[test]
    fn it_passes_with_license_file_instead_of_license() {
        let results = validate(&with_license_file());
        assert!(results.is_ok());
    }
}