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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*  artifact: the requirements tracking tool made for developers
 * Copyright (C) 2017  Garrett Berg <@vitiral, vitiral@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
//! project wide types

use dev_prefix::*;
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering};

pub const REPO_VAR: &'static str = "repo";
pub const CWD_VAR: &'static str = "cwd";

lazy_static!{
    // must start with artifact type, followed by "-", followed by at least 1 valid character
    // cannot end with "-"
    pub static ref ART_VALID: Regex = Regex::new(
        r"^(REQ|SPC|RSK|TST)(-[A-Z0-9_-]*[A-Z0-9_])?$").unwrap();
    pub static ref PARENT_PATH: PathBuf = PathBuf::from("PARENT");
    pub static ref INCREMENTING_ID: AtomicUsize = AtomicUsize::new(0);
}

pub type Artifacts = HashMap<ArtNameRc, Artifact>;
pub type ArtNameRc = Arc<ArtName>;
pub type ArtNames = HashSet<ArtNameRc>;

/// used for artifact ids
fn get_unique_id() -> usize {
    INCREMENTING_ID.fetch_add(1, AtomicOrdering::SeqCst)
}

pub trait LoadFromStr: Sized {
    fn from_str(s: &str) -> Result<Self>;
}

/// represents the results and all the data necessary
/// to reconstruct a loaded project
#[derive(Debug, Clone)]
pub struct Project {
    pub artifacts: Artifacts,
    pub settings: Settings,
    pub files: HashSet<PathBuf>,
    pub dne_locs: HashMap<ArtName, Loc>,

    // preserved locations where each piece is from
    pub origin: PathBuf,
    pub repo_map: HashMap<PathBuf, PathBuf>,
}

impl Default for Project {
    fn default() -> Project {
        Project {
            artifacts: Artifacts::default(),
            settings: Settings::default(),
            files: HashSet::default(),
            dne_locs: HashMap::default(),

            origin: PARENT_PATH.to_path_buf(),
            repo_map: HashMap::default(),
        }
    }
}

fn names_equal(a: &Artifacts, b: &Artifacts) -> Result<()> {
    let a_keys: HashSet<ArtNameRc> = a.keys().cloned().collect();
    let b_keys: HashSet<ArtNameRc> = b.keys().cloned().collect();
    if b_keys != a_keys {
        let missing = a_keys.symmetric_difference(&b_keys)
            .collect::<Vec<_>>();
        let msg = format!("missing artifacts: {:?}\nFIRST:\n{:?}\nSECOND:\n{:?}",
                          missing,
                          a_keys,
                          b_keys);
        Err(ErrorKind::NotEqual(msg).into())
    } else {
        Ok(())
    }
}

/// assert that the attributes are equal on the artifact
/// if they are not, then find what is different and include
/// in the error description.
///
/// This is very expensive for values that differ
fn attr_equal<T, F>(attr: &str, a: &Artifacts, b: &Artifacts, get_attr: &F) -> Result<()>
    where T: Debug + PartialEq,
          F: Fn(&Artifact) -> &T
{
    let mut diff: Vec<String> = Vec::new();

    for (a_name, a_art) in a.iter() {
        let b_art = b.get(a_name).unwrap();
        let a_attr = get_attr(a_art);
        let b_attr = get_attr(b_art);
        if a_attr != b_attr {
            let mut a_str = format!("{:?}", a_attr);
            let mut b_str = format!("{:?}", b_attr);
            let a_big = if a_str.len() > 100 { "..." } else { "" };
            let b_big = if b_str.len() > 100 { "..." } else { "" };
            a_str.truncate(100);
            b_str.truncate(100);
            diff.push(format!("[{}: {}{} != {}{}]", a_name, a_str, a_big, b_str, b_big));
        }
    }

    if diff.is_empty() {
        Ok(())
    } else {
        Err(ErrorKind::NotEqual(format!("{} different: {:?}", attr, diff)).into())
    }
}

