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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use std::fs;
use std::path;
use lazy_static::lazy_static;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::archive::FlatpakArchiveType;
use crate::format::FlatpakManifestFormat;
pub const ARCHIVE: &str = "archive";
pub const GIT: &str = "git";
pub const BAZAAR: &str = "bzr";
pub const SVN: &str = "svn";
pub const DIR: &str = "dir";
pub const FILE: &str = "file";
pub const SCRIPT: &str = "script";
pub const SHELL: &str = "shell";
pub const PATCH: &str = "patch";
pub const EXTRA_DATA: &str = "extra-data";
lazy_static! {
pub static ref CODE_TYPES: Vec<FlatpakSourceType> = vec![
FlatpakSourceType::Archive,
FlatpakSourceType::Git,
FlatpakSourceType::Bazaar,
FlatpakSourceType::Svn,
FlatpakSourceType::Dir,
];
pub static ref VCS_TYPES: Vec<FlatpakSourceType> = vec![
FlatpakSourceType::Git,
FlatpakSourceType::Bazaar,
FlatpakSourceType::Svn,
];
}
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Debug)]
#[derive(Hash)]
#[derive(PartialEq)]
pub enum FlatpakSourceType {
Archive,
Git,
Bazaar,
Svn,
Dir,
File,
Script,
Shell,
Patch,
ExtraData,
}
impl Default for FlatpakSourceType {
fn default() -> Self {
FlatpakSourceType::Archive
}
}
impl FlatpakSourceType {
pub fn is_code(&self) -> bool {
CODE_TYPES.contains(self)
}
pub fn is_vcs(&self) -> bool {
VCS_TYPES.contains(self)
}
pub fn supports_mirror_urls(&self) -> bool {
if *self == FlatpakSourceType::Archive {
return true;
}
if *self == FlatpakSourceType::File {
return true;
}
false
}
pub fn to_string(&self) -> String {
match &self {
FlatpakSourceType::Archive => ARCHIVE.to_string(),
FlatpakSourceType::Git => GIT.to_string(),
FlatpakSourceType::Bazaar => BAZAAR.to_string(),
FlatpakSourceType::Svn => SVN.to_string(),
FlatpakSourceType::Dir => DIR.to_string(),
FlatpakSourceType::File => FILE.to_string(),
FlatpakSourceType::Script => SCRIPT.to_string(),
FlatpakSourceType::Shell => SHELL.to_string(),
FlatpakSourceType::Patch => PATCH.to_string(),
FlatpakSourceType::ExtraData => EXTRA_DATA.to_string(),
}
}
pub fn from_string(source_type: &str) -> Result<FlatpakSourceType, String> {
if source_type == ARCHIVE {
return Ok(FlatpakSourceType::Archive);
}
if source_type == GIT {
return Ok(FlatpakSourceType::Git);
}
if source_type == BAZAAR {
return Ok(FlatpakSourceType::Bazaar);
}
if source_type == SVN {
return Ok(FlatpakSourceType::Svn);
}
if source_type == DIR {
return Ok(FlatpakSourceType::Dir);
}
if source_type == FILE {
return Ok(FlatpakSourceType::File);
}
if source_type == SCRIPT {
return Ok(FlatpakSourceType::Script);
}
if source_type == SHELL {
return Ok(FlatpakSourceType::Shell);
}
if source_type == PATCH {
return Ok(FlatpakSourceType::Patch);
}
if source_type == EXTRA_DATA {
return Ok(FlatpakSourceType::ExtraData);
}
Err(format!("Invalid source type {}.", source_type))
}
}
pub fn serialize_to_string<S>(x: &Option<FlatpakSourceType>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(build_system) = x {
return s.serialize_str(&build_system.to_string());
}
panic!("This should not happen.");
}
pub fn deserialize_from_string<'de, D>(deserializer: D) -> Result<Option<FlatpakSourceType>, D::Error>
where
D: Deserializer<'de>,
{
let buf = String::deserialize(deserializer)?;
match FlatpakSourceType::from_string(&buf) {
Ok(b) => Ok(Some(b)),
Err(e) => Err(e).map_err(serde::de::Error::custom),
}
}
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Debug)]
#[derive(Hash)]
#[serde(rename_all = "kebab-case")]
#[serde(untagged)]
pub enum FlatpakSourceItem {
Path(String),
Description(FlatpakSource),
}
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Debug)]
#[derive(Default)]
#[derive(Hash)]
#[serde(rename_all = "kebab-case")]
pub struct FlatpakSource {
#[serde(deserialize_with = "crate::source::deserialize_from_string")]
#[serde(serialize_with = "crate::source::serialize_to_string")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub r#type: Option<FlatpakSourceType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub commands: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dest_filename: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub filename: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mirror_urls: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub md5: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sha1: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sha256: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sha512: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub git_init: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub installed_size: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub revision: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
#[serde(deserialize_with = "crate::archive::deserialize_from_string")]
#[serde(serialize_with = "crate::archive::serialize_to_string")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub archive_type: Option<FlatpakArchiveType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub commit: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tag: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub paths: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub use_git: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub use_git_am: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub disable_fsckobjects: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub disable_shallow_clone: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub disable_submodules: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strip_components: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub skip: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub only_arches: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub skip_arches: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dest: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub x_checker_data: Option<FlatpakDataCheckerConfig>,
}
impl FlatpakSource {
pub fn get_type(&self) -> Option<FlatpakSourceType> {
self.r#type.clone()
}
pub fn file_path_matches(path: &str) -> bool {
crate::filename::extension_is_valid(path)
}
pub fn load_from_file(path: String) -> Result<Vec<FlatpakSource>, String> {
let file_path = path::Path::new(&path);
if !file_path.is_file() {
return Err(format!("{} is not a file.", path));
}
let manifest_format = match FlatpakManifestFormat::from_path(&path) {
Some(f) => f,
None => return Err(format!("{} is not a Flatpak source manifest.", path)),
};
let manifest_content = match fs::read_to_string(file_path) {
Ok(content) => content,
Err(e) => {
return Err(format!(
"Could not read file {}: {}!",
file_path.to_str().unwrap(),
e
))
}
};
if let Ok(source) = FlatpakSource::parse(manifest_format.clone(), &manifest_content) {
return Ok(vec![source]);
}
if let Ok(sources) = FlatpakSource::parse_many(manifest_format, &manifest_content) {
return Ok(sources);
}
return Err(format!("Failed to parse Flatpak source manifest at {}.", path));
}
pub fn parse(format: FlatpakManifestFormat, manifest_content: &str) -> Result<FlatpakSource, String> {
let flatpak_source: FlatpakSource = match &format {
FlatpakManifestFormat::YAML => match serde_yaml::from_str(&manifest_content) {
Ok(m) => m,
Err(e) => {
return Err(format!("Failed to parse the Flatpak source manifest: {}.", e));
}
},
FlatpakManifestFormat::JSON => {
let json_content_without_comments = crate::utils::remove_comments_from_json(manifest_content);
match serde_json::from_str(&json_content_without_comments) {
Ok(m) => m,
Err(e) => {
return Err(format!("Failed to parse the Flatpak source manifest: {}.", e));
}
}
}
};
if let Err(e) = flatpak_source.is_valid() {
return Err(e);
}
Ok(flatpak_source)
}
pub fn parse_many(
format: FlatpakManifestFormat,
manifest_content: &str,
) -> Result<Vec<FlatpakSource>, String> {
let flatpak_sources: Vec<FlatpakSource> = match &format {
FlatpakManifestFormat::YAML => match serde_yaml::from_str(&manifest_content) {
Ok(m) => m,
Err(e) => {
return Err(format!("Failed to parse the Flatpak source manifest: {}.", e));
}
},
FlatpakManifestFormat::JSON => {
let json_content_without_comments = crate::utils::remove_comments_from_json(manifest_content);
match serde_json::from_str(&json_content_without_comments) {
Ok(m) => m,
Err(e) => {
return Err(format!("Failed to parse the Flatpak source manifest: {}.", e));
}
}
}
};
if flatpak_sources.len() == 0 {
return Err("Empty array is not a valid source manifest!".to_string());
}
for flatpak_source in &flatpak_sources {
if let Err(e) = flatpak_source.is_valid() {
return Err(e);
}
}
Ok(flatpak_sources)
}
pub fn is_valid(&self) -> Result<(), String> {
if self.url.is_none() && self.path.is_none() && self.commands.is_none() {
return Err("There should be at least a url, a path or inline commands in a source!".to_string());
}
Ok(())
}
pub fn get_url(&self) -> Option<String> {
match &self.url {
Some(s) => Some(s.to_string()),
None => None,
}
}
pub fn get_all_mirror_urls(&self) -> Vec<String> {
let mut response: Vec<String> = vec![];
if let Some(urls) = &self.mirror_urls {
for url in urls {
response.push(url.to_string());
}
}
return response;
}
pub fn get_all_urls(&self) -> Vec<String> {
let mut response: Vec<String> = vec![];
if let Some(url) = &self.url {
response.push(url.to_string());
}
if let Some(urls) = &self.mirror_urls {
for url in urls {
response.push(url.to_string());
}
}
return response;
}
pub fn get_type_name(&self) -> String {
if let Some(t) = self.get_type() {
return t.to_string();
}
return "empty".to_string();
}
pub fn supports_mirror_urls(&self) -> bool {
if let Some(t) = self.get_type() {
return t.supports_mirror_urls();
}
return false;
}
}
#[derive(Clone)]
#[derive(Deserialize)]
#[derive(Serialize)]
#[derive(Debug)]
#[derive(Default)]
#[derive(Hash)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct FlatpakDataCheckerConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version_pattern: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_main_source: Option<bool>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_parse_single_source_manifest() {
match FlatpakSource::parse(
FlatpakManifestFormat::YAML,
r###"
type: file
path: apply_extra.sh
"###,
) {
Err(e) => std::panic::panic_any(e),
Ok(source) => {
assert_eq!(source.path, Some("apply_extra.sh".to_string()));
assert_eq!(source.get_type(), Some(FlatpakSourceType::File));
}
}
}
#[test]
pub fn test_parse_multiple_source_manifests() {
match FlatpakSource::parse_many(
FlatpakManifestFormat::YAML,
r###"
- type: file
path: apply_extra.sh
- type: file
path: com.wps.Office.metainfo.xml
- type: file
path: wps.sh
- type: extra-data
filename: wps-office.deb
only-arches:
- x86_64
url: https://wdl1.pcfg.cache.wpscdn.com/wps-office_11.1.0.10702.XA_amd64.deb
sha256: 390a8b358aaccdfda54740d10d5306c2543c5cd42a7a8fd5c776ccff38492992
size: 275210770
installed-size: 988671247
x-checker-data:
type: html
url: https://linux.wps.com/js/meta.js
version-pattern: version\s*=\s*"([\d.-]+)"
url-pattern: download_link_deb\s*=\s*"(http[s]?://[\w\d$-_@.&+]+)"
"###,
) {
Err(e) => std::panic::panic_any(e),
Ok(sources) => {
assert_eq!(sources.len(), 4);
let last_source = sources.last().unwrap();
assert_eq!(last_source.filename, Some("wps-office.deb".to_string()));
}
}
}
#[test]
pub fn test_parse_invalid_type() {
let source_manifest = r###"
type: not_a_valid_source_type
path: apply_extra.sh
"###;
match FlatpakSource::parse(FlatpakManifestFormat::YAML, source_manifest) {
Ok(_source) => {
panic!("We should not be able to parse a source manifest with an invalid source type");
}
Err(e) => {
assert!(e.to_string().contains("Invalid source type"));
}
}
}
#[test]
pub fn test_parse_archive_type() {
let source_manifest = r###"
type: archive
url: https://ftp.gnu.org/gnu/glibc/glibc-2.0.1.tar.gz
archive_type: tar-gz
"###;
match FlatpakSource::parse(FlatpakManifestFormat::YAML, source_manifest) {
Ok(source) => {
assert!(source.url.is_some());
assert_eq!(source.get_type(), Some(FlatpakSourceType::Archive));
}
Err(e) => {
panic!(
"We should be able to parse a source manifest with an archive type: {}",
e
);
}
}
}
#[test]
pub fn test_parse_invalid_archive_type() {
let source_manifest = r###"
type: archive
archive-type: blahblah
url: https://ftp.gnu.org/gnu/glibc/glibc-2.0.1.tar.gz
"###;
match FlatpakSource::parse(FlatpakManifestFormat::YAML, source_manifest) {
Ok(_source) => {
println!("{:?}", _source);
panic!("We should not be able to parse a source manifest with an invalid source type");
}
Err(e) => {
assert!(e.to_string().contains("Invalid archive type"));
}
}
}
#[test]
pub fn test_parse_missing_source_type() {
let source_manifest = r###"
url: "https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz"
sha256: "b81946e7f01f90528a1f7352ab08cc602b9ccc05d4e44da4bd501c5a189ee661"
"###;
match FlatpakSource::parse(FlatpakManifestFormat::YAML, source_manifest) {
Ok(source) => {
assert!(source.url.is_some());
assert!(source.get_type().is_none());
}
Err(e) => {
panic!(
"We should be able to parse a source manifest without a source type: {}",
e
);
}
}
}
#[test]
pub fn test_parse_random_yaml_file() {
let source_manifest = r###"
title: "Copying the Diagram to the Clipboard"
content: []
description: "This is where you click for the action to happen"
"###;
if FlatpakSource::parse(FlatpakManifestFormat::YAML, source_manifest).is_ok() {
panic!("We should not parse a random yaml file as a source manifest",);
}
}
#[test]
pub fn test_parse_empty_array() {
let source_manifest = "[]";
if FlatpakSource::parse(FlatpakManifestFormat::JSON, source_manifest).is_ok() {
panic!("We should not parse an empty json array as a source manifest",);
}
if FlatpakSource::parse_many(FlatpakManifestFormat::JSON, source_manifest).is_ok() {
panic!("We should not parse an empty json array as many source manifests",);
}
}
}