Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
4d995e66
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4d995e66
编写于
3月 27, 2013
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: change default json enum encoder to use strings or a flat vec
上级
b10b8c3e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
33 addition
and
56 deletion
+33
-56
src/libstd/json.rs
src/libstd/json.rs
+16
-41
src/libsyntax/parse/mod.rs
src/libsyntax/parse/mod.rs
+17
-15
未找到文件。
src/libstd/json.rs
浏览文件 @
4d995e66
...
...
@@ -119,22 +119,15 @@ fn emit_enum(&self, _name: &str, f: &fn()) {
f
()
}
fn
emit_enum_variant
(
&
self
,
name
:
&
str
,
_
id
:
uint
,
_
cnt
:
uint
,
f
:
&
fn
())
{
fn
emit_enum_variant
(
&
self
,
name
:
&
str
,
_
id
:
uint
,
cnt
:
uint
,
f
:
&
fn
())
{
// encoding of enums is special-cased for Option. Specifically:
// Some(34) => 34
// None => null
// other enums are encoded as vectors:
// other enums are encoded as strings or vectors:
// Bunny => "Bunny"
// Kangaroo(34,"William") => ["Kangaroo",[34,"William"]]
// the default expansion for enums is more verbose than I'd like;
// specifically, the inner pair of brackets seems superfluous,
// BUT the design of the enumeration framework and the requirements
// of the special-case for Option mean that a first argument must
// be encoded "naked"--with no commas--and that the option name
// can't be followed by just a comma, because there might not
// be any elements in the tuple.
// FIXME #4872: this would be more precise and less frightening
// with fully-qualified option names. To get that information,
// we'd have to change the expansion of auto-encode to pass
...
...
@@ -144,14 +137,14 @@ fn emit_enum_variant(&self, name: &str, _id: uint, _cnt: uint, f: &fn()) {
f
();
}
else
if
name
==
~
"None"
{
self
.wr
.write_str
(
~
"null"
);
}
else
if
cnt
==
0
{
self
.wr
.write_str
(
escape_str
(
name
));
}
else
{
self
.wr
.write_char
(
'['
);
self
.wr
.write_str
(
escape_str
(
name
));
self
.wr
.write_char
(
','
);
self
.wr
.write_char
(
'['
);
f
();
self
.wr
.write_char
(
']'
);
self
.wr
.write_char
(
']'
);
}
}
...
...
@@ -256,28 +249,19 @@ fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) {
f
();
}
else
if
name
==
~
"None"
{
self
.emit_nil
();
}
else
if
cnt
==
0
{
self
.wr
.write_str
(
escape_str
(
name
));
}
else
{
self
.wr
.write_char
(
'['
);
self
.indent
+=
2
;
self
.wr
.write_char
(
'\n'
);
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
escape_str
(
name
));
if
cnt
==
0
{
self
.wr
.write_str
(
",
\n
"
);
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
"[]
\n
"
);
}
else
{
self
.wr
.write_str
(
",
\n
"
);
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
"[
\n
"
);
self
.indent
+=
2
;
f
();
self
.wr
.write_char
(
'\n'
);
self
.indent
-=
2
;
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_str
(
"]
\n
"
);
}
self
.wr
.write_str
(
",
\n
"
);
f
();
self
.wr
.write_char
(
'\n'
);
self
.indent
-=
2
;
self
.wr
.write_str
(
spaces
(
self
.indent
));
self
.wr
.write_char
(
']'
);
}
}
...
...
@@ -1406,7 +1390,7 @@ fn test_write_enum_no_args() {
let
encoder
=
Encoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"
[
\"
Dog
\"
,[]]
"
);
assert_eq!
(
s
,
~
"
\"
Dog
\"
"
);
}
#[test]
...
...
@@ -1417,14 +1401,7 @@ fn test_write_enum_no_args_pretty() {
let
encoder
=
PrettyEncoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"
\
[
\n
\
\"
Dog
\"
,
\n
\
[]
\n
\
]"
);
assert_eq!
(
s
,
~
"
\"
Dog
\"
"
);
}
#[test]
...
...
@@ -1435,7 +1412,7 @@ fn test_write_enum_multiple_args() {
let
encoder
=
Encoder
(
wr
);
animal
.encode
(
&
encoder
);
};
assert_eq!
(
s
,
~
"[
\"
Frog
\"
,
[
\"
Henry
\"
,349]
]"
);
assert_eq!
(
s
,
~
"[
\"
Frog
\"
,
\"
Henry
\"
,349
]"
);
}
#[test]
...
...
@@ -1451,10 +1428,8 @@ fn test_write_enum_multiple_args_pretty() {
~
"
\
[
\n
\
\"
Frog
\"
,
\n
\
[
\n
\
\"
Henry
\"
,
\n
\
349
\n
\
]
\n
\
\"
Henry
\"
,
\n
\
349
\n
\
]"
);
}
...
...
src/libsyntax/parse/mod.rs
浏览文件 @
4d995e66
...
...
@@ -329,25 +329,27 @@ mod test {
assert_eq!
(
to_json_str
(
@
tts
),
~
"[
\
[
\"
tt_tok
\"
,[null,[
\"
IDENT
\"
,[
\"
fn
\"
,false]]]],
\
[
\"
tt_tok
\"
,[null,[
\"
IDENT
\"
,[
\"
foo
\"
,false]]]],
\
[
\"
tt_delim
\"
,[
\
[
\"
tt_tok
\"
,null,[
\"
IDENT
\"
,
\"
fn
\"
,false]],
\
[
\"
tt_tok
\"
,null,[
\"
IDENT
\"
,
\"
foo
\"
,false]],
\
[
\
\"
tt_delim
\"
,
\
[
\
[
\"
tt_tok
\"
,
[null,[
\"
LPAREN
\"
,[]]]
],
\
[
\"
tt_tok
\"
,
[null,[
\"
IDENT
\"
,[
\"
x
\"
,false]]
]],
\
[
\"
tt_tok
\"
,
[null,[
\"
COLON
\"
,[]]]
],
\
[
\"
tt_tok
\"
,
[null,[
\"
IDENT
\"
,[
\"
int
\"
,false]]
]],
\
[
\"
tt_tok
\"
,
[null,[
\"
RPAREN
\"
,[]]]
]
\
[
\"
tt_tok
\"
,
null,
\"
LPAREN
\"
],
\
[
\"
tt_tok
\"
,
null,[
\"
IDENT
\"
,
\"
x
\"
,false
]],
\
[
\"
tt_tok
\"
,
null,
\"
COLON
\"
],
\
[
\"
tt_tok
\"
,
null,[
\"
IDENT
\"
,
\"
int
\"
,false
]],
\
[
\"
tt_tok
\"
,
null,
\"
RPAREN
\"
]
\
]
\
]],
\
[
\"
tt_delim
\"
,[
\
],
\
[
\
\"
tt_delim
\"
,
\
[
\
[
\"
tt_tok
\"
,
[null,[
\"
LBRACE
\"
,[]]]
],
\
[
\"
tt_tok
\"
,
[null,[
\"
IDENT
\"
,[
\"
x
\"
,false]]
]],
\
[
\"
tt_tok
\"
,
[null,[
\"
SEMI
\"
,[]]]
],
\
[
\"
tt_tok
\"
,
[null,[
\"
RBRACE
\"
,[]]]
]
\
[
\"
tt_tok
\"
,
null,
\"
LBRACE
\"
],
\
[
\"
tt_tok
\"
,
null,[
\"
IDENT
\"
,
\"
x
\"
,false
]],
\
[
\"
tt_tok
\"
,
null,
\"
SEMI
\"
],
\
[
\"
tt_tok
\"
,
null,
\"
RBRACE
\"
]
\
]
\
]
]
\
]
\
]"
);
let
ast1
=
new_parser_from_tts
(
new_parse_sess
(
None
),
~
[],
tts
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录