/// num *approximately* equal
fn float_equal<F>(attr: &str, a: &Artifacts, b: &Artifacts, get_num: &F) -> Result<()>
    where F: Fn(&Artifact) -> f32
{
    let mut diff: Vec<String> = Vec::new();
    fn thous(f: f32) -> i64 {
        (f * 1000.) as i64
    }

    for (a_name, a_art) in a.iter() {
        let b_art = b.get(a_name).unwrap();
        let a_attr = get_num(a_art);
        let b_attr = get_num(b_art);
        if thous(a_attr) != thous(b_attr) {
            let mut a_str = format!("{:?}", a_attr);
            let mut b_str = format!("{:?}", b_attr);
            a_str.truncate(50);
            b_str.truncate(50);
            diff.push(format!("({}, {} != {})", a_name, a_str, b_str));
        }
    }

    if diff.is_empty() {
        Ok(())
    } else {
        Err(ErrorKind::NotEqual(format!("{} different: {:?}", attr, diff)).into())
    }
}

fn proj_attr_equal<T>(attr: &str, a: &T, b: &T) -> Result<()>
    where T: Debug + PartialEq
{
    if a != b {
        Err(ErrorKind::NotEqual(format!("{} FIRST:\n{:?}\n\nSECOND:\n{:?}", attr, a, b)).into())
    } else {
        Ok(())
    }
}


impl Project {
    /// better than equal... has reasons why NOT equal!
    pub fn equal(&self, other: &Project) -> Result<()> {
        names_equal(&self.artifacts, &other.artifacts)?;
        attr_equal("path",
                   &self.artifacts,
                   &other.artifacts,
                   &|a: &Artifact| &a.path)?;
        attr_equal("text",
                   &self.artifacts,
                   &other.artifacts,
                   &|a: &Artifact| &a.text)?;
        attr_equal("partof",
                   &self.artifacts,
                   &other.artifacts,
                   &|a: &Artifact| &a.partof)?;
        attr_equal("parts",
                   &self.artifacts,
                   &other.artifacts,
                   &|a: &Artifact| &a.parts)?;
        attr_equal("loc",
                   &self.artifacts,
                   &other.artifacts,
                   &|a: &Artifact| &a.loc)?;
        float_equal("completed",
                    &self.artifacts,
                    &other.artifacts,
                    &|a: &Artifact| a.completed)?;
        float_equal("tested",
                    &self.artifacts,
                    &other.artifacts,
                    &|a: &Artifact| a.tested)?;
        proj_attr_equal("origin", &self.origin, &other.origin)?;
        proj_attr_equal("settings", &self.settings, &other.settings)?;
        proj_attr_equal("files", &self.files, &other.files)?;
        proj_attr_equal("dne_locs", &self.dne_locs, &other.dne_locs)?;
        proj_attr_equal("repo_map", &self.repo_map, &other.repo_map)?;
        Ok(())
    }
}

/// struct for representing a project as just a collection of
/// Path and String values, used for loading/formatting/saving files
#[derive(Debug, PartialEq)]
pub struct ProjectText {
    pub origin: PathBuf,
    pub files: HashMap<PathBuf, String>,
}

