Skip to content

Commit 403d4e0

Browse files
committed
profile toml version
1 parent 9f4c5d9 commit 403d4e0

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

ytflow-app-util/src/profile/export.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub fn export_profile_toml(
9999
let plugins = ytflow::data::Plugin::query_all_by_profile(profile_id, conn)?;
100100

101101
let mut doc = DocumentMut::new();
102+
doc.insert("version", TomlItem::Value(1i64.into()));
102103

103104
let metadata_table: Table = [
104105
("name", TomlValue::from(profile.name)),
@@ -291,7 +292,9 @@ mod tests {
291292
let toml = export_profile_toml(profile_id, &db).unwrap().unwrap();
292293
assert_eq!(
293294
toml,
294-
r#"[profile]
295+
r#"version = 1
296+
297+
[profile]
295298
name = "test"
296299
permanent_id = "fadd694dacc3d1c0ea7cce8077927dc5"
297300
locale = "en-US"

ytflow-app-util/src/profile/import.rs

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ fn parse_plugin_param(value: &TomlItem) -> Option<ByteBuf> {
109109
pub fn parse_profile_toml(toml: &[u8]) -> ParseTomlProfileResult<ParsedTomlProfile> {
110110
let toml = String::from_utf8_lossy(toml);
111111
let doc = toml_edit::ImDocument::parse(&*toml)?;
112+
doc.get("version")
113+
.ok_or_else(|| ParseTomlProfileError::MissingInfo("version".into()))?
114+
.as_integer()
115+
.filter(|v| *v == 1)
116+
.ok_or_else(|| ParseTomlProfileError::InvalidValue("version".into()))?;
117+
112118
let profile_table = doc
113119
.as_table()
114120
.get("profile")
@@ -240,7 +246,8 @@ mod tests {
240246

241247
#[test]
242248
fn test_parse_profile_toml() {
243-
let toml = br#"[profile]
249+
let toml = br#"version = 1
250+
[profile]
244251
name = "test"
245252
permanent_id = "fadd694dacc3d1c0ea7cce8077927dc5"
246253
locale = "en-US"
@@ -509,7 +516,8 @@ updated_at = 2024-04-27T09:43:17.191
509516

510517
#[test]
511518
fn test_parse_profile_toml_minimal_profile() {
512-
let toml = br#"[profile]
519+
let toml = br#"version = 1
520+
[profile]
513521
entry_plugins = []
514522
"#;
515523
let parsed = parse_profile_toml(toml).unwrap();
@@ -522,41 +530,50 @@ entry_plugins = []
522530

523531
#[test]
524532
fn test_parse_profile_toml_invalid_toml() {
525-
let toml = br#"[profile"#;
533+
let toml = br#"version = 1
534+
[profile"#;
526535
let err = parse_profile_toml(toml).unwrap_err();
527536
assert!(matches!(err, ParseTomlProfileError::TomlError(_)));
528537
}
529538

530539
#[test]
531540
fn test_parse_profile_toml_missing_info() {
532-
let cases: [(&[u8], &str); 5] = [
533-
(b"", "profile"),
534-
(br#"[profile]"#, "entry_plugins"),
541+
let cases: [(&[u8], &str); 6] = [
542+
(b"", "version"),
543+
(b"version = 1", "profile"),
544+
(
545+
br#"version = 1
546+
[profile]"#,
547+
"entry_plugins",
548+
),
535549
(
536550
br#"
537-
[profile]
538-
entry_plugins = []
539-
[plugins.null]
540-
"#,
551+
version = 1
552+
[profile]
553+
entry_plugins = []
554+
[plugins.null]
555+
"#,
541556
"plugins.null.plugin",
542557
),
543558
(
544559
br#"
545-
[profile]
546-
entry_plugins = []
547-
[plugins.null]
548-
plugin = "null"
549-
"#,
560+
version = 1
561+
[profile]
562+
entry_plugins = []
563+
[plugins.null]
564+
plugin = "null"
565+
"#,
550566
"plugins.null.plugin_version",
551567
),
552568
(
553569
br#"
554-
[profile]
555-
entry_plugins = []
556-
[plugins.null]
557-
plugin = "null"
558-
plugin_version = 0
559-
"#,
570+
version = 1
571+
[profile]
572+
entry_plugins = []
573+
[plugins.null]
574+
plugin = "null"
575+
plugin_version = 0
576+
"#,
560577
"plugins.null.param",
561578
),
562579
];
@@ -572,6 +589,7 @@ entry_plugins = []
572589
#[test]
573590
fn test_parse_profile_toml_invalid_entry_point() {
574591
let toml = br#"
592+
version = 1
575593
[profile]
576594
name = "test"
577595
permanent_id = "fadd694dacc3d1c0ea7cce8077927dc5"
@@ -590,49 +608,61 @@ entry_plugins = []
590608

591609
#[test]
592610
fn test_parse_profile_toml_invalid_value() {
593-
let cases: [(&[u8], &str); 10] = [
594-
(b"profile = 1", "profile"),
611+
let cases: [(&[u8], &str); 11] = [
612+
(br#"version = "aa""#, "version"),
595613
(
596-
br#"[profile]
614+
b"version = 1
615+
profile = 1",
616+
"profile",
617+
),
618+
(
619+
br#"version = 1
620+
[profile]
597621
permanent_id = 1
598622
"#,
599623
"permanent_id",
600624
),
601625
(
602-
br#"[profile]
626+
br#"version = 1
627+
[profile]
603628
permanent_id = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
604629
"#,
605630
"permanent_id",
606631
),
607632
(
608-
br#"[profile]
633+
br#"version = 1
634+
[profile]
609635
entry_plugins = 1
610636
"#,
611637
"entry_plugins",
612638
),
613639
(
614-
br#"[profile]
640+
br#"version = 1
641+
[profile]
615642
entry_plugins = [1]
616643
"#,
617644
"entry_plugins",
618645
),
619646
(
620-
br#"plugins = 1
647+
br#"version = 1
648+
plugins = 1
621649
[profile]
622650
entry_plugins = []
623651
"#,
624652
"plugins",
625653
),
626654
(
627-
br#"[profile]
655+
br#"version = 1
656+
[profile]
628657
entry_plugins = []
629658
[plugins.null]
630659
plugin = 1
631660
"#,
632661
"plugins.null.plugin",
633662
),
634663
(
635-
br#"[profile]
664+
br#"version = 1
665+
[profile]
636666
entry_plugins = []
637667
[plugins.null]
638668
plugin = "null"
@@ -641,7 +671,8 @@ entry_plugins = []
641671
"plugins.null.plugin_version",
642672
),
643673
(
644-
br#"[profile]
674+
br#"version = 1
675+
[profile]
645676
entry_plugins = []
646677
[plugins.null]
647678
plugin = "null"
@@ -651,7 +682,8 @@ entry_plugins = []
651682
"plugins.null.param",
652683
),
653684
(
654-
br#"[profile]
685+
br#"version = 1
686+
[profile]
655687
entry_plugins = []
656688
[plugins.null]
657689
plugin = "null"

0 commit comments

Comments
 (0)