impl Default for ProjectText {
    fn default() -> ProjectText {
        ProjectText {
            origin: PARENT_PATH.to_path_buf(),
            files: HashMap::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct RawArtifact {
    pub partof: Option<String>,
    pub text: Option<String>,
}

#[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct RawSettings {
    pub artifact_paths: Option<Vec<String>>,
    pub code_paths: Option<Vec<String>>,
    pub exclude_code_paths: Option<Vec<String>>,
    pub additional_repos: Option<Vec<String>>,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ArtType {
    REQ,
    SPC,
    RSK,
    TST,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Loc {
    pub path: PathBuf,
    pub line_col: (usize, usize),
}

impl Loc {
    pub fn fake() -> Loc {
        Loc {
            path: Path::new("fake").to_path_buf(),
            line_col: (42, 0),
        }
    }
}

impl fmt::Display for Loc {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "{}({}:{})",
               self.path.display(),
               self.line_col.0,
               self.line_col.1)
    }
}

/// Definition of an artifact name, with Traits for hashing,
/// displaying, etc
// note: methods are implemented in name.rs
#[derive(Clone)]
pub struct ArtName {
    pub raw: String,
    pub value: Vec<String>,
    pub ty: ArtType,
}

/// The Artifact type. This encapsulates
/// REQ, SPC, RSK, and TST artifacts and
/// contains space to link them
/// #SPC-artifact
#[derive(Clone, Debug, PartialEq)]
pub struct Artifact {
    // directly loaded types
    pub path: PathBuf,
    pub text: String,
    pub partof: ArtNames,
    pub parts: ArtNames,
    pub loc: Option<Loc>,
    pub completed: f32, // completed ratio (calculated)
    pub tested: f32, // tested ratio (calculated)
}

impl Artifact {
    pub fn is_parent(&self) -> bool {
        self.path == PARENT_PATH.as_path()
    }

    pub fn to_data(&self, name: &ArtNameRc) -> ArtifactData {
        ArtifactData {
            id: get_unique_id() as u64,
            name: name.raw.clone(),
            path: self.path.to_string_lossy().to_string(),
            text: self.text.clone(),
            partof: self.partof.iter().map(|n| n.raw.clone()).collect(),
            parts: self.parts.iter().map(|n| n.raw.clone()).collect(),
            loc: self.loc.as_ref().map(|l| {
                LocData {
                    path: l.path.to_string_lossy().to_string(),
                    row: l.line_col.0 as u64,
                    col: l.line_col.1 as u64,
                }
            }),
            completed: self.completed,
            tested: self.tested,
        }
    }

    pub fn from_data(data: &ArtifactData) -> Result<(ArtNameRc, Artifact)> {
        let name = try!(ArtNameRc::from_str(&data.name));
        let mut partof: HashSet<ArtNameRc> = HashSet::new();
        for p in &data.partof {
            let pname = try!(ArtNameRc::from_str(p));
            partof.insert(pname);
        }
        Ok((name,
            Artifact {
                path: PathBuf::from(&data.path),
                text: data.text.clone(),
                partof: partof,
                loc: None,
                parts: HashSet::new(),
                completed: -1.0,
                tested: -1.0,
            }))
    }
}


#[derive(Debug, Default, Clone, PartialEq)]
pub struct Settings {
    pub artifact_paths: HashSet<PathBuf>,
    pub code_paths: VecDeque<PathBuf>,
    pub exclude_code_paths: VecDeque<PathBuf>,
    pub additional_repos: VecDeque<PathBuf>,
}

impl Settings {
    pub fn new() -> Settings {
        Settings {
            artifact_paths: HashSet::new(),
            code_paths: VecDeque::new(),
            exclude_code_paths: VecDeque::new(),
            additional_repos: VecDeque::new(),
        }
    }
}

// ##################################################
// Data Types

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct LocData {
    pub path: String,
    pub row: u64,
    pub col: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ArtifactData {
    pub id: u64,
    pub name: String,
    pub path: String,
    pub text: String,
    pub partof: Vec<String>,

    // // TODO: until I serde gets up to speed, the web-api will
    // // have to send these values even though they are ignored
    //#[serde(default)]
    pub parts: Vec<String>,
    //#[serde(default)]
    pub loc: Option<LocData>,
    //#[serde(default = -1)]
    pub completed: f32,
    //#[serde(default = -1)]
    pub tested: f32,
}

#[allow(non_camel_case_types)]
pub enum RpcErrors {
    xIdsNotFound,
    xFilesNotFound,
    xInvalidName,
    xInvalidPartof,
}